I've been working on a task to apply throttling on a flask app. For throttling, I've been looking into flask-limiter. My app has all endpoints extended from flask-restful's Resource.
class CompanyApi(Resource): decorators = [limiter.limit(limit_value="10/minute")] def get(self): return "successful"
From flask-limiter doc, its clearly mentioned that dynamic limits can be loaded using callable in decorator for method based views.
def get_limit(): company = request.args.get('company') limit = Company.query.get(company) #returns a limit string stored in db return limit @app.route("/check_company", methods=["GET"]) @limiter.limit(limit_value=get_limit) def check_company(): return "success"
While for pluggable views, its only provided the hardcoded example by setting the decorator as:
decorators = [limiter.limit(limit_value="10/minute")]
I've tried by setting default value in decorators and when request is processing, I get request params (company) on which limit is retrieved from db. Then overwriting limiter's limit value:
CompanyApi.decorators = [Limiter.limit(limit_value=get_limit)]
It's been changed but not effective. I need to set limit for each endpoint dynamically on the basis of request.
How can I achieve to set dynamic limits for class based views?