Files
orion/middleware/decorators.py
Samir Boulahtit 21c13ca39b style: apply black and isort formatting across entire codebase
- Standardize quote style (single to double quotes)
- Reorder and group imports alphabetically
- Fix line breaks and indentation for consistency
- Apply PEP 8 formatting standards

Also updated Makefile to exclude both venv and .venv from code quality checks.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 19:30:17 +01:00

39 lines
1.1 KiB
Python

# middleware/decorators.py
"""
FastAPI decorators for cross-cutting concerns.
This module provides classes and functions for:
- Rate limiting decorators for endpoint protection
- Request throttling and abuse prevention
- Consistent error handling for rate limit violations
"""
from functools import wraps
from app.exceptions.base import RateLimitException # Add this import
from middleware.rate_limiter import RateLimiter
# Initialize rate limiter instance
rate_limiter = RateLimiter()
def rate_limit(max_requests: int = 100, window_seconds: int = 3600):
"""Rate limiting decorator for FastAPI endpoints."""
def decorator(func):
@wraps(func)
async def wrapper(*args, **kwargs):
client_id = "anonymous" # In production, extract from request
if not rate_limiter.allow_request(client_id, max_requests, window_seconds):
# Use custom exception instead of HTTPException
raise RateLimitException(
message="Rate limit exceeded", retry_after=window_seconds
)
return await func(*args, **kwargs)
return wrapper
return decorator