Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gère les erreurs 5XX de l'API externe ratios financiers #97

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions impact/api/ratios_financiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import requests
import sentry_sdk

from api.exceptions import API_ERROR_SENTRY_MESSAGE
from api.exceptions import APIError
from api.exceptions import ServerError
from entreprises.models import CaracteristiquesAnnuelles

NOM_API = "ratios financiers"
RATIOS_FINANCIERS_TIMEOUT = 10


Expand All @@ -27,24 +30,27 @@ def dernier_exercice_comptable(siren):
sentry_sdk.capture_exception(e)
raise APIError()

try:
record = response.json()["records"][0]
fields = record["fields"]
donnees_financieres["date_cloture_exercice"] = _extrait_date_cloture_exercice(
fields
)
donnees_financieres.update(_extrait_chiffre_affaires(fields))
except IndexError:
pass

try:
record = response.json()["records"][1]
fields = record["fields"]
date_cloture_exercice = _extrait_date_cloture_exercice(fields)
if date_cloture_exercice == donnees_financieres["date_cloture_exercice"]:
if response.status_code == 200:
try:
record = response.json()["records"][0]
fields = record["fields"]
donnees_financieres[
"date_cloture_exercice"
] = _extrait_date_cloture_exercice(fields)
donnees_financieres.update(_extrait_chiffre_affaires(fields))
except IndexError:
pass
except IndexError:
pass
try:
record = response.json()["records"][1]
fields = record["fields"]
date_cloture_exercice = _extrait_date_cloture_exercice(fields)
if date_cloture_exercice == donnees_financieres["date_cloture_exercice"]:
donnees_financieres.update(_extrait_chiffre_affaires(fields))
except IndexError:
pass
else:
sentry_sdk.capture_message(API_ERROR_SENTRY_MESSAGE.format(NOM_API))
raise ServerError()
return donnees_financieres


Expand Down
11 changes: 11 additions & 0 deletions impact/api/tests/test_ratios_financiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from requests.exceptions import Timeout

from api.exceptions import APIError
from api.exceptions import ServerError
from api.ratios_financiers import dernier_exercice_comptable
from api.ratios_financiers import RATIOS_FINANCIERS_TIMEOUT
from api.tests import MockedResponse
Expand Down Expand Up @@ -123,3 +124,13 @@ def test_echec_ratio_financiers_exception_provoquee_par_l_api(mocker):
capture_exception_mock.assert_called_once()
args, _ = capture_exception_mock.call_args
assert type(args[0]) == Timeout


def test_echec_erreur_de_l_API(mocker):
mocker.patch("requests.get", return_value=MockedResponse(500))
capture_message_mock = mocker.patch("sentry_sdk.capture_message")

with pytest.raises(ServerError) as e:
dernier_exercice_comptable(SIREN)

capture_message_mock.assert_called_once_with("Erreur API ratios financiers")
Loading