Skip to content

Commit aab54da

Browse files
authored
chore: remove unnecessary checks and cleanup error handling (#284)
- properly raise 401 errors in SIRENE calls - more consistent handling of errors and 404 in `get_sirene_relatives` - remove config file check in `_get_credentials_from_configfile` (checks are done in PynseeAPISession._request_api_insee) - cleanup error handling in PynseeAPISession._request_api_insee
1 parent feba987 commit aab54da

7 files changed

Lines changed: 134 additions & 165 deletions

File tree

pynsee/sirene/_request_sirene.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def _request_sirene(query, kind, number=1001):
3838
if number > number_query_limit:
3939
link = link + "&curseur=*"
4040

41-
with PynseeAPISession(url=link) as session:
41+
with PynseeAPISession() as session:
4242
request = session.request_insee(
4343
api_url=link,
4444
file_format="application/json;charset=utf-8",
@@ -84,7 +84,7 @@ def _request_sirene(query, kind, number=1001):
8484
+ following_cursor
8585
)
8686

87-
with PynseeAPISession(url=new_query) as session:
87+
with PynseeAPISession() as session:
8888
request_new = session.request_insee(
8989
api_url=new_query,
9090
file_format="application/json;charset=utf-8",

pynsee/sirene/get_sirene_data.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
# -*- coding: utf-8 -*-
22
# Copyright : INSEE, 2021
33

4-
import pandas as pd
5-
from functools import lru_cache
4+
import logging
65
import re
6+
from functools import lru_cache
7+
8+
import pandas as pd
9+
from requests import RequestException
710

811
from pynsee.utils.requests_session import PynseeAPISession
912
from pynsee.utils._make_dataframe_from_dict import _make_dataframe_from_dict
1013
from pynsee.utils.HiddenPrints import HiddenPrints
1114

1215
from .sirenedataframe import SireneDataFrame
1316

14-
import logging
15-
1617

1718
logger = logging.getLogger(__name__)
1819

@@ -51,21 +52,19 @@ def get_sirene_data(*id):
5152

5253
list_data = []
5354

54-
for i in range(len(list_ids)):
55+
for sid in list_ids:
5556
for kind in ["siret", "siren"]:
5657
if kind == "siren":
5758
main_key = "uniteLegale"
5859
elif kind == "siret":
5960
main_key = "etablissement"
6061

6162
INSEE_api_sirene = "https://api.insee.fr/api-sirene/3.11/" + kind
62-
link = (
63-
INSEE_api_sirene + "/" + re.sub(r"\s+", "", str(list_ids[i]))
64-
)
63+
link = INSEE_api_sirene + "/" + re.sub(r"\s+", "", str(sid))
6564

6665
try:
6766
with HiddenPrints():
68-
with PynseeAPISession(url=link) as session:
67+
with PynseeAPISession() as session:
6968
request = session.request_insee(
7069
api_url=link,
7170
file_format="application/json;charset=utf-8",
@@ -85,19 +84,20 @@ def get_sirene_data(*id):
8584
data = data_request[main_key]
8685

8786
data_final = _make_dataframe_from_dict(data)
87+
except RequestException as e:
88+
if e.response.status_code == 401:
89+
raise
8890
except Exception:
8991
pass
9092
else:
9193
list_data.append(data_final)
9294
break
9395

94-
if len(list_data) > 0:
96+
if list_data:
9597
data_final = pd.concat(list_data).reset_index(drop=True)
96-
else:
97-
raise ValueError("!!! No data found for the provided identifiers !!!")
9898

99-
_warning_get_data()
99+
_warning_get_data()
100100

101-
SireneDF = SireneDataFrame(data_final)
101+
return SireneDataFrame(data_final)
102102

103-
return SireneDF
103+
raise ValueError("!!! No data found for the provided identifiers !!!")

pynsee/sirene/get_sirene_relatives.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import pandas as pd
22
import re
33

4+
from requests import RequestException
5+
46
from pynsee.utils.requests_session import PynseeAPISession
57
from pynsee.utils._make_dataframe_from_dict import _make_dataframe_from_dict
6-
from pynsee.utils.HiddenPrints import HiddenPrints
78
from .sirenedataframe import SireneDataFrame
89

910

@@ -24,7 +25,6 @@ def get_sirene_relatives(*siret):
2425
>>> data = get_sirene_relatives('00555008200027')
2526
>>> data = get_sirene_relatives(['39860733300059', '00555008200027'])
2627
"""
27-
2828
list_siret = []
2929

3030
for id in range(len(siret)):
@@ -40,38 +40,44 @@ def get_sirene_relatives(*siret):
4040
types = ["siretEtablissementPredecesseur", "siretEtablissementSuccesseur"]
4141
list_df = []
4242

43-
for s in range(len(list_siret)):
44-
for i in range(len(types)):
45-
46-
criteria = types[i] + ":" + re.sub(r"\s+", "", list_siret[s])
43+
for s in list_siret:
44+
for t in types:
45+
criteria = t + ":" + re.sub(r"\s+", "", s)
4746
query = (
4847
"https://api.insee.fr/api-sirene/3.11/siret/liensSuccession"
4948
f"?q={criteria}"
5049
)
50+
5151
try:
52-
with HiddenPrints():
53-
with PynseeAPISession(url=query) as session:
54-
result = session.request_insee(
55-
api_url=query,
56-
file_format="application/json;charset=utf-8",
57-
raise_if_not_ok=True,
58-
print_msg=False,
59-
)
60-
61-
json = result.json()
52+
with PynseeAPISession() as session:
53+
result = session.request_insee(
54+
api_url=query,
55+
file_format="application/json;charset=utf-8",
56+
raise_if_not_ok=True,
57+
print_msg=False,
58+
)
59+
60+
json = result.json()
61+
except RequestException as e:
62+
if e.response.status_code == 401:
63+
raise
6264
except Exception:
6365
pass
6466
else:
6567
list_df += [_make_dataframe_from_dict(json)]
6668

67-
if len(list_df) > 0:
68-
df = SireneDataFrame(pd.concat(list_df).reset_index(drop=True))
69-
70-
for c in ["statut", "message", "nombre", "total", "debut"]:
71-
if c in df.columns:
72-
del df[c]
69+
if list_df:
70+
df = SireneDataFrame(
71+
pd.concat(list_df)
72+
.drop(
73+
columns=["statut", "message", "nombre", "total", "debut"],
74+
errors="ignore",
75+
)
76+
.reset_index(drop=True)
77+
)
7378

74-
return df
79+
if df.columns.any():
80+
return df
7581

7682
raise ValueError(
7783
"Neither parent nor child entities were found for any entity"

pynsee/utils/_get_credentials.py

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,25 @@
33

44
import json
55
import logging
6-
import re
7-
8-
from functools import lru_cache
9-
from typing import Dict
106

117
from pynsee.constants import CONFIG_FILE
128

139

1410
logger = logging.getLogger(__name__)
1511

1612

17-
def _get_credentials_from_configfile(url) -> Dict[str, str]:
13+
def _get_credentials_from_configfile() -> dict[str, str]:
1814
"""
1915
Try to load credentials and proxy configuration from config file.
2016
2117
Returns a dict containing at least an "sirene_key" entry.
2218
"""
23-
key_dict: Dict[str, str] = {}
19+
key_dict: dict[str, str] = {}
2420

2521
try:
2622
with open(CONFIG_FILE, "r") as f:
2723
key_dict = json.load(f)
28-
2924
except FileNotFoundError:
30-
if re.match(".*api-sirene.*", url):
31-
# no credentials/config stored
32-
_missing_credentials()
33-
return key_dict
25+
pass
3426

3527
return key_dict
36-
37-
38-
@lru_cache(maxsize=None)
39-
def _missing_credentials() -> None:
40-
logger.critical(
41-
"INSEE API credentials have not been found: please try to reuse "
42-
"pynsee.init_conn to save them locally.\n"
43-
"Otherwise, you can still use environment variables as follow:\n\n"
44-
"import os\n"
45-
"os.environ['sirene_key'] = 'my_sirene_key'"
46-
)

0 commit comments

Comments
 (0)