Advanced Hook Usage
Multiple Hooks
You can apply multiple hooks to a resource. Just chain them together using the Falcon decorators:
@falcon.before(auth_hook)
@falcon.before(another_hook)
class AnotherResource:
def on_get(self, req, resp):
resp.media = {'message': 'Multiple hooks applied!'}
By using Falcon hooks, we achieve clean separation of concerns, making our code more modular and easier to maintain.
Python Falcon – Hooks
In Falcon, hooks are type of callbacks that are defined by the user and are executed before or after a responder method in a particular resource class to process client request. They are useful in as much as they allow a client to add or overwrite request processing functionality at certain predefined stages.