from slowapi.util import get_remote_address | |
from slowapi import Limiter | |
from fastapi import Request | |
# Initialize a limiter instance | |
limiter = Limiter(key_func=get_remote_address) | |
def check_rate_limit(request: Request): | |
""" | |
Function to check if a user has exceeded their rate limit. | |
This is managed by slowapi's limiter. | |
""" | |
if not limiter.is_allowed(request): | |
raise HTTPException( | |
status_code=429, | |
detail="Rate limit exceeded. Please try again later." | |
) | |