处理程序装饰器

由于sanic处理程序是简单的python函数,所以可以以类似于flask的方式将修饰器应用于它们。一个典型的用例是,当您希望在执行处理程序的代码之前运行一些代码时。

授权修饰器

假设您希望检查用户是否有权访问特定端点。您可以创建一个包装处理程序函数的装饰器,检查客户机是否有权访问资源,并发送适当的响应。

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'})