summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2017-10-12 13:45:49 +0300
committerLars Wirzenius <liw@liw.fi>2017-10-12 13:45:49 +0300
commitb9f388080e4381c0e8c1c1efcd6c319435a1d685 (patch)
tree860374d986078999e76cdd3a4550e11de19204de
parentf37250e82f8ca8302d7459cb426bf249ae925ef2 (diff)
downloadqvisqve-b9f388080e4381c0e8c1c1efcd6c319435a1d685.tar.gz
Refactor: use NotificationRouter
-rw-r--r--qvarn/__init__.py1
-rw-r--r--qvarn/api.py216
-rw-r--r--without-tests1
3 files changed, 8 insertions, 210 deletions
diff --git a/qvarn/__init__.py b/qvarn/__init__.py
index 49fb32a..f2f0efb 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 .notification_router import NotificationRouter
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 6ee69d7..221b693 100644
--- a/qvarn/api.py
+++ b/qvarn/api.py
@@ -260,216 +260,12 @@ class QvarnAPI:
return routes + self._get_notification_routes(coll, path, id_path)
def _get_notification_routes(self, coll, path, id_path):
- rt = self.get_listener_resource_type()
- listeners = qvarn.CollectionAPI()
- listeners.set_object_store(self._store)
- listeners.set_resource_type(rt)
-
- return [
- {
- 'method': 'POST',
- 'path': path + '/listeners',
- 'callback': self.get_post_listener_callback(coll, listeners),
- },
- {
- 'method': 'GET',
- 'path': path + '/listeners',
- 'callback': self.get_listener_list_callback(listeners),
- },
- {
- 'method': 'GET',
- 'path': path + '/listeners/<id>',
- 'callback': self.get_listener_callback(coll, listeners),
- },
- {
- 'method': 'PUT',
- 'path': path + '/listeners/<id>',
- 'callback': self.put_listener_callback(listeners),
- },
- {
- 'method': 'DELETE',
- 'path': path + '/listeners/<id>',
- 'callback': self.delete_listener_callback(listeners),
- },
- {
- 'method': 'GET',
- 'path': path + '/listeners/<id>/notifications',
- 'callback': self.get_notifications_list_callback(),
- },
- {
- 'method': 'GET',
- 'path': path + '/listeners/<listener_id>/notifications/<id>',
- 'callback': self.get_notification_callback(),
- },
- {
- 'method': 'DELETE',
- 'path': path + '/listeners/<listener_id>/notifications/<id>',
- 'callback': self.delete_notification_callback(),
- },
- ]
-
- def get_post_listener_callback(self, coll, listeners): # pragma: no cover
- def wrapper(content_type, body, **kwargs):
- if content_type != 'application/json':
- raise qvarn.NotJson(content_type)
-
- rt = listeners.get_type()
- try:
- self._validator.validate_against_prototype(
- rt.get_type(), body, rt.get_latest_prototype())
- except qvarn.ValidationError as e:
- qvarn.log.log('error', msg_text=str(e), body=body)
- return qvarn.bad_request_response(str(e))
-
- if 'type' not in body:
- body['type'] = 'listener'
-
- result_body = listeners.post(body)
- qvarn.log.log(
- 'debug', msg_text='POST a new listener, result',
- body=result_body)
- location = '{}{}/listeners/{}'.format(
- self._baseurl, coll.get_type().get_path(),
- result_body['id'])
- return qvarn.created_response(result_body, location)
- return wrapper
-
- def get_listener_list_callback(self, listeners): # pragma: no cover
- def wrapper(content_type, body, **kwargs):
- body = listeners.list()
- return qvarn.ok_response(body)
- return wrapper
-
- def get_listener_callback(self, coll, listeners): # pragma: no cover
- def wrapper(content_type, body, **kwargs):
- try:
- obj = listeners.get(kwargs['id'])
- except qvarn.NoSuchResource as e:
- return qvarn.no_such_resource_response(str(e))
- return qvarn.ok_response(obj)
- return wrapper
-
- def put_listener_callback(self, listeners): # pragma: no cover
- def wrapper(content_type, body, **kwargs):
- if content_type != 'application/json':
- raise qvarn.NotJson(content_type)
-
- if 'type' not in body:
- body['type'] = 'listener'
-
- listener_id = kwargs['id']
-
- if 'id' not in body:
- body['id'] = listener_id
-
- try:
- self._validator.validate_resource_update(
- body, listeners.get_type())
- 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 = listeners.put(body)
- except qvarn.WrongRevision as e:
- return qvarn.conflict_response(str(e))
- except qvarn.NoSuchResource as e:
- # We intentionally say bad request, instead of not found.
- # This is to be compatible with old Qvarn. This may get
- # changed later.
- return qvarn.bad_request_response(str(e))
-
- return qvarn.ok_response(result_body)
- return wrapper
-
- def get_notifications_list_callback(self): # pragma: no cover
- def timestamp(pair):
- _, obj = pair
- return obj['timestamp']
-
- def wrapper(content_type, body, **kwargs):
- rid = kwargs['id']
- cond = qvarn.All(
- qvarn.Equal('type', 'notification'),
- qvarn.Equal('listener_id', rid)
- )
- pairs = self._store.find_objects(cond)
- ordered = sorted(pairs, key=timestamp)
- qvarn.log.log(
- 'trace', msg_text='Found notifications',
- notifications=ordered)
- body = {
- 'resources': [
- {
- 'id': keys['obj_id']
- }
- for keys, _ in ordered
- ]
- }
- return qvarn.ok_response(body)
- return wrapper
-
- def get_notification_callback(self): # pragma: no cover
- def wrapper(content_type, body, **kwargs):
- listener_id = kwargs['listener_id']
- notification_id = kwargs['id']
- cond = qvarn.All(
- qvarn.Equal('type', 'notification'),
- qvarn.Equal('listener_id', listener_id),
- qvarn.Equal('id', notification_id),
- )
- pairs = self._store.find_objects(cond)
- qvarn.log.log(
- 'trace', msg_text='Found notifications',
- notifications=pairs)
- if len(pairs) == 0:
- return qvarn.no_such_resource_response(notification_id)
- if len(pairs) > 1:
- raise qvarn.TooManyResources(notification_id)
- return qvarn.ok_response(pairs[0][1])
- return wrapper
-
- def delete_listener_callback(self, listeners): # pragma: no cover
- def wrapper(content_type, body, **kwargs):
- listener_id = kwargs['id']
- listeners.delete(listener_id)
- for obj_id in self.find_notifications(listener_id):
- self._store.remove_objects(obj_id=obj_id)
- return qvarn.ok_response({})
- return wrapper
-
- def delete_notification_callback(self): # pragma: no cover
- def wrapper(content_type, body, **kwargs):
- listener_id = kwargs['listener_id']
- notification_id = kwargs['id']
- cond = qvarn.All(
- qvarn.Equal('type', 'notification'),
- qvarn.Equal('listener_id', listener_id),
- qvarn.Equal('id', notification_id),
- )
- for keys, _ in self._store.find_objects(cond):
- values = {
- key: keys[key]
- for key in keys
- if isinstance(keys[key], str)
- }
- self._store.remove_objects(**values)
- return qvarn.ok_response({})
- return wrapper
-
- def find_notifications(self, listener_id): # pragma: no cover
- cond = qvarn.All(
- qvarn.Equal('type', 'notification'),
- qvarn.Equal('listener_id', listener_id),
- )
- obj_ids = [
- keys['obj_id']
- for keys, _ in self._store.find_objects(cond)
- ]
- qvarn.log.log(
- 'trace', msg_text='Found notifications',
- notifications=obj_ids)
- return obj_ids
+ listener_rt = self.get_listener_resource_type()
+ notif_router = qvarn.NotificationRouter()
+ notif_router.set_baseurl(self._baseurl)
+ notif_router.set_parent_collection(coll)
+ notif_router.set_object_store(self._store, listener_rt)
+ return notif_router.get_routes()
def notify(self, rid, rrev, change): # pragma: no cover
rt = self.get_notification_resource_type()
diff --git a/without-tests b/without-tests
index 578e2d5..09d1efc 100644
--- a/without-tests
+++ b/without-tests
@@ -4,6 +4,7 @@ qvarn/api_errors.py
qvarn/backend.py
qvarn/file_router.py
qvarn/logging.py
+qvarn/notification_router.py
qvarn/responses.py
qvarn/router.py
qvarn/sql.py