Skip to content
Open
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
17 changes: 13 additions & 4 deletions pynsee/utils/init_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ def init_conn(
sirene_key: Optional[str] = None,
http_proxy: Optional[str] = None,
https_proxy: Optional[str] = None,
raise_if_not_ok: bool = True,
) -> None:
"""Save your credentials to connect to INSEE APIs, subscribe to api.insee.fr

In case of failure of an API outside your current usecase (ie. failure
of SIREN API server while you are actually interested in metadata) you
can bypass this `init_conn(raise_if_not_ok=False)`

Args:
sirene_key (str, optional): user's key for sirene API
http_proxy (str, optional): Proxy server address, e.g. 'http://my_proxy_server:port'. Defaults to "".
https_proxy (str, optional): Proxy server address, e.g. 'http://my_proxy_server:port'. Defaults to "".
raise_if_not_ok (bool, optional) : If set to True, a RequestException will automatically be raised if the response is not ok (= `status_code` >= 400). The default is True.

Notes:
Environment variables can be used instead of init_conn function
Expand Down Expand Up @@ -69,7 +75,9 @@ def init_conn(
sirene_key=sirene_key, http_proxy=http_proxy, https_proxy=https_proxy
) as session:
try:
invalid_requests = session._test_connections()
invalid_requests = session._test_connections(
raise_if_not_ok=raise_if_not_ok
)
except (ValueError, requests.exceptions.RequestException):
try:
os.remove(CONFIG_FILE)
Expand All @@ -80,15 +88,16 @@ def init_conn(
if invalid_requests:
logger.error(
"Invalid credentials, the following APIs returned error codes, "
"please make sure you subscribed to them (if you need those):\n"
f"{invalid_requests}"
"please make sure you subscribed to them (if you need those):\n%s",
invalid_requests,
)
else:
logger.info(
"Subscription to all INSEE's APIs has been successfull\n"
"Unless the user wants to change the key or secret, using this "
"function is no longer needed as the credentials will be saved "
f"locally here:\n{CONFIG_FILE}"
"locally here:\n%s",
CONFIG_FILE,
)

if invalid_requests and ("Sirene" in invalid_requests):
Expand Down
36 changes: 26 additions & 10 deletions pynsee/utils/requests_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def request(
info. The default is (10, 15).
raise_if_not_ok : bool, optional
If set to True, a RequestException will automatically be raised if
the response is not ok (= `status_code` < 400).
the response is not ok (= `status_code` >= 400).
The default is True.
**kwargs :
Any other kwargs are passed directly to requests.Session.request
Expand Down Expand Up @@ -397,7 +397,7 @@ def _request_sdmx_insee(
URL to query.
raise_if_not_ok : bool, optional
If set to True, a RequestException will automatically be raised if
the response is not ok (= `status_code` < 400).
the response is not ok (= `status_code` >= 400).
The default is True.

Raises
Expand Down Expand Up @@ -438,7 +438,7 @@ def _request_api_insee(
INSEE's APIs. The default is "application/xml".
raise_if_not_ok : bool, optional
If set to True, a RequestException will automatically be raised if
the response is not ok (= `status_code` < 400).
the response is not ok (= `status_code` >= 400).
The default is False.
print_msg : bool, optional
If True, will log critical entries to warn that the call to
Expand Down Expand Up @@ -528,10 +528,17 @@ def _called_sirene(self, url: str) -> bool:
"""
return re.match(".*api-sirene.*", url) is not None

def _test_connections(self) -> dict:
def _test_connections(self, raise_if_not_ok: bool = True) -> dict:
"""
Test the valid connection to each API.

Parameters
----------
raise_if_not_ok : bool, optional
If set to True, a RequestException will automatically be raised if
the response is not ok (= `status_code` >= 400).
The default is True.

Raises
------
RequestException
Expand All @@ -545,6 +552,7 @@ def _test_connections(self) -> dict:
Dict of {"api name": response.status_code} for invalid queries.

"""

queries = {
"BDM": "https://api.insee.fr/series/BDM/dataflow/FR1/all",
"Metadata": "https://api.insee.fr/metadonnees/codes/cj/n3/5599",
Expand Down Expand Up @@ -576,19 +584,27 @@ def _test_connections(self) -> dict:
"your proxy configuration "
f"- proxies were {self.proxies}."
) from exc
elif exc.response.status_code == 404:
raise requests.exceptions.RequestException(
if exc.response.status_code >= 400:
msg = (
f"Could not reach {api} at {api_url}, the server "
"returned 404 (not found); please get in touch if "
"the issue persists."
) from exc
"returned {exc.response.status_code}; please get "
f"in touch if the issue persists."
)
if raise_if_not_ok:
raise requests.exceptions.RequestException(
msg
) from exc
logger.warning(msg)

invalid_requests[api] = exc.response.status_code

if len(invalid_requests) == len(queries):
raise ValueError(
msg = (
"No API was reached. That's strange, please get in touch if "
"the issue persists."
)
if raise_if_not_ok:
raise ValueError(msg)
logger.warning(msg)

return invalid_requests
6 changes: 5 additions & 1 deletion tests/utils/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ def patch_test_connections(func):

def wrapper(*args, **kwargs):
init = PynseeAPISession._test_connections
PynseeAPISession._test_connections = lambda x: {}

def patched_test_connections(self, raise_if_not_ok=False):
return {}

PynseeAPISession._test_connections = patched_test_connections
try:
func(*args, **kwargs)
except Exception:
Expand Down
Loading