File size: 2,876 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
.. _aiohttp-router-refactoring-021:

Router refactoring in 0.21
==========================

Rationale
---------

First generation (v1) of router has mapped ``(method, path)`` pair to
:term:`web-handler`.  Mapping is named **route**. Routes used to have
unique names if any.

The main mistake with the design is coupling the **route** to
``(method, path)`` pair while really URL construction operates with
**resources** (**location** is a synonym). HTTP method is not part of URI
but applied on sending HTTP request only.

Having different **route names** for the same path is confusing. Moreover
**named routes** constructed for the same path should have unique
non overlapping names which is cumbersome is certain situations.

From other side sometimes it's desirable to bind several HTTP methods
to the same web handler. For *v1* router it can be solved by passing '*'
as HTTP method. Class based views require '*' method also usually.


Implementation
--------------

The change introduces **resource** as first class citizen::

   resource = router.add_resource('/path/{to}', name='name')

*Resource* has a **path** (dynamic or constant) and optional **name**.

The name is **unique** in router context.

*Resource* has **routes**.

*Route* corresponds to *HTTP method* and :term:`web-handler` for the method::

   route = resource.add_route('GET', handler)

User still may use wildcard for accepting all HTTP methods (maybe we
will add something like ``resource.add_wildcard(handler)`` later).

Since **names** belongs to **resources** now ``app.router['name']``
returns a **resource** instance instead of :class:`aiohttp.web.AbstractRoute`.

**resource** has ``.url()`` method, so
``app.router['name'].url(parts={'a': 'b'}, query={'arg': 'param'})``
still works as usual.


The change allows to rewrite static file handling and implement nested
applications as well.

Decoupling of *HTTP location* and *HTTP method* makes life easier.

Backward compatibility
----------------------

The refactoring is 99% compatible with previous implementation.

99% means all example and the most of current code works without
modifications but we have subtle API backward incompatibles.

``app.router['name']`` returns a :class:`aiohttp.web.AbstractResource`
instance instead of :class:`aiohttp.web.AbstractRoute` but resource has the
same ``resource.url(...)`` most useful method, so end user should feel no
difference.

``route.match(...)`` is **not** supported anymore, use
:meth:`aiohttp.web.AbstractResource.resolve` instead.

``app.router.add_route(method, path, handler, name='name')`` now is just
shortcut for::

    resource = app.router.add_resource(path, name=name)
    route = resource.add_route(method, handler)
    return route

``app.router.register_route(...)`` is still supported, it creates
``aiohttp.web.ResourceAdapter`` for every call (but it's deprecated now).