decorators
This commit is contained in:
parent
291747d575
commit
5191c8c0b8
@ -1,39 +0,0 @@
|
|||||||
# 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.
|
|
||||||
|
|
||||||
|
|
||||||
```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'})
|
|
||||||
```
|
|
||||||
|
|
40
docs/sanic/decorators.rst
Normal file
40
docs/sanic/decorators.rst
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
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'})
|
Loading…
x
Reference in New Issue
Block a user