diff options
Diffstat (limited to 'ick2/apibase.py')
-rw-r--r-- | ick2/apibase.py | 73 |
1 files changed, 26 insertions, 47 deletions
diff --git a/ick2/apibase.py b/ick2/apibase.py index 0537b6b..c08f7cf 100644 --- a/ick2/apibase.py +++ b/ick2/apibase.py @@ -1,4 +1,4 @@ -# Copyright (C) 2017-2019 Lars Wirzenius +# Copyright (C) 2017-2018 Lars Wirzenius # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or @@ -16,20 +16,11 @@ import ick2 -import bottle - - class APIbase: def __init__(self, state): - assert (state is None or - isinstance(state, ick2.MemoryStore) or - isinstance(state, ick2.MuckStore)) + assert state is None or isinstance(state, ick2.FilePersistentState) self._trans = ick2.TransactionalState(state) - self._token_getter = None - - def set_token_getter(self, getter): # pragma: no cover - self._token_getter = getter def get_routes(self, path): resource_path = '{}/<name:re:[^/+][^/]*?(/[^/+][^/]*?)*>'.format(path) @@ -61,26 +52,12 @@ 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) @@ -105,7 +82,6 @@ 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) @@ -118,7 +94,6 @@ 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: @@ -137,7 +112,6 @@ 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) @@ -148,19 +122,19 @@ class APIbase: return ick2.OK(body) return wrapper - def create(self, body, token=None, **kwargs): + def create(self, body, **kwargs): raise NotImplementedError() - def update(self, body, name, token=None, **kwargs): + def update(self, body, name, **kwargs): raise NotImplementedError() - def delete(self, name, token=None, **kwargs): + def delete(self, name, **kwargs): raise NotImplementedError() - def list(self, token=None, **kwargs): + def list(self, **kwargs): raise NotImplementedError() - def show(self, name, token=None, **kwargs): + def show(self, name, **kwargs): raise NotImplementedError() @@ -170,25 +144,26 @@ class ResourceApiBase(APIbase): super().__init__(state) self._type_name = type_name - def list(self, token=None, **kwargs): - resources = self._trans.get_resources(token, self._type_name) + def list(self, **kwargs): + resources = self._trans.get_resources(self._type_name) return { self._type_name: [r.as_dict() for r in resources] } - def show(self, name, token=None, **kwargs): - return self._trans.get_resource(token, self._type_name, name).as_dict() + def show(self, name, **kwargs): + return self._trans.get_resource(self._type_name, name).as_dict() - def create(self, body, token=None, **kwargs): + def create(self, body, **kwargs): ick2.log.log( 'trace', msg_text='create resource', - resource_type=self._type_name, - body=body, token=token, kwargs=kwargs) + resource_type=self._type_name, body=body, kwargs=kwargs) as_dict = self.mangle_new_resource(body) - name = self.get_resource_name(as_dict) + rid = self.get_resource_name(as_dict) + if self._trans.has_resource(self._type_name, rid): + raise ick2.ExistsAlready(rid) - with self._trans.new(token, self._type_name, name) as resource: + with self._trans.new(self._type_name, rid) as resource: resource.from_dict(as_dict) return as_dict @@ -199,10 +174,12 @@ class ResourceApiBase(APIbase): def get_resource_name(self, resource): # pragma: no cover raise NotImplementedError() - def update(self, body, name, token=None, **kwargs): - name = self.get_resource_name(body) + def update(self, body, name, **kwargs): + rid = self.get_resource_name(body) + if not self._trans.has_resource(self._type_name, rid): + raise ick2.NotFound(kind=self._type_name, rid=rid) - with self._trans.modify(token, self._type_name, name) as resource: + with self._trans.modify(self._type_name, rid) as resource: as_dict = self.mangle_updated_resource(resource.as_dict(), body) resource.from_dict(as_dict) @@ -211,5 +188,7 @@ class ResourceApiBase(APIbase): def mangle_updated_resource(self, old, new): # pragma: no cover return new - def delete(self, name, token=None, **kwargs): - self._trans.remove_resource(token, self._type_name, name) + def delete(self, name, **kwargs): + if not self._trans.has_resource(self._type_name, name): + raise ick2.NotFound(kind=self._type_name, rid=name) + self._trans.remove_resource(self._type_name, name) |