From c1b57e15149ebcf3cba39165bea153f2d34cfea8 Mon Sep 17 00:00:00 2001 From: Alexandru Mihai <92.alexandru.mihai@gmail.com> Date: Sat, 23 Jul 2016 13:03:51 +0200 Subject: [PATCH 1/3] Resolved #986 - implemented router shortcuts --- aiohttp/web_urldispatcher.py | 30 ++++++++++++++++++++++++++++++ tests/test_urldispatch.py | 10 ++++++++++ 2 files changed, 40 insertions(+) diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index 600b1948c99..02ef30134e1 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -738,3 +738,33 @@ def add_static(self, prefix, path, *, name=None, expect_handler=None, response_factory=response_factory) self.register_route(route) return route + + def add_get(self, *args, **kwargs): + """ + Shortcut for add_route with method GET + """ + return self.add_route('GET', *args, **kwargs) + + def add_post(self, *args, **kwargs): + """ + Shortcut for add_route with method POST + """ + return self.add_route('POST', *args, **kwargs) + + def add_put(self, *args, **kwargs): + """ + Shortcut for add_route with method PUT + """ + return self.add_route('PUT', *args, **kwargs) + + def add_patch(self, *args, **kwargs): + """ + Shortcut for add_route with method PATCH + """ + return self.add_route('PATCH', *args, **kwargs) + + def add_delete(self, *args, **kwargs): + """ + Shortcut for add_route with method DELETE + """ + return self.add_route('DELETE', *args, **kwargs) diff --git a/tests/test_urldispatch.py b/tests/test_urldispatch.py index 4d34af6ae27..a5aadaf4276 100644 --- a/tests/test_urldispatch.py +++ b/tests/test_urldispatch.py @@ -89,6 +89,16 @@ def test_add_with_matchdict(self): self.assertIs(handler, info.handler) self.assertIsNone(info.route.name) + def test_add_route_with_add_get_shortcut(self): + handler = self.make_handler() + self.router.add_get('/handler/to/path', handler) + req = self.make_request('GET', '/handler/to/path') + info = self.loop.run_until_complete(self.router.resolve(req)) + self.assertIsNotNone(info) + self.assertEqual(0, len(info)) + self.assertIs(handler, info.handler) + self.assertIsNone(info.route.name) + def test_add_with_name(self): handler = self.make_handler() self.router.add_route('GET', '/handler/to/path', handler, From a76e6b7d0a0bcf367c4aa125c3b0f7db5fddd9e6 Mon Sep 17 00:00:00 2001 From: Alexandru Mihai <92.alexandru.mihai@gmail.com> Date: Sat, 23 Jul 2016 16:16:53 +0200 Subject: [PATCH 2/3] Added more unittests for all shortcuts --- tests/test_urldispatch.py | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/test_urldispatch.py b/tests/test_urldispatch.py index a5aadaf4276..9e3a887fa1a 100644 --- a/tests/test_urldispatch.py +++ b/tests/test_urldispatch.py @@ -99,6 +99,46 @@ def test_add_route_with_add_get_shortcut(self): self.assertIs(handler, info.handler) self.assertIsNone(info.route.name) + def test_add_route_with_add_post_shortcut(self): + handler = self.make_handler() + self.router.add_post('/handler/to/path', handler) + req = self.make_request('POST', '/handler/to/path') + info = self.loop.run_until_complete(self.router.resolve(req)) + self.assertIsNotNone(info) + self.assertEqual(0, len(info)) + self.assertIs(handler, info.handler) + self.assertIsNone(info.route.name) + + def test_add_route_with_add_put_shortcut(self): + handler = self.make_handler() + self.router.add_put('/handler/to/path', handler) + req = self.make_request('PUT', '/handler/to/path') + info = self.loop.run_until_complete(self.router.resolve(req)) + self.assertIsNotNone(info) + self.assertEqual(0, len(info)) + self.assertIs(handler, info.handler) + self.assertIsNone(info.route.name) + + def test_add_route_with_add_patch_shortcut(self): + handler = self.make_handler() + self.router.add_patch('/handler/to/path', handler) + req = self.make_request('PATCH', '/handler/to/path') + info = self.loop.run_until_complete(self.router.resolve(req)) + self.assertIsNotNone(info) + self.assertEqual(0, len(info)) + self.assertIs(handler, info.handler) + self.assertIsNone(info.route.name) + + def test_add_route_with_add_delete_shortcut(self): + handler = self.make_handler() + self.router.add_delete('/handler/to/path', handler) + req = self.make_request('DELETE', '/handler/to/path') + info = self.loop.run_until_complete(self.router.resolve(req)) + self.assertIsNotNone(info) + self.assertEqual(0, len(info)) + self.assertIs(handler, info.handler) + self.assertIsNone(info.route.name) + def test_add_with_name(self): handler = self.make_handler() self.router.add_route('GET', '/handler/to/path', handler, From 29df274442355e64e991892d2f2b7a65a19054d3 Mon Sep 17 00:00:00 2001 From: Alexandru Mihai <92.alexandru.mihai@gmail.com> Date: Sat, 23 Jul 2016 16:59:59 +0200 Subject: [PATCH 3/3] Added documentation for shortcut methods on router --- docs/web_reference.rst | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/web_reference.rst b/docs/web_reference.rst index 79368a98f73..25a4d8bb9bd 100644 --- a/docs/web_reference.rst +++ b/docs/web_reference.rst @@ -1202,6 +1202,31 @@ Router is any object that implements :class:`AbstractRouter` interface. :returns: new :class:`PlainRoute` or :class:`DynamicRoute` instance. + .. method:: add_get(path, *args, **kwargs) + + Shortcut for adding a GET handler. Calls the :meth:`add_route` with \ + ``method`` equals to ``'GET'``. + + .. method:: add_post(path, *args, **kwargs) + + Shortcut for adding a POST handler. Calls the :meth:`add_route` with \ + ``method`` equals to ``'POST'``. + + .. method:: add_put(path, *args, **kwargs) + + Shortcut for adding a PUT handler. Calls the :meth:`add_route` with \ + ``method`` equals to ``'PUT'``. + + .. method:: add_patch(path, *args, **kwargs) + + Shortcut for adding a PATCH handler. Calls the :meth:`add_route` with \ + ``method`` equals to ``'PATCH'``. + + .. method:: add_delete(path, *args, **kwargs) + + Shortcut for adding a DELETE handler. Calls the :meth:`add_route` with \ + ``method`` equals to ``'DELETE'``. + .. method:: add_static(prefix, path, *, name=None, expect_handler=None, \ chunk_size=256*1024, response_factory=StreamResponse)