@@ -89,7 +89,8 @@ Handlers can be first-class functions, e.g.::
89
89
90
90
app.router.add_route('GET', '/', hello)
91
91
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.
93
94
94
95
:mod: `aiohttp.web ` doesn't dictate any implementation details,
95
96
so application developer can use classes if he wants::
@@ -398,9 +399,10 @@ Response classes
398
399
For now, :mod: `aiohttp.web ` has two classes for the *HTTP response *:
399
400
:class: `StreamResponse ` and :class: `Response `.
400
401
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 *.
404
406
405
407
For sake of design decisions :class: `Response ` is derived from
406
408
:class: `StreamResponse ` parent class.
@@ -411,7 +413,8 @@ The response supports *keep-alive* handling out-of-the-box if
411
413
You can disable *keep-alive * by :meth: `~StreamResponse.force_close ` though.
412
414
413
415
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::
415
418
416
419
def handler(request):
417
420
return Response(request, "All right!")
@@ -744,7 +747,8 @@ Router is any object that implements :class:`AbstractRouter` interface.
744
747
745
748
.. class :: UrlDispatcher()
746
749
747
- Straightforward url-mathing router.
750
+ Straightforward url-mathing router, implements
751
+ :class: `collections.abc.Mapping ` for access to *named routes *.
748
752
749
753
Before running :class: `Application ` you should fill *route
750
754
table * first by calling :meth: `add_route ` and :meth: `add_static `.
@@ -753,7 +757,15 @@ Router is any object that implements :class:`AbstractRouter` interface.
753
757
added *routes * in FIFO order. The first matching *route * will be used
754
758
to call corresponding *handler *.
755
759
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)
757
769
758
770
Append :ref: `handler<aiohttp-web-handler> ` to the end of route table.
759
771
@@ -765,9 +777,9 @@ Router is any object that implements :class:`AbstractRouter` interface.
765
777
766
778
:param callable handler: route handler
767
779
768
- :param str endpoint: route name for :meth: ` reverse ` lookup .
780
+ :param str name: optional route name.
769
781
770
- .. method :: add_static(prefix, path, *, endpoint =None)
782
+ .. method :: add_static(prefix, path, *, name =None)
771
783
772
784
Adds router for returning static files.
773
785
@@ -784,36 +796,120 @@ Router is any object that implements :class:`AbstractRouter` interface.
784
796
:param str path: path to the folder in file system that contains
785
797
handled static files.
786
798
787
- :param str endpoint: route name for :meth: ` reverse ` lookup .
799
+ :param str name: optional route name.
788
800
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 *.
791
807
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.
794
809
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 :
799
811
800
- :param str filename: file name for specifying file for * static
801
- endpoint * (created by :meth: ` add_static `)
812
+ Route
813
+ ^^^^^
802
814
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 *.
806
816
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.
810
821
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
+ ^^^^^^^^^
812
912
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 *.
817
913
818
914
Utilities
819
915
---------
0 commit comments