from fastapi import Request, HTTPException from db.receipt_repository import ReceiptRepository class RateLimiter: def __init__(self, max_requests: int = 5): self.max_requests = max_requests self.repository = ReceiptRepository() async def check_rate_limit(self, request: Request): """ Check if the client has exceeded the rate limit """ client_ip = request.client.host # Get the current submission count for this IP submission_count = self.repository.get_submission_count(client_ip) # If they've exceeded the limit, raise an exception if submission_count > self.max_requests: raise HTTPException( status_code=429, detail=f"Rate limit exceeded. Maximum {self.max_requests} receipts per hour." ) return client_ip