2

我想使用Flask-RESTFUL创建一个 API ,但有些东西我找不到。我不想重复自己的每个请求,所以我考虑使用 before_request() 和 tear_down() 方法。

4

1 回答 1

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()
于 2017-02-05T17:09:13.333 回答