diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-04-22 18:02:51 +0200 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-04-22 18:02:51 +0200 |
commit | decd9a8b86410386783df7c9faa8b4d90febe09b (patch) | |
tree | cabb8070ffca1c8fa6aa6b11bf7b733fb1492872 | |
parent | 94baa0b3c43954204dd6c2049b36fb7523e6c844 (diff) |
Retrieve objects from an URL
-rw-r--r-- | pyapd/__init__.py | 0 | ||||
-rw-r--r-- | pyapd/app.py | 20 | ||||
-rw-r--r-- | pyapd/stores/__init__.py | 0 | ||||
-rw-r--r-- | tests/data/test_config.yaml | 2 | ||||
-rw-r--r-- | tests/test_app.py | 17 | ||||
-rw-r--r-- | tests/test_config.py | 2 |
6 files changed, 37 insertions, 4 deletions
diff --git a/pyapd/__init__.py b/pyapd/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/pyapd/__init__.py diff --git a/pyapd/app.py b/pyapd/app.py index 0eaa80f..311e4f5 100644 --- a/pyapd/app.py +++ b/pyapd/app.py @@ -1,4 +1,7 @@ import tornado.web +from tornado.routing import URLSpec + +from . import stores class RootHandler(tornado.web.RequestHandler): @@ -6,10 +9,27 @@ class RootHandler(tornado.web.RequestHandler): self.write("Hello World") +class ObjectHandler(tornado.web.RequestHandler): + def get(self, oid): + oid = self.request.full_url() + try: + obj = self.application.store.get('object', oid) + except ( + stores.exceptions.UnknownObjectType, + stores.exceptions.DoesNotExist, + ): + raise tornado.web.HTTPError( + status_code=404, + ) + self.write(obj.to_jsons()) + + class App(tornado.web.Application): def __init__(self, config, *args, **kw): self.config = config + self.store = getattr(stores, self.config.backend).Store() urls = [ (r'/', RootHandler), + URLSpec(r'/object/(.*)', handler=ObjectHandler), ] super().__init__(urls, *args, **kw) diff --git a/pyapd/stores/__init__.py b/pyapd/stores/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/pyapd/stores/__init__.py diff --git a/tests/data/test_config.yaml b/tests/data/test_config.yaml index fa44e32..4a485ff 100644 --- a/tests/data/test_config.yaml +++ b/tests/data/test_config.yaml @@ -1 +1 @@ -backend: none +backend: memory diff --git a/tests/test_app.py b/tests/test_app.py index 1ff504c..f78149b 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,12 +1,25 @@ from tornado.testing import AsyncHTTPTestCase -from pyapd import app, config +from pyapd import app, config, objects class TestApp(AsyncHTTPTestCase): def get_app(self, *args, **kw): - return app.App(config.Config('tests/data/test_config.yaml')) + if not getattr(self, 'app', None): + self.app = app.App(config.Config('tests/data/test_config.yaml')) + return self.app def test_root(self): res = self.fetch('/') self.assertEqual(res.code, 200) + + def test_object_view(self): + oid = self.get_url('/object/12345') + obj = objects.Object(id=oid) + self.app.store.add(obj) + res = self.fetch('/object/12345') + self.assertEqual(res.code, 200) + + def test_object_view_non_existing(self): + res = self.fetch('/object/does_not_exist') + self.assertEqual(res.code, 404) diff --git a/tests/test_config.py b/tests/test_config.py index 7851019..6002dd9 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -19,4 +19,4 @@ class TestConfig(unittest.TestCase): 'tests/data/test_config.yaml' ) self.assertIn('backend', self.config.data) - self.assertEqual(self.config.backend, 'none') + self.assertEqual(self.config.backend, 'memory') |