diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-10-12 12:36:59 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-10-12 12:36:59 +0300 |
commit | f37250e82f8ca8302d7459cb426bf249ae925ef2 (patch) | |
tree | c1b33bb17125f0f190862d17647910ed1740058c | |
parent | 2849ecf2d640810a1a4b368346519cc39d93424b (diff) | |
download | qvisqve-f37250e82f8ca8302d7459cb426bf249ae925ef2.tar.gz |
Refactor: use SubresrouceRouter
-rw-r--r-- | qvarn/__init__.py | 1 | ||||
-rw-r--r-- | qvarn/api.py | 57 |
2 files changed, 5 insertions, 53 deletions
diff --git a/qvarn/__init__.py b/qvarn/__init__.py index ceeb5f3..49fb32a 100644 --- a/qvarn/__init__.py +++ b/qvarn/__init__.py @@ -105,6 +105,7 @@ from .api_errors import ( from .router import Router from .file_router import FileRouter +from .subresource_router import SubresourceRouter from .version_router import VersionRouter from .timestamp import get_current_timestamp diff --git a/qvarn/api.py b/qvarn/api.py index e88417a..6ee69d7 100644 --- a/qvarn/api.py +++ b/qvarn/api.py @@ -245,7 +245,10 @@ class QvarnAPI: files = rt.get_files() for subpath in rt.get_subpaths(): if subpath not in files: - more = self.get_subresource_routes(id_path, coll, subpath) + sub_router = qvarn.SubresourceRouter() + sub_router.set_subpath(subpath) + sub_router.set_parent_collection(coll) + more = sub_router.get_routes() else: file_router = qvarn.FileRouter() file_router.set_subpath(subpath) @@ -256,21 +259,6 @@ class QvarnAPI: return routes + self._get_notification_routes(coll, path, id_path) - def get_subresource_routes( - self, id_path, coll, subpath): # pragma: no cover - return [ - { - 'method': 'GET', - 'path': '{}/{}'.format(id_path, subpath), - 'callback': self.get_subpath_callback(coll, subpath), - }, - { - 'method': 'PUT', - 'path': '{}/{}'.format(id_path, subpath), - 'callback': self.put_subpath_callback(coll, subpath), - }, - ] - def _get_notification_routes(self, coll, path, id_path): rt = self.get_listener_resource_type() listeners = qvarn.CollectionAPI() @@ -578,34 +566,6 @@ class QvarnAPI: return qvarn.ok_response(result_body) return wrapper - def put_subpath_callback(self, coll, subpath): # pragma: no cover - def wrapper(content_type, body, **kwargs): - if content_type != 'application/json': - raise qvarn.NotJson(content_type) - - obj_id = kwargs['id'] - if 'revision' not in body: - return qvarn.bad_request_response('must have revision') - revision = body.pop('revision') - - rt = coll.get_type() - try: - self._validator.validate_subresource(subpath, rt, body) - except qvarn.ValidationError as e: - qvarn.log.log('error', msg_text=str(e), body=body) - return qvarn.bad_request_response(str(e)) - - try: - result_body = coll.put_subresource( - body, subpath=subpath, obj_id=obj_id, revision=revision) - except qvarn.WrongRevision as e: - return qvarn.conflict_response(str(e)) - except qvarn.NoSuchResource as e: - return qvarn.no_such_resource_response(str(e)) - - return qvarn.ok_response(result_body) - return wrapper - def get_resource_callback(self, coll): # pragma: no cover def wrapper(content_type, body, **kwargs): try: @@ -615,15 +575,6 @@ class QvarnAPI: return qvarn.ok_response(obj) return wrapper - def get_subpath_callback(self, coll, subpath): # pragma: no cover - def wrapper(content_type, body, **kwargs): - try: - obj = coll.get_subresource(kwargs['id'], subpath) - except qvarn.NoSuchResource as e: - return qvarn.no_such_resource_response(str(e)) - return qvarn.ok_response(obj) - return wrapper - def get_resource_list_callback(self, coll): # pragma: no cover def wrapper(content_type, body, **kwargs): body = coll.list() |