Skip to content

Commit

Permalink
Add app.add_routes() method (#2787)
Browse files Browse the repository at this point in the history
* Add app.add_routes() method

* Add changenote

* Add a test
  • Loading branch information
asvetlov authored Mar 3, 2018
1 parent 297e952 commit d6c0714
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGES/2787.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement ``app.add_routes()`` method.
3 changes: 3 additions & 0 deletions aiohttp/web_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ def add_subapp(self, prefix, subapp):
subapp._set_loop(self._loop)
return resource

def add_routes(self, routes):
self.router.add_routes(routes)

@property
def on_response_prepare(self):
return self._on_response_prepare
Expand Down
44 changes: 29 additions & 15 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1218,8 +1218,9 @@ duplicated like one using :meth:`Application.copy`.
:param handler_args: dict-like object that overrides keyword arguments of
:meth:`Application.make_handler`

:param client_max_size: client's maximum size in a request, in bytes. If a POST
request exceeds this value, it raises an
:param client_max_size: client's maximum size in a request, in
bytes. If a POST request exceeds this
value, it raises an
`HTTPRequestEntityTooLarge` exception.

:param loop: event loop
Expand Down Expand Up @@ -1309,6 +1310,32 @@ duplicated like one using :meth:`Application.copy`.

.. seealso:: :ref:`aiohttp-web-graceful-shutdown` and :attr:`on_shutdown`.

.. method:: add_subapp(prefix, subapp)

Register nested sub-application under given path *prefix*.

In resolving process if request's path starts with *prefix* then
further resolving is passed to *subapp*.

:param str prefix: path's prefix for the resource.

:param Application subapp: nested application attached under *prefix*.

:returns: a :class:`PrefixedSubAppResource` instance.

.. method:: add_routes(routes_table)

Register route definitions from *routes_table*.

The table is a :class:`list` of :class:`RouteDef` items or
:class:`RouteTableDef`.

The method is a shortcut for
``app.router.add_routes(routes_table)``, see also
:meth:`UrlDispatcher.add_routes`.

.. versionadded:: 3.1

.. method:: make_handler(loop=None, **kwargs)

Creates HTTP protocol factory for handling requests.
Expand Down Expand Up @@ -1619,19 +1646,6 @@ Router is any object that implements :class:`AbstractRouter` interface.

:returns: new :class:`StaticRoute` instance.

.. method:: add_subapp(prefix, subapp)

Register nested sub-application under given path *prefix*.

In resolving process if request's path starts with *prefix* then
further resolving is passed to *subapp*.

:param str prefix: path's prefix for the resource.

:param Application subapp: nested application attached under *prefix*.

:returns: a :class:`PrefixedSubAppResource` instance.

.. comethod:: resolve(request)

A :ref:`coroutine<coroutine>` that returns
Expand Down
13 changes: 13 additions & 0 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1708,3 +1708,16 @@ async def handler(request):
assert 200 == resp.status
txt = await resp.text()
assert 'OK' == txt


async def test_app_add_routes(aiohttp_client):

async def handler(request):
return web.Response()

app = web.Application()
app.add_routes([web.get('/get', handler)])

client = await aiohttp_client(app)
resp = await client.get('/get')
assert resp.status == 200

0 comments on commit d6c0714

Please sign in to comment.