summaryrefslogtreecommitdiff
path: root/ick2/apibase.py
diff options
context:
space:
mode:
Diffstat (limited to 'ick2/apibase.py')
-rw-r--r--ick2/apibase.py63
1 files changed, 43 insertions, 20 deletions
diff --git a/ick2/apibase.py b/ick2/apibase.py
index f330eae..e18ebe2 100644
--- a/ick2/apibase.py
+++ b/ick2/apibase.py
@@ -16,10 +16,15 @@
import ick2
+import bottle
+
+
class APIbase:
def __init__(self, state):
- assert state is None or isinstance(state, ick2.MemoryPersistentState)
+ assert (state is None or
+ isinstance(state, ick2.MemoryPersistentState) or
+ isinstance(state, ick2.MuckPersistentState))
self._trans = ick2.TransactionalState(state)
def get_routes(self, path):
@@ -52,12 +57,26 @@ class APIbase:
},
]
+ # This is quite ugly. The apifw library we use as a wrapper around
+ # Bottle should be doing this. But due to stupid reasons, it's
+ # awkward to modify that library, for now, and anyway all of this
+ # is going to be rewritten in a better programming language
+ # eventually, so we take the past of least effort and increase
+ # technical debt.
+ def _get_token(self):
+ v = bottle.request.get_header('Authorization', '')
+ prefix = 'Bearer '
+ if v.startswith(prefix):
+ return v[len(prefix):]
+ return None
+
def GET(self, callback):
def wrapper(content_type, body, **kwargs):
ick2.log.log(
'trace', msg_text='GET called', kwargs=kwargs,
content_type=content_type, body=body)
try:
+ kwargs['token'] = self._get_token()
if 'raw_uri_path' in kwargs:
del kwargs['raw_uri_path']
body = callback(**kwargs)
@@ -82,6 +101,7 @@ class APIbase:
'trace', msg_text='POST called', kwargs=kwargs,
content_type=content_type, body=body)
try:
+ kwargs['token'] = self._get_token()
body = callback(body, **kwargs)
except ick2.ExistsAlready as e:
ick2.log.log('error', msg_text=str(e), kwargs=kwargs)
@@ -94,6 +114,7 @@ class APIbase:
ick2.log.log(
'trace', msg_text='PUT called', kwargs=kwargs,
content_type=content_type, body=body)
+ kwargs['token'] = self._get_token()
if 'raw_uri_path' in kwargs:
del kwargs['raw_uri_path']
try:
@@ -112,6 +133,7 @@ class APIbase:
'trace', msg_text='DELETE called', kwargs=kwargs,
content_type=content_type, body=body)
try:
+ kwargs['token'] = self._get_token()
if 'raw_uri_path' in kwargs:
del kwargs['raw_uri_path']
body = callback(**kwargs)
@@ -122,19 +144,19 @@ class APIbase:
return ick2.OK(body)
return wrapper
- def create(self, body, **kwargs):
+ def create(self, body, token=None, **kwargs):
raise NotImplementedError()
- def update(self, body, name, **kwargs):
+ def update(self, body, name, token=None, **kwargs):
raise NotImplementedError()
- def delete(self, name, **kwargs):
+ def delete(self, name, token=None, **kwargs):
raise NotImplementedError()
- def list(self, **kwargs):
+ def list(self, token=None, **kwargs):
raise NotImplementedError()
- def show(self, name, **kwargs):
+ def show(self, name, token=None, **kwargs):
raise NotImplementedError()
@@ -144,26 +166,27 @@ class ResourceApiBase(APIbase):
super().__init__(state)
self._type_name = type_name
- def list(self, **kwargs):
- resources = self._trans.get_resources(self._type_name)
+ def list(self, token=None, **kwargs):
+ resources = self._trans.get_resources(token, self._type_name)
return {
self._type_name: [r.as_dict() for r in resources]
}
- def show(self, name, **kwargs):
- return self._trans.get_resource(self._type_name, name).as_dict()
+ def show(self, name, token=None, **kwargs):
+ return self._trans.get_resource(token, self._type_name, name).as_dict()
- def create(self, body, **kwargs):
+ def create(self, body, token=None, **kwargs):
ick2.log.log(
'trace', msg_text='create resource',
- resource_type=self._type_name, body=body, kwargs=kwargs)
+ resource_type=self._type_name,
+ body=body, token=token, kwargs=kwargs)
as_dict = self.mangle_new_resource(body)
rid = self.get_resource_name(as_dict)
- if self._trans.has_resource(self._type_name, rid):
+ if self._trans.has_resource(token, self._type_name, rid):
raise ick2.ExistsAlready(rid)
- with self._trans.new(self._type_name, rid) as resource:
+ with self._trans.new(token, self._type_name, rid) as resource:
resource.from_dict(as_dict)
return as_dict
@@ -174,12 +197,12 @@ class ResourceApiBase(APIbase):
def get_resource_name(self, resource): # pragma: no cover
raise NotImplementedError()
- def update(self, body, name, **kwargs):
+ def update(self, body, name, token=None, **kwargs):
rid = self.get_resource_name(body)
- if not self._trans.has_resource(self._type_name, rid):
+ if not self._trans.has_resource(token, self._type_name, rid):
raise ick2.NotFound(kind=self._type_name, rid=rid)
- with self._trans.modify(self._type_name, rid) as resource:
+ with self._trans.modify(token, self._type_name, rid) as resource:
as_dict = self.mangle_updated_resource(resource.as_dict(), body)
resource.from_dict(as_dict)
@@ -188,7 +211,7 @@ class ResourceApiBase(APIbase):
def mangle_updated_resource(self, old, new): # pragma: no cover
return new
- def delete(self, name, **kwargs):
- if not self._trans.has_resource(self._type_name, name):
+ def delete(self, name, token=None, **kwargs):
+ if not self._trans.has_resource(token, self._type_name, name):
raise ick2.NotFound(kind=self._type_name, rid=name)
- self._trans.remove_resource(self._type_name, name)
+ self._trans.remove_resource(token, self._type_name, name)