Flask-Restful请求电话前

时间:2014-03-09 20:23:07

标签: python flask flask-restful

我想使用Flask-RESTFUL创建一个API,并且有一些我无法找到的东西,before_request()和tear_down(),我不想重复每一个请求

1 个答案:

答案 0 :(得分:0)

您还可以将method_decorators用于充满烧瓶的Resource对象。

class Resource(MethodView):
    """
    Represents an abstract RESTful resource. Concrete resources should
    extend from this class and expose methods for each supported HTTP
    method. If a resource is invoked with an unsupported HTTP method,
    the API will return a response with status 405 Method Not Allowed.
    Otherwise the appropriate method is called and passed all arguments
    from the url rule used when adding the resource to an Api instance. See
    :meth:`~flask_restful.Api.add_resource` for details.
    """
    representations = None
    method_decorators = []

如果您查看源代码,它将按顺序应用装饰器:

for decorator in self.method_decorators:
    meth = decorator(meth)

resp = meth(*args, **kwargs)

if isinstance(resp, ResponseBase):  # There may be a better way to test
    return resp

representations = self.representations or OrderedDict()