departements

This commit is contained in:
el 2025-07-01 18:25:10 +02:00
parent 6b53a419c9
commit 4c180fe1f8
19 changed files with 21999 additions and 431 deletions

View file

@ -2,7 +2,7 @@
import httpx
import logging
from core.config import settings
from datetime import datetime, timedelta
logger = logging.getLogger(__name__)
class FranceTravailAuthService:
@ -16,12 +16,12 @@ class FranceTravailAuthService:
async def get_access_token(self):
# Vérifiez si le token est encore valide dans le cache
if self._token_cache and self._token_cache.get("expires_at", 0) > httpx._compat.current_time():
if self._token_cache and self._token_cache.get("expires_at", 0) > datetime.now():
logger.info("Utilisation du token France Travail depuis le cache.")
return self._token_cache["access_token"]
logger.info("Obtention d'un nouveau token France Travail...")
token_url = settings.FRANCE_TRAVAIL_TOKEN_URL
token_url = settings.FRANCE_TRAVAIL_TOKEN_URL # C'est la ligne modifiée
client_id = settings.FRANCE_TRAVAIL_CLIENT_ID
client_secret = settings.FRANCE_TRAVAIL_CLIENT_SECRET
scope = "o2dsoffre api_offresdemploiv2" # Assurez-vous que ces scopes sont activés pour votre application
@ -34,35 +34,36 @@ class FranceTravailAuthService:
}
headers = {
"Content-Type": "application/x-www-form-urlencoded" # C'est très important !
"Content-Type": "application/x-www-form-urlencoded"
}
try:
async with httpx.AsyncClient() as client:
async with httpx.AsyncClient() as client:
try:
response = await client.post(token_url, data=data, headers=headers)
response.raise_for_status() # Lève une exception pour les codes d'erreur HTTP
token_data = response.json()
access_token = token_data.get("access_token")
expires_in = token_data.get("expires_in") # Durée de validité en secondes
# DÉBUT DE LA CORRECTION : Ces lignes sont maintenant correctement indentées dans le bloc try
token_data = response.json()
access_token = token_data.get("access_token")
expires_in = token_data.get("expires_in") # Durée de validité en secondes
if not access_token:
raise ValueError("Le token d'accès n'a pas été trouvé dans la réponse de France Travail.")
if not access_token:
raise ValueError("Le token d'accès n'a pas été trouvé dans la réponse de France Travail.")
# Mettre à jour le cache
self._token_cache = {
"access_token": access_token,
"expires_at": httpx._compat.current_time() + expires_in - 60 # 60 secondes de marge de sécurité
}
logger.info("Nouveau token France Travail obtenu et mis en cache.")
return access_token
except httpx.HTTPStatusError as e:
logger.error(f"Erreur HTTP lors de l'obtention du token France Travail: {e.response.status_code} - {e.response.text}")
# Re-raise une RuntimeError pour que le service appelant puisse la gérer
raise RuntimeError(f"Erreur d'authentification France Travail: {e.response.text}")
except Exception as e:
logger.error(f"Erreur inattendue lors de l'obtention du token France Travail: {e}")
raise RuntimeError(f"Erreur inattendue lors de l'obtention du token France Travail: {e}")
# Mettre à jour le cache
self._token_cache = {
"access_token": access_token,
"expires_at": datetime.now() + timedelta(seconds=expires_in - 60) # 60 secondes de marge de sécurité
}
logger.info("Nouveau token France Travail obtenu et mis en cache.")
return access_token
# FIN DE LA CORRECTION
except httpx.HTTPStatusError as e:
logger.error(f"Erreur HTTP lors de l'obtention du token France Travail: {e.response.status_code} - {e.response.text}")
# Re-raise une RuntimeError pour que le service appelant puisse la gérer
raise RuntimeError(f"Erreur d'authentification France Travail: {e.response.text}")
except Exception as e:
logger.error(f"Erreur inattendue lors de l'obtention du token France Travail: {e}")
raise RuntimeError(f"Erreur inattendue lors de l'obtention du token France Travail: {e}")
france_travail_auth_service = FranceTravailAuthService()