Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved #986 - implemented router shortcuts #989

Merged
merged 3 commits into from
Jul 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
25 changes: 25 additions & 0 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
50 changes: 50 additions & 0 deletions tests/test_urldispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,56 @@ 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_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,
Expand Down