Home > Back-end >  How to apply flask_limiter to a route based on its variable based url?
How to apply flask_limiter to a route based on its variable based url?

Time:09-27

I have a route where the final part is variable.

api.add_resource(AsyncStatus, "generate/status/<string:id>")

In this route, I have applied a flask_limiter

class AsyncStatus(Resource):
    decorators = [limiter.limit("1/minute")]
    def get(self, id = ''):

However, this limiter applies for the whole route. Want to apply the limiter to each id individually. So that the same IP can grab the id 1, id 2 and id 3 within the same minute, but cannot grab id 1, 3 times in the same minute.

CodePudding user response:

I figured it out. I need to create a function and pick the path from the request

from flask import request

def get_request_id():
    return(request.path)

class AsyncStatus(Resource):
    decorators = [limiter.limit("1/minute", key_func = get_request_id)]
    def get(self, id = ''):
  • Related