pgjones / quart-rate-limiter

Quart-Rate-Limiter is an extension for Quart to allow for rate limits to be defined and enforced on a per route basis.
MIT License
23 stars 6 forks source link

Enhancement: Ratelimit for different keys #7

Closed QuartzWarrior closed 5 months ago

QuartzWarrior commented 11 months ago

RateLimit key function should return both the key and a boolean. If the boolean is false, don't run the said RateLimit. If it is true, the RateLimit will run.

Example:

async def get_api_key():
    key = request.headers.get("x-api-key") or request.args.get("api_key")
    return key, True
async def is_premium():
    key = request.headers.get("x-api-key") or request.args.get("api_key")
    prem = False
    if "prem" in key:
        prem = True
    return key, prem
app = Quart(__name__)
limiter = RateLimiter(
    app=app,
    rate=[RateLimit(10, timedelta(minutes=1), get_api_key), RateLimit(20, timedelta(minutes=1), is_premium)],
)

( Now any route called will have a different rate limit depending on the key type. )

pgjones commented 5 months ago

I think the skip_function introduced in 41e4cc8bd62a7712eae9942c2ea6d548b1b81548 will do what you want here, please reopen if not.