First commit
This commit is contained in:
45
api/app/dependencies.py
Normal file
45
api/app/dependencies.py
Normal file
@@ -0,0 +1,45 @@
|
||||
"""
|
||||
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
|
||||
Reference in New Issue
Block a user