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
|
.. _hooks:
Hooks
=====
* `Before Hooks`_
* `After Hooks`_
Falcon supports *before* and *after* hooks. You install a hook simply by
applying one of the decorators below, either to an individual responder or
to an entire resource.
For example, consider this hook that validates a POST request for
an image resource:
.. code:: python
def validate_image_type(req, resp, resource, params):
if req.content_type not in ALLOWED_IMAGE_TYPES:
msg = 'Image type not allowed. Must be PNG, JPEG, or GIF'
raise falcon.HTTPBadRequest(title='Bad request', description=msg)
You would attach this hook to an ``on_post`` responder like so:
.. code:: python
@falcon.before(validate_image_type)
def on_post(self, req, resp):
pass
Or, suppose you had a hook that you would like to apply to *all*
responders for a given resource. In that case, you would simply
decorate the resource class:
.. code:: python
@falcon.before(extract_project_id)
class Message:
def on_post(self, req, resp, project_id):
pass
def on_get(self, req, resp, project_id):
pass
.. note::
When decorating an entire resource class, all method names that resemble
responders, including *suffix*\ed (see also :meth:`~falcon.App.add_route`)
ones, are decorated. If, for instance, a method is called ``on_get_items``,
but it is not meant for handling ``GET`` requests under a route with the
*suffix* ``items``, the easiest workaround for preventing the hook function
from being applied to the method is renaming it not to clash with the
responder pattern.
Note also that you can pass additional arguments to your hook function
as needed:
.. code:: python
def validate_image_type(req, resp, resource, params, allowed_types):
if req.content_type not in allowed_types:
msg = 'Image type not allowed.'
raise falcon.HTTPBadRequest(title='Bad request', description=msg)
@falcon.before(validate_image_type, ['image/png'])
def on_post(self, req, resp):
pass
Falcon supports using any callable as a hook. This allows for using a class
instead of a function:
.. code:: python
class Authorize:
def __init__(self, roles):
self._roles = roles
def __call__(self, req, resp, resource, params):
pass
@falcon.before(Authorize(['admin']))
def on_post(self, req, resp):
pass
Falcon :ref:`middleware components <middleware>` can also be used to insert
logic before and after requests. However, unlike hooks,
:ref:`middleware components <middleware>` are triggered **globally** for all
requests.
.. Tip::
In order to pass data from a hook function to a resource function
use the ``req.context`` and ``resp.context`` objects. These context objects
are intended to hold request and response data specific to your app as it
passes through the framework.
Before Hooks
------------
.. autofunction:: falcon.before
After Hooks
-----------
.. autofunction:: falcon.after
|