Я хочу создать API с помощью Flask-RESTFUL, и есть некоторые вещи, которые я не могу чтобы найти, before_request() и tear_down(), я не хочу повторять каждый запрос.
Flas-Restful Звонок перед запросом
Ответы (1)
Вы также можете использовать 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()
person
Games Brainiac
schedule
05.02.2017
flask.before_request()
; здесь Flask-RESTful ничего не должен делать. - person Martijn Pieters   schedule 10.03.2014