From 5191c8c0b8667b7074f87572bf35f1f38591e85c Mon Sep 17 00:00:00 2001 From: Lagicrus Date: Fri, 11 Oct 2019 14:01:33 +0100 Subject: [PATCH] decorators --- docs/sanic/decorators.md | 39 -------------------------------------- docs/sanic/decorators.rst | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 39 deletions(-) delete mode 100644 docs/sanic/decorators.md create mode 100644 docs/sanic/decorators.rst diff --git a/docs/sanic/decorators.md b/docs/sanic/decorators.md deleted file mode 100644 index 68495a22..00000000 --- a/docs/sanic/decorators.md +++ /dev/null @@ -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'}) -``` - diff --git a/docs/sanic/decorators.rst b/docs/sanic/decorators.rst new file mode 100644 index 00000000..2351b3bc --- /dev/null +++ b/docs/sanic/decorators.rst @@ -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'})