File: routing.rst

package info (click to toggle)
python-falcon 4.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,172 kB
  • sloc: python: 33,608; javascript: 92; sh: 50; makefile: 50
file content (526 lines) | stat: -rw-r--r-- 18,941 bytes parent folder | download
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
.. _routing:

Routing
=======

Falcon uses resource-based routing to encourage a RESTful architectural style.
Each resource is represented by a class that is responsible for handling all of
the HTTP methods that the resource supports.

For each HTTP method supported by the resource, the class implements a
corresponding Python method with a name that starts with ``on_`` and ends in the
lowercased HTTP method name (e.g., ``on_get()``, ``on_patch()``,
``on_delete()``, etc.)

.. note::
    Resources in Falcon are represented by a single class instance that is
    created at application startup when the routes are configured. This
    minimizes routing overhead and simplifies the implementation of resource
    classes. In the case of WSGI apps, this also means that resource classes
    must be implemented in a thread-safe manner (see also:
    :ref:`faq_thread_safety`).

Falcon routes incoming requests (including :ref:`WebSocket handshakes <ws>`) to
resources based on a set of URI templates. If the path requested by the client
matches the template for a given route, the request is then passed on to the
associated resource for processing.

Here's a quick example to show how all the pieces fit together:

.. tab-set::

    .. tab-item:: WSGI

        .. code:: python

            import json

            import falcon


            class ImagesResource:

                def on_get(self, req, resp):
                    doc = {
                        'images': [
                            {
                                'href': '/images/1eaf6ef1-7f2d-4ecc-a8d5-6e8adba7cc0e.png'
                            }
                        ]
                    }

                    # Create a JSON representation of the resource; this could
                    #   also be done automatically by assigning to resp.media
                    resp.text = json.dumps(doc, ensure_ascii=False)

                    # The following line can be omitted because 200 is the default
                    # status returned by the framework, but it is included here to
                    # illustrate how this may be overridden as needed.
                    resp.status = falcon.HTTP_200


            app = falcon.App()

            images = ImagesResource()
            app.add_route('/images', images)

    .. tab-item:: ASGI

        .. code:: python

            import json

            import falcon
            import falcon.asgi


            class ImagesResource:

                async def on_get(self, req, resp):
                    doc = {
                        'images': [
                            {
                                'href': '/images/1eaf6ef1-7f2d-4ecc-a8d5-6e8adba7cc0e.png'
                            }
                        ]
                    }

                    # Create a JSON representation of the resource; this could
                    #   also be done automatically by assigning to resp.media
                    resp.text = json.dumps(doc, ensure_ascii=False)

                    # The following line can be omitted because 200 is the default
                    # status returned by the framework, but it is included here to
                    # illustrate how this may be overridden as needed.
                    resp.status = falcon.HTTP_200


            app = falcon.asgi.App()

            images = ImagesResource()
            app.add_route('/images', images)

If no route matches the request, control then passes to a default responder that
simply raises an instance of :class:`~.HTTPRouteNotFound`. By default, this
error will be rendered as a 404 response for a regular HTTP request, and a 403
response with a 3404 close code for a :ref:`WebSocket <ws>` handshake. This
behavior can be modified by adding a custom error handler (see also
:ref:`this FAQ topic <faq_override_404_500_handlers>`).

On the other hand, if a route is matched but the resource does not implement a
responder for the requested HTTP method, the framework invokes a default
responder that raises an instance of :class:`~.HTTPMethodNotAllowed`. This class
will be rendered by default as a 405 response for a regular HTTP request, and a
403 response with a 3405 close code for a :ref:`WebSocket <ws>` handshake.

Falcon also provides a default responder for OPTIONS requests that takes into
account which methods are implemented for the target resource.

Default Behavior
----------------

Falcon's default routing engine is based on a decision tree that is
first compiled into Python code, and then evaluated by the runtime.
By default, the decision tree is compiled only when the router handles
the first request. See :class:`.CompiledRouter` for more details.

