diff --git a/src/business_logic/formation/__init__.py b/src/business_logic/formation/__init__.py index 7023a4b..e69de29 100644 --- a/src/business_logic/formation/__init__.py +++ b/src/business_logic/formation/__init__.py @@ -1,20 +0,0 @@ -from enum import Enum -import os - -from src.business_logic.formation.scrap.utils.get_onisep_token import ( - BearerToken, - get_token, -) - - -class HeaderKey(Enum): - APPLICATION_ID = "Application-ID" - AUTHORIZATION = "Authorization" - - -ONISEP_URL = "https://api.opendata.onisep.fr/api/1.0/dataset/" - -HEADERS: dict[HeaderKey, BearerToken | str] = { - HeaderKey.APPLICATION_ID.value: os.environ.get("ONISEP_APP_ID"), - HeaderKey.AUTHORIZATION.value: get_token(), -} diff --git a/src/business_logic/formation/scrap/utils/get_onisep_data.py b/src/business_logic/formation/scrap/utils/get_onisep_data.py index 2d76e5e..91ac1c3 100644 --- a/src/business_logic/formation/scrap/utils/get_onisep_data.py +++ b/src/business_logic/formation/scrap/utils/get_onisep_data.py @@ -1,15 +1,31 @@ # Idéo-Formations initiales en France # https://opendata.onisep.fr/data/5fa591127f501/2-ideo-formations-initiales-en-france.htm +from enum import Enum +import os import requests -from src.business_logic.formation import HEADERS, ONISEP_URL, HeaderKey from src.business_logic.formation.exceptions import NoOnisepAPIException -from src.business_logic.formation.scrap.utils.get_onisep_token import get_token +from src.business_logic.formation.scrap.utils.get_onisep_token import ( + BearerToken, + get_token, +) from src.constants.http_status_codes import ( HTTP_200_OK, HTTP_401_UNAUTHORIZED, ) +class HeaderKey(Enum): + APPLICATION_ID = "Application-ID" + AUTHORIZATION = "Authorization" + + +ONISEP_URL = "https://api.opendata.onisep.fr/api/1.0/dataset/" + +HEADERS: dict[HeaderKey, BearerToken | str] = { + HeaderKey.APPLICATION_ID.value: os.environ.get("ONISEP_APP_ID"), + HeaderKey.AUTHORIZATION.value: get_token(), +} + DATASET = "5fa591127f501" diff --git a/src/business_logic/formation/scrap/utils/get_onisep_token.py b/src/business_logic/formation/scrap/utils/get_onisep_token.py index 501dcfa..dcb9402 100644 --- a/src/business_logic/formation/scrap/utils/get_onisep_token.py +++ b/src/business_logic/formation/scrap/utils/get_onisep_token.py @@ -1,12 +1,15 @@ import os from typing import Literal import requests +from urllib3 import Retry from src.business_logic.formation.exceptions import NoOnisepAPIException from src.constants.http_status_codes import HTTP_200_OK - +from requests.adapters import HTTPAdapter URL = "https://api.opendata.onisep.fr/api/1.0/login" +RETRY = 3 +ERROR_STATUS = [500, 502, 503, 504] def _get_form_data() -> dict[str, str]: @@ -16,14 +19,37 @@ def _get_form_data() -> dict[str, str]: } +def _send_request(session: requests.Session) -> requests.Response: + response = session.post(URL, data=_get_form_data()) + return response + + BearerToken = Literal["Bearer"] def get_token() -> BearerToken: - response = requests.post(URL, data=_get_form_data()) - if response.status_code == HTTP_200_OK: - data = response.json() - return f"Bearer {data.get('token')}" - raise NoOnisepAPIException( - f"Failed to get onisep token. Status code: {response.status_code}" + session = create_retry_session() + for _ in range(RETRY): + response = _send_request(session) + if response.status_code == HTTP_200_OK: + data = response.json() + return f"Bearer {data.get('token')}" + elif response.status_code in ERROR_STATUS: + pass + else: + raise NoOnisepAPIException( + f"Failed to get onisep token. Status code: {response.status_code}" + ) + raise NoOnisepAPIException("Maximum number of retry attempts reached.") + + +def create_retry_session() -> requests.Session: + session = requests.Session() + retry_strategy = Retry( + total=RETRY, + backoff_factor=1, + status_forcelist=ERROR_STATUS, ) + adapter = HTTPAdapter(max_retries=retry_strategy) + session.mount("https://", adapter) + return session