1
# -*- coding: utf-8 -*-
3
from __future__ import unicode_literals
10
from gatekeeper.resources import GatekeeperResource
13
class SandboxAPI(GatekeeperResource):
16
- GET '' -> list sandbox objects
17
- DELETE '' -> delete sandbox and its contents
18
- GET '{object_name}' -> returns a tempurl for downloading the object
19
- PUT '{object_name}' -> returns a tempurl for uploading the object
22
def __init__(self, identifier):
23
super(SandboxAPI, self).__init__()
24
self.identifier = identifier
26
def _render_response(self, payload):
28
(b'Content-Type', b'text/plain')
30
return http.ok(headers, str(payload))
33
def contents(self, request):
34
sandbox = self.datastore.get_sandbox(self.identifier)
35
objects = [o.title for o in sandbox.objects]
36
return self._render_response('\n'.join(objects))
39
def drop(self, request):
40
sandbox = self.datastore.get_sandbox(self.identifier)
43
'Sandbox {} deleted.'.format(sandbox.identifier))
44
return self._render_response('')
46
def _get_temp_url(self, object_name, method):
47
sandbox = self.datastore.get_sandbox(self.identifier)
48
temp_url = sandbox.get_temp_url(object_name, method)
49
return self._render_response(temp_url)
51
@resource.child('{object_name}')
52
def object_wrapper(self, request, segments, object_name):
53
allow = ['GET', 'PUT']
54
if request.method not in allow:
55
return http.method_not_allowed(allow)
56
return self._get_temp_url(object_name, request.method)
59
class API(GatekeeperResource):
60
"""Base Sandbox API provider.
62
- GET '' -> greeting message
63
- POST '' -> creates a sandbox
64
- 'sandbox/{identifier}' -> traverse to `SandboxAPI`
68
GREETINGS = b'CI-Airline gatekeeper.\n'
71
def root(self, request, segments):
72
allow = ['GET', 'POST']
73
if request.method not in allow:
74
return http.method_not_allowed(allow)
76
(b'Content-Type', b'text/plain'),
78
if request.method == 'GET':
79
return http.ok(headers, self.GREETINGS)
80
sandbox = self.datastore.create_sandbox()
81
location = b'{}/sandbox/{}'.format(self.PATH, sandbox.identifier)
82
self.logger.info('Sandbox {} created.'.format(location))
83
return http.created(location, headers, location)
85
@resource.child('sandbox/{sandbox_identifier}')
86
def object_wrapper(self, request, segments, sandbox_identifier):
87
return SandboxAPI(sandbox_identifier)