Flas-Restful Звонок перед запросом

Я хочу создать API с помощью Flask-RESTFUL, и есть некоторые вещи, которые я не могу чтобы найти, before_request() и tear_down(), я не хочу повторять каждый запрос.


person moisesvega    schedule 09.03.2014    source источник
comment
Каков твой вопрос?   -  person msvalkon    schedule 10.03.2014
comment
Просто используйте обычный flask.before_request(); здесь Flask-RESTful ничего не должен делать.   -  person Martijn Pieters    schedule 10.03.2014
comment
Привет, проблема в том, что я уже пытаюсь использовать @before_request, но похоже, что после этого словарь args пуст, поэтому я не уверен, что я что-то там упускаю.   -  person moisesvega    schedule 11.03.2014


Ответы (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