From 936b55ec82416d0e82a394db35878bf4c87e2e06 Mon Sep 17 00:00:00 2001 From: Samantha Hughes Date: Tue, 12 Nov 2024 17:26:50 -0800 Subject: [PATCH 1/2] avoid refreshing token each request --- sync/clients/sync.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/sync/clients/sync.py b/sync/clients/sync.py index 1ae8567..37eeb1e 100644 --- a/sync/clients/sync.py +++ b/sync/clients/sync.py @@ -58,16 +58,17 @@ def __init__( ) def auth_flow(self, request: httpx.Request) -> Generator[httpx.Request, httpx.Response, None]: with self._sync_lock: - # fetch with retry and exponential backoff - response = yield self.build_auth_request() - if response.status_code == httpx.codes.OK: - self.update_access_token(response) - elif response.status_code in self.retryable_status_codes: + if not self.cached_token.is_access_token_valid: + # fetch with retry and exponential backoff + response = yield self.build_auth_request() + if response.status_code == httpx.codes.OK: + self.update_access_token(response) + elif response.status_code in self.retryable_status_codes: + raise TryAgain() + else: + logger.error(f"{response.status_code}: Failed to authenticate") + if not self.cached_token.is_access_token_valid: raise TryAgain() - else: - logger.error(f"{response.status_code}: Failed to authenticate") - if not self.cached_token.is_access_token_valid: - raise TryAgain() request.headers["Authorization"] = f"Bearer {self.cached_token.access_token}" yield request @@ -100,6 +101,7 @@ async def async_auth_flow( yield request def build_auth_request(self) -> httpx.Request: + print(self.api_key.dict(by_alias=True)) return httpx.Request("POST", self.auth_url, json=self.api_key.dict(by_alias=True)) def update_access_token(self, response: httpx.Response): From c197f94b28268ca06fb61094a0c8770b5103183a Mon Sep 17 00:00:00 2001 From: Samantha Hughes Date: Tue, 12 Nov 2024 17:36:51 -0800 Subject: [PATCH 2/2] Update sync/clients/sync.py --- sync/clients/sync.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sync/clients/sync.py b/sync/clients/sync.py index 37eeb1e..2c41bc0 100644 --- a/sync/clients/sync.py +++ b/sync/clients/sync.py @@ -101,7 +101,6 @@ async def async_auth_flow( yield request def build_auth_request(self) -> httpx.Request: - print(self.api_key.dict(by_alias=True)) return httpx.Request("POST", self.auth_url, json=self.api_key.dict(by_alias=True)) def update_access_token(self, response: httpx.Response):