The :meth:`falcon.App.add_route` and :meth:`falcon.asgi.App.add_route` methods
are used to associate a URI template with a resource. Falcon then maps incoming
requests to resources based on these templates.

Falcon's default router uses Python classes to represent resources. In
practice, these classes act as controllers in your application. They
convert an incoming request into one or more internal actions, and then
compose a response back to the client based on the results of those
actions. (See also:
:ref:`Tutorial: Creating Resources <tutorial_resources>`)

.. code:: none

               ┌────────────┐
    request  → │            │
               │ Resource   │ ↻ Orchestrate the requested action
               │ Controller │ ↻ Compose the result
    response ← │            │
               └────────────┘

Each resource class defines various "responder" methods, one for each
HTTP method the resource allows. Responder names start with ``on_`` and
are named according to which HTTP method they handle, as in ``on_get()``,
``on_post()``, ``on_put()``, etc.

.. note::
    If your resource does not support a particular
    HTTP method, simply omit the corresponding responder and
    Falcon will use a default responder that raises
    an instance of :class:`~.HTTPMethodNotAllowed` when that
    method is requested. Normally this results in sending a
    405 response back to the client.

Responders must always define at least two arguments to receive
:class:`~.falcon.Request` and :class:`~.falcon.Response` objects, respectively::

    def on_post(self, req, resp):
        pass

For ASGI apps, the responder must be a coroutine function::

    async def on_post(self, req, resp):
        pass

The :class:`~.falcon.Request` object represents the incoming HTTP
request. It exposes properties and methods for examining headers,
query string parameters, and other metadata associated with
the request. A file-like stream object is also provided for reading
any data that was included in the body of the request.

The :class:`~.falcon.Response` object represents the application's
HTTP response to the above request. It provides properties
and methods for setting status, header and body data. The
:class:`~.falcon.Response` object also exposes a dict-like
:attr:`~.falcon.Response.context` property for passing arbitrary
data to hooks and middleware methods.

.. note::
    Rather than directly manipulate the :class:`~.falcon.Response`
    object, a responder may raise an instance of either
    :class:`~.HTTPError` or :class:`~.HTTPStatus`. Falcon will
    convert these exceptions to appropriate HTTP responses.
    Alternatively, you can handle them yourself via
    :meth:`~.falcon.App.add_error_handler`.

In addition to the standard `req` and `resp` parameters, if the
route's template contains field expressions, any responder that
desires to receive requests for that route must accept arguments
named after the respective field names defined in the template.

A field expression consists of a bracketed field name. For
example, given the following template::

    /user/{name}

A PUT request to ``'/user/kgriffs'`` would cause the framework to invoke
the ``on_put()`` responder method on the route's resource class, passing
``'kgriffs'`` via an additional `name` argument defined by the responder:

.. tab-set::

    .. tab-item:: WSGI

        .. code:: python

            # Template fields correspond to named arguments or keyword
            #   arguments, following the usual req and resp args.
            def on_put(self, req, resp, name):
                pass

    .. tab-item:: ASGI

        .. code:: python

            # Template fields correspond to named arguments or keyword
            #   arguments, following the usual req and resp args.
            async def on_put(self, req, resp, name):
                pass

Because field names correspond to argument names in responder
methods, they must be valid Python identifiers.

Individual path segments may contain one or more field
expressions, and fields need not span the entire path
segment. For example::

    /repos/{org}/{repo}/compare/{usr0}:{branch0}...{usr1}:{branch1}
    /serviceRoot/People('{name}')

(See also the :ref:`Falcon tutorial <tutorial>` for additional examples
and a walkthrough of setting up routes within the context of a sample
application.)

.. _routing_field_converters:

Field Converters
----------------

Falcon's default router supports the use of field converters to
transform a URI template field value. Field converters may also perform
simple input validation. For example, the following URI template uses
the `int` converter to convert the value of `tid` to a Python ``int``,
but only if it has exactly eight digits::

    /teams/{tid:int(8)}

