Skip to content

Commit 5501ff4

Browse files
committed
Work on web documentation
1 parent 65e791b commit 5501ff4

File tree

1 file changed

+127
-31
lines changed

1 file changed

+127
-31
lines changed

docs/web.rst

Lines changed: 127 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ Handlers can be first-class functions, e.g.::
8989

9090
app.router.add_route('GET', '/', hello)
9191

92-
Sometimes you would like to group logically coupled handlers into a python class.
92+
Sometimes you would like to group logically coupled handlers into a
93+
python class.
9394

9495
:mod:`aiohttp.web` doesn't dictate any implementation details,
9596
so application developer can use classes if he wants::
@@ -398,9 +399,10 @@ Response classes
398399
For now, :mod:`aiohttp.web` has two classes for the *HTTP response*:
399400
:class:`StreamResponse` and :class:`Response`.
400401

401-
Usually you need to use the second one. :class:`StreamResponse`
402-
is intended for streaming data, while :class:`Response` contains *HTTP BODY* as an attribute and sends own content as single piece with the correct
403-
*Content-Length HTTP header*.
402+
Usually you need to use the second one. :class:`StreamResponse` is
403+
intended for streaming data, while :class:`Response` contains *HTTP
404+
BODY* as an attribute and sends own content as single piece with the
405+
correct *Content-Length HTTP header*.
404406

405407
For sake of design decisions :class:`Response` is derived from
406408
:class:`StreamResponse` parent class.
@@ -411,7 +413,8 @@ The response supports *keep-alive* handling out-of-the-box if
411413
You can disable *keep-alive* by :meth:`~StreamResponse.force_close` though.
412414

413415
The common case for sending an answer from
414-
:ref:`web-handler<aiohttp-web-handler>` is returning a :class:`Response`instance::
416+
:ref:`web-handler<aiohttp-web-handler>` is returning a
417+
:class:`Response` instance::
415418

416419
def handler(request):
417420
return Response(request, "All right!")
@@ -744,7 +747,8 @@ Router is any object that implements :class:`AbstractRouter` interface.
744747

745748
.. class:: UrlDispatcher()
746749

747-
Straightforward url-mathing router.
750+
Straightforward url-mathing router, implements
751+
:class:`collections.abc.Mapping` for access to *named routes*.
748752

749753
Before running :class:`Application` you should fill *route
750754
table* first by calling :meth:`add_route` and :meth:`add_static`.
@@ -753,7 +757,15 @@ Router is any object that implements :class:`AbstractRouter` interface.
753757
added *routes* in FIFO order. The first matching *route* will be used
754758
to call corresponding *handler*.
755759

756-
.. method:: add_route(method, path, handler, *, endpoint=None)
760+
If on route creation you specify *name* parameter the result is
761+
*named route*.
762+
763+
*Named route* can be retrieved by ``app.router[name]`` call, checked for
764+
existence by ``name in app.router`` etc.
765+
766+
.. seealso:: :ref:`Route classes <aiohttp-web-route>`
767+
768+
.. method:: add_route(method, path, handler, *, name=None)
757769

758770
Append :ref:`handler<aiohttp-web-handler>` to the end of route table.
759771

@@ -765,9 +777,9 @@ Router is any object that implements :class:`AbstractRouter` interface.
765777

766778
:param callable handler: route handler
767779

768-
:param str endpoint: route name for :meth:`reverse` lookup.
780+
:param str name: optional route name.
769781

770-
.. method:: add_static(prefix, path, *, endpoint=None)
782+
.. method:: add_static(prefix, path, *, name=None)
771783

772784
Adds router for returning static files.
773785

@@ -784,36 +796,120 @@ Router is any object that implements :class:`AbstractRouter` interface.
784796
:param str path: path to the folder in file system that contains
785797
handled static files.
786798

787-
:param str endpoint: route name for :meth:`reverse` lookup.
799+
:param str name: optional route name.
788800

789-
.. method:: reverse(method, endpoint, *, parts=None, filename=None, \
790-
query=None)
801+
.. method:: resolve(requst)
802+
803+
A :ref:`coroutine<coroutine>` that returns
804+
:class:`AbstractMatchInfo` for *request* or raises http
805+
exception like :exc:`HTTPNotFound` if there is no registered
806+
route for *request*.
791807

792-
A :ref:`coroutine<coroutine>` that returns *URL* for given
793-
(*method*, *endpoint*) pair.
808+
Used by internal machinery, end user unlikely need to call the method.
794809

