diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-10-12 11:06:53 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-10-12 11:06:53 +0300 |
commit | 38cf6347845dba9eb1613d930bfd00e09da58fc6 (patch) | |
tree | 1a1e7441a0717f9b4c7435ccd91e05f01133f537 | |
parent | 39bb5c5eb50ae87fce3c7d7395e908d0f43d4f73 (diff) | |
download | qvisqve-38cf6347845dba9eb1613d930bfd00e09da58fc6.tar.gz |
Refactor: add qvarn.Router interface, and qvarn.VersionRouter
My goal it so to split up the large and ugly QvarnAPI class into more
manageable classes.
-rw-r--r-- | qvarn/__init__.py | 3 | ||||
-rw-r--r-- | qvarn/api.py | 25 | ||||
-rw-r--r-- | qvarn/api_tests.py | 11 | ||||
-rw-r--r-- | qvarn/router.py | 23 | ||||
-rw-r--r-- | qvarn/version_router.py | 42 | ||||
-rw-r--r-- | without-tests | 2 |
6 files changed, 72 insertions, 34 deletions
diff --git a/qvarn/__init__.py b/qvarn/__init__.py index fffff3a..f270cba 100644 --- a/qvarn/__init__.py +++ b/qvarn/__init__.py @@ -95,4 +95,7 @@ from .responses import ( unknown_search_field_response, ) +from .router import Router +from .version_router import VersionRouter + from .api import QvarnAPI, NoSuchResourceType diff --git a/qvarn/api.py b/qvarn/api.py index 968a7bc..7dfd20c 100644 --- a/qvarn/api.py +++ b/qvarn/api.py @@ -184,7 +184,8 @@ class QvarnAPI: if path == '/version': qvarn.log.log('info', msg_text='Add /version route') - return self.version_route() + v = qvarn.VersionRouter() + return v.get_routes() try: rt = self.get_resource_type(path) @@ -203,28 +204,6 @@ class QvarnAPI: qvarn.log.log('info', msg_text='Add routes', routes=loggable_routes) return routes - def version_route(self): - return [ - { - 'method': 'GET', - 'path': '/version', - 'callback': self.version, - 'needs-authorization': False, - }, - ] - - def version(self, content_type, body, **kwargs): - version = { - 'api': { - 'version': qvarn.__version__, - }, - 'implementation': { - 'name': 'Qvarn', - 'version': qvarn.__version__, - }, - } - return qvarn.ok_response(version) - def resource_routes(self, path, rt): # pragma: no cover coll = qvarn.CollectionAPI() coll.set_object_store(self._store) diff --git a/qvarn/api_tests.py b/qvarn/api_tests.py index 29c0e6c..0669b4c 100644 --- a/qvarn/api_tests.py +++ b/qvarn/api_tests.py @@ -26,17 +26,6 @@ class QvarnAPITests(unittest.TestCase): api = qvarn.QvarnAPI() self.assertNotEqual(api.find_missing_route('/version'), []) - def test_version_returns_sensible_data(self): - api = qvarn.QvarnAPI() - r = api.version(None, None) - v = r['body'] - self.assertTrue(isinstance(v, dict)) - self.assertTrue('api' in v) - self.assertTrue('version' in v['api']) - self.assertTrue('implementation' in v) - self.assertTrue('name' in v['implementation']) - self.assertTrue('version' in v['implementation']) - def test_returns_no_routes_for_unknown_resource_type(self): store = qvarn.MemoryObjectStore() api = qvarn.QvarnAPI() diff --git a/qvarn/router.py b/qvarn/router.py new file mode 100644 index 0000000..9f171b0 --- /dev/null +++ b/qvarn/router.py @@ -0,0 +1,23 @@ +# Copyright (C) 2017 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 (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + + +class Router: + + def __init__(self): + pass + + def get_routes(self): + raise NotImplementedError() diff --git a/qvarn/version_router.py b/qvarn/version_router.py new file mode 100644 index 0000000..b9f1056 --- /dev/null +++ b/qvarn/version_router.py @@ -0,0 +1,42 @@ +# Copyright (C) 2017 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 (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + + +import qvarn + + +class VersionRouter(qvarn.Router): + + def get_routes(self): + return [ + { + 'method': 'GET', + 'path': '/version', + 'callback': self._version, + 'needs-authorization': False, + }, + ] + + def _version(self, *args, **kwargs): + version = { + 'api': { + 'version': qvarn.__version__, + }, + 'implementation': { + 'name': 'Qvarn', + 'version': qvarn.__version__, + }, + } + return qvarn.ok_response(version) diff --git a/without-tests b/without-tests index 09bf740..a1ae586 100644 --- a/without-tests +++ b/without-tests @@ -3,6 +3,8 @@ qvarn/__init__.py qvarn/backend.py qvarn/logging.py qvarn/responses.py +qvarn/router.py qvarn/sql.py qvarn/version.py +qvarn/version_router.py yarns/lib.py |