noirbizarre / flask-restplus

Fully featured framework for fast, easy and documented API development with Flask
http://flask-restplus.readthedocs.org
Other
2.73k stars 507 forks source link

Feature request: add before_first_request decorator #140

Open postrational opened 8 years ago

postrational commented 8 years ago

Hi there!

Keep up the great work you're doing on RestPlus. It's becoming a significant, fully featured framework.

There is a feature I would like to request. Sometimes we find a need to do some setup work before a request, in particular before the first request. We can currently use the Flask before_first_request decorator to do this.

It would be great if we didn't have to put these bindings globally in the flask app definition, but if we could keep them together with our API definitions.

To achieve this you could add the decorators before_first_request, before_request and after_request to the RestPlus API and Namespace classes, so I could do something like:

from flask_restplus import Namespace, Resource, fields

api = Namespace('cats', description='Cats related operations')

@api.before_first_request
def set_up():
    run_some_setup()

@api.route('/')
class CatList(Resource):
    ...
nirob07 commented 4 years ago

can you please tell me why this is happen i am using flask restplus my code is from flask_jwt_extended import ( jwt_required, jwt_refresh_token_required, get_jwt_identity, get_raw_jwt )

ns1 = Namespace('brands', description='Cats related operations')

@ns1.before_first_request def set_up(): u = get_jwt_identity() if(u): user = User.query.filter_by(id=u).first() else: return forbidden('Not Log In') g.current_user = user