diff --git a/src/sumo/wrapper/_auth_provider.py b/src/sumo/wrapper/_auth_provider.py index 0093126..de493d7 100644 --- a/src/sumo/wrapper/_auth_provider.py +++ b/src/sumo/wrapper/_auth_provider.py @@ -9,6 +9,7 @@ from pathlib import Path from urllib.parse import parse_qs +import httpx import jwt import msal import tenacity as tn @@ -90,6 +91,14 @@ def store_shared_access_key_for_case(self, case_uuid, token): f.write(token) protect_token_cache(self._resource_id, ".sharedkey", case_uuid) + def _store_fallback_auth(self, sumo_client): + sync_client = sumo_client._client + if sync_client is None: + sumo_client._client = httpx.Client() + token = sumo_client.get("/createfallbackauth").text + self.store_shared_access_key_for_case("fallback", token) + sumo_client._client = sync_client + def store_fallback_auth(self, sumo_client): return @@ -119,8 +128,7 @@ def __init__(self, client_id, authority, resource_id): self._scope = scope_for_resource(resource_id) def store_fallback_auth(self, sumo_client): - token = sumo_client.get("/createfallbackauth").text - self.store_shared_access_key_for_case("fallback", token) + self._store_fallback_auth(sumo_client) class AuthProviderAccessToken(AuthProvider): @@ -276,8 +284,7 @@ def login(self): return def store_fallback_auth(self, sumo_client): - token = sumo_client.get("/createfallbackauth").text - self.store_shared_access_key_for_case("fallback", token) + self._store_fallback_auth(sumo_client) class AuthProviderDeviceCode(AuthProvider): diff --git a/src/sumo/wrapper/sumo_client.py b/src/sumo/wrapper/sumo_client.py index 9439d90..ee83e9d 100644 --- a/src/sumo/wrapper/sumo_client.py +++ b/src/sumo/wrapper/sumo_client.py @@ -149,13 +149,11 @@ def _get(): self.base_url = base_url - sync_client = self._client - if sync_client is None: - self._client = httpx.Client() - self.auth.store_fallback_auth(self) - self._client = sync_client + with contextlib.suppress(Exception): + self.auth.store_fallback_auth(self) def __enter__(self): + return self def __exit__(self, *_):