If the value is malformed and can not be converted, Falcon will reject
the request with a 404 response to the client.

Converters are instantiated with the argument specification given in the
field expression. These specifications follow the standard Python syntax
for passing arguments. For example, the comments in the following code
show how a converter would be instantiated given different argument
specifications in the URI template:

.. code:: python

    # IntConverter()
    app.add_route(
        '/a/{some_field:int}',
        some_resource
    )

    # IntConverter(8)
    app.add_route(
        '/b/{some_field:int(8)}',
        some_resource
    )

    # IntConverter(8, min=10000000)
    app.add_route(
        '/c/{some_field:int(8, min=10000000)}',
        some_resource
    )

(See also how :class:`~.UUIDConverter` is used in Falcon's ASGI tutorial:
:ref:`asgi_tutorial_image_resources`.)

.. _routing_builtin_converters:

Built-in Converters
-------------------

============  =================================  ==================================================================
 Identifier    Class                              Example
============  =================================  ==================================================================
 ``int``       :class:`~.IntConverter`            ``/teams/{tid:int(8)}``
 ``uuid``      :class:`~.UUIDConverter`           ``/diff/{left:uuid}...{right:uuid}``
 ``dt``        :class:`~.DateTimeConverter`       ``/logs/{day:dt("%Y-%m-%d")}``
 ``float``     :class:`~.FloatConverter`          ``/python/versions/{version:float(min=3.7)}``
 ``path``      :class:`~.PathConverter`           ``/prefix/{other:path}``
============  =================================  ==================================================================

|

.. autoclass:: falcon.routing.IntConverter
    :members:

.. autoclass:: falcon.routing.FloatConverter
    :members:

.. autoclass:: falcon.routing.UUIDConverter
    :members:

.. autoclass:: falcon.routing.DateTimeConverter
    :members:

.. autoclass:: falcon.routing.PathConverter
    :members:

.. _routing_custom_converters:

Custom Converters
-----------------

Custom converters can be registered via the
:attr:`~.CompiledRouterOptions.converters` router option. A converter is
simply a class that implements the ``BaseConverter`` interface:

.. autoclass:: falcon.routing.BaseConverter
    :members:

.. _routing_custom:

Custom Routers
--------------

A custom routing engine may be specified when instantiating
:meth:`falcon.App` or :meth:`falcon.asgi.App`. For example:

.. code:: python

    router = MyRouter()
    app = App(router=router)

Custom routers may derive from the default :class:`~.CompiledRouter`
engine, or implement a completely different routing strategy (such as
object-based routing).

A custom router is any class that implements the following interface:

.. code:: python

    class MyRouter:
        def add_route(self, uri_template, resource, **kwargs):
            """Adds a route between URI path template and resource.

            Args:
                uri_template (str): A URI template to use for the route
                resource (object): The resource instance to associate with
                    the URI template.

            Keyword Args:
                suffix (str): Optional responder name suffix for this
                    route. If a suffix is provided, Falcon will map GET
                    requests to ``on_get_{suffix}()``, POST requests to
                    ``on_post_{suffix}()``, etc. In this way, multiple
                    closely-related routes can be mapped to the same
                    resource. For example, a single resource class can
                    use suffixed responders to distinguish requests for
                    a single item vs. a collection of those same items.
                    Another class might use a suffixed responder to handle
                    a shortlink route in addition to the regular route for
                    the resource.

                **kwargs (dict): Accepts any additional keyword arguments
                    that were originally passed to the falcon.App.add_route()
                    method. These arguments MUST be accepted via the
                    double-star variadic pattern (**kwargs), and ignore any
                    unrecognized or unsupported arguments.
            """

        def find(self, uri, req=None):
            """Search for a route that matches the given partial URI.

            Args:
                uri(str): The requested path to route.

            Keyword Args:
                 req(Request): The Request object that will be passed to
                    the routed responder. The router may use `req` to
                    further differentiate the requested route. For
                    example, a header may be used to determine the
                    desired API version and route the request
                    accordingly.

                    Note:
                        The `req` keyword argument was added in version
                        1.2. To ensure backwards-compatibility, routers
                        that do not implement this argument are still
                        supported.

            Returns:
                tuple: A 4-member tuple composed of (resource, method_map,
                    params, uri_template), or ``None`` if no route matches
                    the requested path.

            """