795-
:param mapping parts: :class:`dict` or other mapping for
796-
substitute into *dynamic endpoint* template
797-
(created by :meth:`add_route` with
798-
*path* like ``'/a/{var}'``)
810+
.. _aiohttp-web-route:
799811

800-
:param str filename: file name for specifying file for *static
801-
endpoint* (created by :meth:`add_static`)
812+
Route
813+
^^^^^
802814

803-
:param query: mapping or list of *(name, value)* pairs for
804-
specifying *query* part of url (parameter is
805-
processed by :func:`~urllib.parse.urlencode`).
815+
Default router :class:`UrlDispatcher` operates with *routes*.
806816

807-
:returns str: matched URL.
808-
:raises KeyError: *endpoint* is not registered.
809-
:raises ValueError: params combination is not acceptable.
817+
User should not instantiate route classes by hand but can give *named
818+
route instance* by ``router[name]`` if he have added route by
819+
:meth:`UrlDispatcher.add_route` or :meth:`UrlDispatcher.add_static`
820+
calls with non-empty *name* parameter.
810821

811-
.. method:: resolve(requst)
822+
The main usage of *named routes* is constructing URL by route name for
823+
passing it into *template engine* for example::
824+
825+
url = app.router['route_name'].url(query={'a': 1, 'b': 2})
826+
827+
There are three conctrete route classes:* :class:`DynamicRoute` for
828+
urls with :ref:`variable pathes<aiohttp-web-variable-handler>` spec.
829+
830+
831+
* :class:`PlainRoute` for urls without :ref:`variable
832+
pathes<aiohttp-web-variable-handler>`
833+
834+
* :class:`DynamicRoute` for urls with :ref:`variable
835+
pathes<aiohttp-web-variable-handler>` spec.
836+
837+
* :class:`StaticRoute` for static file handlers.
838+
839+
.. class:: Route
840+
841+
Base class for routes served by :class:`UrlDispatcher`.
842+
843+
.. attribute:: method
844+
845+
HTTP method handled by the route, e.g. *GET*, *POST* etc.
846+
847+
.. attribute:: handler
848+
849+
:ref:`handler<aiohttp-web-handler>` that processes the route.
850+
851+
.. attribute:: name
852+
853+
Name of the route.
854+
855+
.. method:: match(path)
856+
857+
Abstract method, accepts *URL path* and returns :class:`dict` with
858+
parsed *path parts* for :class:`UrlMappingMatchInfo` or ``None`` if
859+
the route cannot handle given *path*.
860+
861+
The method exists for internal usage, end user unlikely need to call it.
862+
863+
.. method:: url(*, query=None, **kwargs)
864+
865+
Abstract method for constructing url handled by the route.
866+
867+
*query* is a mapping or list of *(name, value)* pairs for
868+
specifying *query* part of url (parameter is processed by
869+
:func:`~urllib.parse.urlencode`).
870+
871+
Other available parameters depends on concrete route class and
872+
described in descendant classes.
873+
874+
.. class:: PlainRoute
875+
876+
The route class for handling plain *URL path*, e.g. ``"/a/b/c"``
877+
878+
.. method:: url(*, parts, query=None)
879+
880+
Construct url, doesn't accepts extra parameters::
881+
882+
>>> route.url(query={'d': 1, 'e': 2})
883+
'/a/b/c/?d=1&e=2'``
884+
885+
.. class:: DynamicRoute
886+
887+
The route class for handling :ref:`variable
888+
path<aiohttp-web-variable-handler>`, e.g. ``"/a/{name1}/{name2}"``
889+
890+
.. method:: url(*, parts, query=None)
891+
892+
Construct url with given *dynamic parts*::
893+
894+
>>> route.url(parts={'name1': 'b', 'name2': 'c'}, query={'d': 1, 'e': 2})
895+
'/a/b/c/?d=1&e=2'
896+
897+
898+
.. class:: StaticRoute
899+
900+
The route class for handling static files, created by
901+
:meth:`UrlDispatcher.add_static` call.
902+
903+
.. method:: url(*, filename, query=None)
904+
905+
Construct url for given *filename*::
906+
907+
>>> route.url(filename='img/logo.png', query={'param': 1})
908+
'/path/to/static/img/logo.png?param=1'
909+
910+
MatchInfo
911+
^^^^^^^^^
812912

813-
A :ref:`coroutine<coroutine>` that returns
814-
:class:`AbstractMatchInfo` for *request* or raises http
815-
exception like :exc:`HTTPNotFound` if there is no registered
816-
route for *request*.
817913

818914
Utilities
819915
---------

0 commit comments

Comments
 (0)