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

@ -1,81 +1,62 @@
import httpx
import logging
from typing import List, Dict, Any, Optional
from typing import List, Dict, Any
from core.config import settings
from services.oauth_service import OAuthService # Assurez-vous que ce service existe
from services.france_travail_auth_service import france_travail_auth_service
logger = logging.getLogger(__name__)
class RomeoService:
def __init__(self):
self.base_url = settings.FRANCE_TRAVAIL_ROMEO_API_URL # URL de base de l'API Romeo
self.scope = "api_romeov2" # Scope spécifique pour Romeo
self.oauth_service = OAuthService(settings.FRANCE_TRAVAIL_OAUTH_URL, settings.FRANCE_TRAVAIL_CLIENT_ID, settings.FRANCE_TRAVAIL_CLIENT_SECRET)
self.client = httpx.AsyncClient()
# CORRIGÉ ICI: Utilise 'FRANCE_TRAVAIL_ROMEO_API_URL' comme suggéré par l'erreur
self.base_url = settings.FRANCE_TRAVAIL_ROMEO_API_URL
self._http_client = httpx.AsyncClient()
logger.info(f"RomeoService initialized with base_url: {self.base_url}")
async def _get_access_token(self) -> str:
"""Récupère le token d'accès spécifique à l'API Romeo."""
try:
token_response = await self.oauth_service.get_access_token(self.scope)
return token_response.access_token
except Exception as e:
logger.error(f"Erreur lors de la récupération du token d'accès pour Romeo: {e}")
raise RuntimeError(f"Impossible de récupérer le token d'accès pour Romeo: {e}")
async def _call_api(self, endpoint: str, text: str) -> Optional[Dict[str, Any]]:
async def _call_api(self, endpoint: str, data: Dict[str, Any]) -> Dict[str, Any]:
"""
Appelle un endpoint de l'API Romeo avec le texte donné.
Gère l'authentification et les erreurs de base.
Appel générique à une endpoint de l'API Romeo.
Récupère le jeton d'accès via le service d'authentification.
"""
token = await self._get_access_token()
access_token = await france_travail_auth_service.get_access_token()
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json; charset=utf-8"
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json"
}
# Les APIs Romeo attendent le texte dans un champ 'texte' de l'objet JSON
data = {"texte": text}
url = f"{self.base_url}{endpoint}"
logger.info(f"Calling Romeo API: {url} with text length {len(text)}")
logger.info(f"Appel API Romeo: {url} avec données: {data.keys()}")
try:
response = await self.client.post(url, headers=headers, json=data, timeout=30.0)
response.raise_for_status() # Lève une exception pour les codes d'erreur HTTP (4xx ou 5xx)
response = await self._http_client.post(url, json=data, headers=headers)
response.raise_for_status() # Lève une exception pour les codes d'état HTTP 4xx/5xx
logger.info(f"Réponse API Romeo reçue (status: {response.status_code}).")
return response.json()
except httpx.HTTPStatusError as e:
logger.error(f"Erreur HTTP lors de l'appel à Romeo {endpoint}: {e.response.status_code} - {e.response.text}")
raise RuntimeError(f"Erreur de l'API Romeo: {e.response.text}")
except httpx.RequestError as e:
logger.error(f"Erreur réseau ou de requête lors de l'appel à Romeo {endpoint}: {e}")
raise RuntimeError(f"Erreur de communication avec l'API Romeo: {e}")
logger.error(f"Erreur HTTP lors de l'appel à l'API Romeo: {e.response.status_code} - {e.response.text}")
raise RuntimeError(f"Erreur lors de l'appel à l'API Romeo: {e.response.text}")
except Exception as e:
logger.error(f"Une erreur inattendue est survenue lors de l'appel à Romeo {endpoint}: {e}")
raise RuntimeError(f"Erreur inattendue avec l'API Romeo: {e}")
logger.error(f"Erreur inattendue lors de l'appel à l'API Romeo: {e}")
raise RuntimeError(f"Erreur inattendue lors de l'appel à l'API Romeo: {e}")
async def predict_metiers(self, text: str) -> List[Dict[str, Any]]:
"""
Prédit les métiers ROME à partir d'un texte donné.
Retourne une liste de dictionnaires avec les détails des prédictions métiers.
"""
if not text:
return [] # Retourne une liste vide si le texte est vide
response_data = await self._call_api("/predictionMetiers", text)
# Romeo renvoie souvent une liste directe de prédictions si successful
return response_data if response_data is not None else []
endpoint = "/predire/metiers"
data = {"texte": text}
response_data = await self._call_api(endpoint, data)
return response_data.get("predictions", [])
async def predict_competences(self, text: str) -> List[Dict[str, Any]]:
"""
Prédit les compétences ROME à partir d'un texte donné.
Retourne une liste de dictionnaires avec les détails des prédictions de compétences.
"""
if not text:
return [] # Retourne une liste vide si le texte est vide
response_data = await self._call_api("/predictionCompetences", text)
# Romeo renvoie souvent une liste directe de prédictions si successful
return response_data if response_data is not None else []
endpoint = "/predire/competences"
data = {"texte": text}
response_data = await self._call_api(endpoint, data)
return response_data.get("predictions", [])
# Instanciation unique du service Romeo
romeo_service = RomeoService()