a4185a0ba7
* blueprints * class_based_views * config * decorators * deploying * exceptions * extensions * getting_started * middleware * request_data * response * routing * static_files * streaming * testing * versioning * Fix bug and links * spelling mistakes * Bug fixes and minor tweaks * Create 1691.doc.rst * Bug fixes and tweaks Co-Authored-By: Harsha Narayana <harsha2k4@gmail.com>
41 lines
1.5 KiB
ReStructuredText
41 lines
1.5 KiB
ReStructuredText
Handler Decorators
|
|
==================
|
|
|
|
Since Sanic handlers are simple Python functions, you can apply decorators to them in a similar manner to Flask. A typical use case is when you want some code to run before a handler's code is executed.
|
|
|
|
Authorization Decorator
|
|
-----------------------
|
|
|
|
Let's say you want to check that a user is authorized to access a particular endpoint. You can create a decorator that wraps a handler function, checks a request if the client is authorized to access a resource, and sends the appropriate response.
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
from functools import wraps
|
|
from sanic.response import json
|
|
|
|
def authorized():
|
|
def decorator(f):
|
|
@wraps(f)
|
|
async def decorated_function(request, *args, **kwargs):
|
|
# run some method that checks the request
|
|
# for the client's authorization status
|
|
is_authorized = check_request_for_authorization_status(request)
|
|
|
|
if is_authorized:
|
|
# the user is authorized.
|
|
# run the handler method and return the response
|
|
response = await f(request, *args, **kwargs)
|
|
return response
|
|
else:
|
|
# the user is not authorized.
|
|
return json({'status': 'not_authorized'}, 403)
|
|
return decorated_function
|
|
return decorator
|
|
|
|
|
|
@app.route("/")
|
|
@authorized()
|
|
async def test(request):
|
|
return json({'status': 'authorized'})
|