Suffixed Responders
-------------------

While Falcon encourages the REST architectural style, it is flexible enough to accommodate other
paradigms. Consider the task of building an API for a calculator which can both add and subtract
two numbers. You could implement the
following:

.. code:: python

    class Add():
        def on_get(self, req, resp):
            resp.text = str(req.get_param_as_int('x') + req.get_param_as_int('y'))
            resp.status = falcon.HTTP_200


    class Subtract():
        def on_get(self, req, resp):
            resp.text = str(req.get_param_as_int('x') - req.get_param_as_int('y'))
            resp.status = falcon.HTTP_200


    add = Add()
    subtract = Subtract()
    app = falcon.App()
    app.add_route('/add', add)
    app.add_route('/subtract', subtract)

However, this approach highlights a situation in which grouping by resource may not make sense for
your domain. In this context, adding and subtracting don't seem to conceptually map to two separate resource
collections. Instead of separating them based on the idea of "getting" different resources from
each, we might want to group them based on the attributes of their function (i.e., take two
numbers, do something to them, return the result).

With Suffixed Responders, we can do just that, rewriting the example above in a more procedural
style:

.. code:: python

    class Calculator():
        def on_get_add(self, req, resp):
            resp.text = str(req.get_param_as_int('x') + req.get_param_as_int('y'))
            resp.status = falcon.HTTP_200

        def on_get_subtract(self, req, resp):
            resp.text = str(req.get_param_as_int('x') - req.get_param_as_int('y'))
            resp.status = falcon.HTTP_200


    calc = Calculator()
    app = falcon.App()
    app.add_route('/add', calc, suffix='add')
    app.add_route('/subtract', calc, suffix='subtract')

In the second iteration, using Suffixed Responders, we're able to group responders based on their
actions rather than the data they represent. This gives us added flexibility to accommodate
situations in which a purely RESTful approach simply doesn't fit.

Default Router
--------------

.. autoclass:: falcon.routing.CompiledRouter
    :members:


Routing Utilities
-----------------

The *falcon.routing* module contains the following utilities that may
be used by custom routing engines.

.. autofunction:: falcon.routing.map_http_methods

.. autofunction:: falcon.routing.set_default_responders

.. autofunction:: falcon.app_helpers.prepare_middleware

.. autofunction:: falcon.app_helpers.prepare_middleware_ws


Custom HTTP Methods
-------------------

While not normally advised, some applications may need to support non-standard
HTTP methods, in addition to the standard HTTP methods like GET and PUT. To
support custom HTTP methods, use one of the following methods:

- Ideally, if you don't use hooks in your application, you can easily add the
  custom methods in your application setup by overriding the value of
  ``falcon.constants.COMBINED_METHODS``. For example::

    import falcon.constants
    falcon.constants.COMBINED_METHODS += ['FOO', 'BAR']

- Due to the nature of hooks, if you do use them, you'll need to define the
  FALCON_CUSTOM_HTTP_METHODS environment variable as a comma-delimited list
  of custom methods. For example::

    $ export FALCON_CUSTOM_HTTP_METHODS=FOO,BAR


Once you have used the appropriate method, your custom methods should be active.
You then can define request methods like any other HTTP method:

.. tab-set::

    .. tab-item:: WSGI

        .. code:: python

            # Handle the custom FOO method
            def on_foo(self, req, resp):
                pass

    .. tab-item:: ASGI

        .. code:: python

            # Handle the custom FOO method
            async def on_foo(self, req, resp):
                pass