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