Skip to content

Commit

Permalink
Fix: App is blocked when onisep is down (#23)
Browse files Browse the repository at this point in the history
fix: remove settings from `__init__.py`
  • Loading branch information
Angel-Dijoux authored Jan 1, 2024
1 parent f8945ae commit 4e4362e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
20 changes: 0 additions & 20 deletions src/business_logic/formation/__init__.py
Original file line number Diff line number Diff line change
@@ -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(),
}
20 changes: 18 additions & 2 deletions src/business_logic/formation/scrap/utils/get_onisep_data.py
Original file line number Diff line number Diff line change
@@ -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"


Expand Down
40 changes: 33 additions & 7 deletions src/business_logic/formation/scrap/utils/get_onisep_token.py
Original file line number Diff line number Diff line change
@@ -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]:
Expand All @@ -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

0 comments on commit 4e4362e

Please sign in to comment.