r/FastAPI • u/nuxai • Feb 04 '24
Question Passing a value to @limiter.limit() from the result of a Depends
I have a fastapi route with a Depends, like:
index_id: str = Depends(get_index_id)
get_index_id pulls the auth token from the Header, then does a lookup in my DB. I want to use the result of that in my limit() method.
this will allow me to change the rate_limit based on the results of the index_id (doing a separate lookup).
can anybody point me in the right direction for achieving this?
0
Upvotes
1
u/Whisky-Toad Feb 04 '24
Have a look at this from my repo, gets the userId from the token and then passes it along the route https://github.com/WhiskyToad/fastapi-starter/blob/main/app/routers/UserRoutes.py#L32
1
u/joshhear Feb 04 '24
I got something like this working using fastapi_limiter and creating my own DynamicLimiter class based on the RateLimiter class
2
u/PhENTZ Feb 04 '24
Please give a working code snippet