diff --git a/sync/clients/sync.py b/sync/clients/sync.py index 1ae8567..2c41bc0 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