Files
Imago/app/middleware/versioning.py
T
bruno 2f2a5e9b4a
CI / Lint & Format (push) Failing after 28s
CI / Tests (push) Has been skipped
CI / Security Scan (push) Failing after 8s
CI / Docker Build (push) Has been skipped
Add admin panel, WebSocket support, and API versioning
Introduce an admin portal (React + Nginx), WebSocket routing, and
API versioning middleware with `/api/v1/` prefix deprecation.
Add master API key authentication, new Prometheus metrics for AI
token consumption and active WebSockets, and extend S3 config
with a public endpoint URL. Update test paths and fixtures to
align with the new routing structure.
2026-06-22 11:25:22 -04:00

40 lines
1.3 KiB
Python

"""
Middleware de versionnement d'API.
Ajoute l'en-tête `X-API-Version` aux réponses des routes sous `/api/v1/`
et gère un éventuel avertissement de dépréciation via Sunset.
"""
import logging
from typing import Awaitable, Callable
from fastapi import Request, Response
from starlette.middleware.base import BaseHTTPMiddleware
from app.config import settings
logger = logging.getLogger(__name__)
class APIVersioningMiddleware(BaseHTTPMiddleware):
async def dispatch(
self, request: Request, call_next: Callable[[Request], Awaitable[Response]]
) -> Response:
response = await call_next(request)
# Appliquer X-API-Version seulement aux routes versionnées (ex: /api/v1/)
path = request.url.path
if path.startswith("/api/v1/"):
response.headers["X-API-Version"] = "v1"
# Si une date de fin de support est configurée, l'ajouter en en-tête
if settings.API_V1_SUNSET_DATE:
response.headers["Sunset"] = settings.API_V1_SUNSET_DATE
# Logger un avertissement si on l'utilise
logger.warning("api.v1_sunset_warning", extra={
"client": request.client.host if request.client else "unknown",
"path": path,
"sunset": settings.API_V1_SUNSET_DATE
})
return response