I have problems to apply a decorator to my Resource
. The Api
decorators
parameter gets applied to each Resource-function or how do I have to understand the decorators
parameter? My decorator gets only applied at the start up and not on each function call. What am I doing wrong?
from flask import Flask, Blueprint
from flask.ext.restplus import Api, Resource, fields
app = Flask(__name__)
def my_decorator(input):
print("my decorator")
return input
api_blueprint = Blueprint('index', __name__, url_prefix='/0')
api = Api(api_blueprint, version='0.1.0', title='Index API',
description='The Index API helps to find things faster in the own database',
# ui=False,
decorators=[my_decorator]
)
app.register_blueprint(api_blueprint)
ns = api.namespace('index', description='Index API')
search_id_score_model = api.model('id', {
'id': fields.String(required=True),
'score': fields.Float(required=True)
})
search_result_model = api.model('result', {
'key': fields.String(required=True),
'ids': fields.List(fields.Nested(search_id_score_model, required=True), required=True)
})
@ns.route('/search/<string:query>')
class Search(Resource):
'''Serach'''
@api.marshal_list_with(search_result_model)
@my_decorator
def get(self, query):
'''fetch result by query'''
return None
if __name__ == '__main__':
app.run(debug=True)
There is two decorators levels: API-wide and Resource-wide.
The Api.decorators parameter allow to apply decorators to all resources whereas the Resource.method_decorators will be applied on all method for a given resource.
The problem in your case it that your decorator only call the print once applied and not once called. You should modify your decorator like this:
def my_decorator(func):
def wrapper(*args, **kwargs):
print("my decorator")
return func(*args, **kwargs)
wrapper.__doc__ = func.__doc__
wrapper.__name__ = func.__name__
return wrapper
Notice the __doc__
and __name__
affectations. The first allow to correctly extract the description, the __name__
allow to avoid automatic naming collisions. The name is optional if you manually specify an endpoint for each resource.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With