45 lines
1.0 KiB
Python
45 lines
1.0 KiB
Python
"""
|
|
Dépendances pour l'API FastAPI
|
|
"""
|
|
|
|
import redis
|
|
from fastapi import Depends, HTTPException, Header
|
|
from typing import Optional
|
|
|
|
from .config import settings
|
|
|
|
|
|
def get_redis_connection():
|
|
"""
|
|
Crée et retourne une connexion à Redis
|
|
"""
|
|
try:
|
|
conn = redis.Redis.from_url(settings.REDIS_URL)
|
|
yield conn
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=500,
|
|
detail=f"Erreur de connexion à Redis: {str(e)}"
|
|
)
|
|
finally:
|
|
# Fermer la connexion
|
|
pass # La connexion est fermée automatiquement
|
|
|
|
|
|
async def verify_api_key(x_api_key: Optional[str] = Header(None)):
|
|
"""
|
|
Vérifie que la clé API fournie est valide
|
|
"""
|
|
if not x_api_key:
|
|
raise HTTPException(
|
|
status_code=401,
|
|
detail="Clé API manquante"
|
|
)
|
|
|
|
if x_api_key != settings.API_KEY:
|
|
raise HTTPException(
|
|
status_code=403,
|
|
detail="Clé API invalide"
|
|
)
|
|
|
|
return x_api_key |