首页 > 解决方案 > Tryind to create custom error response for flask_limiter in a flask restful app

问题描述

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

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)

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

app = Flask(__name__)
limiter = Limiter(app, key_func=get_remote_address)
class MyView(flask.views.MethodView):
    decorators = [limiter.limit("10/second")]
    def post(self):
        return "get"

    def someother_functions(self):
        return ""
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!.

标签: pythonpython-3.xflaskflask-restfulflask-limiter

解决方案


推荐阅读