Complete the code to import the rate limiting dependency from fastapi-limiter.
from fastapi_limiter import [1]
The RateLimiter is the correct class to import for rate limiting in FastAPI using fastapi-limiter.
Complete the code to apply a rate limit of 5 requests per minute to the endpoint.
@app.get("/items", dependencies=[Depends([1]("5/minute"))]) def read_items(): return {"message": "Hello"}
The RateLimiter dependency is used to enforce the rate limit on the endpoint.
Fix the error in the initialization of the Redis backend for rate limiting.
import aioredis redis = aioredis.from_url([1]) @app.on_event("startup") async def startup(): await FastAPILimiter.init(redis)
The Redis URL must start with redis:// and include the port number for proper connection.
Fill both blanks to create a rate limit that allows 10 requests per minute and 100 requests per hour.
@app.get("/data", dependencies=[Depends(RateLimiter([1])), Depends(RateLimiter([2]))]) async def get_data(): return {"data": "sample"}
The first rate limiter allows 10 requests per minute, and the second allows 100 requests per hour, combining limits.
Fill all three blanks to create a dictionary comprehension that maps each user to their request count if it is greater than 10.
user_counts = {user: count for user, count in counts.items() if count [1] [2] and user.is_active == [3]This comprehension filters users with counts greater than 10 and only active users.