1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
from pecan import expose, request, response
from pecan.rest import RestController
from restful import context
from restful.decorators import auth, lock, paginate
class RequestId(RestController):
def __init__(self, request_id):
self.request_id = request_id
@expose(template='json')
@auth
def get(self, **kwargs):
"""
Show the information for the request id
"""
request = [x for x in context.instance.requests
if x.id == self.request_id]
if len(request) != 1:
response.status = 500
return {'message': 'Unknown request id "{}"'.format(self.request_id)}
return request[0]
@expose(template='json')
@auth
@lock
def delete(self, **kwargs):
"""
Remove the request id from the database
"""
for index in range(len(context.instance.requests)):
if context.instance.requests[index].id == self.request_id:
return context.instance.requests.pop(index)
# Failed to find the job to cancel
response.status = 500
return {'message': 'No such request id'}
class Request(RestController):
@expose(template='json')
@paginate
@auth
def get(self, **kwargs):
"""
List all the available requests
"""
return context.instance.requests
@expose(template='json')
@auth
@lock
def delete(self, **kwargs):
"""
Remove all the finished requests
"""
num_requests = len(context.instance.requests)
context.instance.requests = [x for x in context.instance.requests
if not x.is_finished()]
remaining = len(context.instance.requests)
# Return the job statistics
return {
'cleaned': num_requests - remaining,
'remaining': remaining,
}
@expose(template='json')
@auth
def post(self, **kwargs):
"""
Pass through method to create any request
"""
if isinstance(request.json, list):
if all(isinstance(element, list) for element in request.json):
return context.instance.submit_request(request.json, **kwargs)
# The request.json has wrong format
response.status = 500
return {'message': 'The request format should be [[{c1},{c2}]]'}
return context.instance.submit_request([[request.json]], **kwargs)
@expose()
def _lookup(self, request_id, *remainder):
return RequestId(request_id), remainder
|