0

I was trying to create a flask app with certain limits on a particular endpoint. I have defined my app structure like so:

  • run.py - >. to instantiate and run the flask server
class MyFlaskApp(Flask):
    def run(self, host=None, port=None, debug=None, **options):
        with self.app_context():
            super(MyFlaskApp, self).run(host=host, port=port, debug=debug, **options)

app = MyFlaskApp(__name__)
CORS(app)

@app.errorhandler(429)
def ratelimit_handler(e):
    return e, 209

if __name__ == "__main__":
    app.run(host="0.0.0.0",port=5001, debug=True)
  • limiter.py - > the ratelmiter config file containing.

@app.errorhandler(429)
def ratelimit_handler(e):
    return e, 209

app = Flask(__name__)
limiter = Limiter(app, key_func=get_remote_address)
  • service.py -> Using Flask Pluggable Views
class MyView(flask.views.MethodView):
    decorators = [limiter.limit("10/second")]
    def post(self):
        return "get"

    def someother_functions(self):
        return ""
  • app.py-> to include all the routes
api_bp = Blueprint('api', __name__)
api = Api(api_bp)



#Route
api.add_resource(service_file.service,'/getpost')

The rate limit seems to work properly but i the custom errorhandler doesn't seem to work. what i am trying to achieve is to capture 429 error code and return it as some other code in 2xx group because of some limitation on my server side, but this doesn;t seem to work even when i place it in the limiter.py file similarly. I have been stuck at this for hours!. Any help would be appreciated!.

4

0 回答 0