1- import asyncio
21import datetime
32import json
43import logging
@@ -31,7 +30,7 @@ def __init__(
3130 self .__configuration = configuration
3231 self .__login_client = SaicLoginClient (configuration , listener = listener )
3332 self .__api_client = SaicApiClient (configuration , listener = listener )
34- self .__token_expiration = None
33+ self .__token_expiration : Optional [ datetime . datetime ] = None
3534
3635 @property
3736 def configuration (self ) -> SaicApiConfiguration :
@@ -137,12 +136,9 @@ async def deserialize(
137136 error_message = json_data .get ('message' , 'Unknown error' )
138137 logger .debug (f"Response code: { return_code } { response .text } " )
139138
140- if return_code == 401 :
141- await self ._handle_logout (
142- error_message = error_message ,
143- return_code = return_code ,
144- response = response ,
145- )
139+ if return_code in (401 , 403 ) or response .status_code in (401 , 403 ):
140+ self .logout ()
141+ raise SaicLogoutException (response .text , return_code )
146142
147143 if return_code in (2 , 3 , 7 ):
148144 logger .error (f"API call return code is not acceptable: { return_code } : { response .text } " )
@@ -197,21 +193,14 @@ async def deserialize(
197193 else :
198194 raise SaicApiException (f"Failed to deserialize response: { e } . Original json was { response .text } " ) from e
199195
200- async def _handle_logout (self , * , error_message : str , return_code : int , response : httpx .Response ):
201- logger .error (f"API client got de-authenticated. { return_code } : { response .text } " )
202- self .logout ()
203- relogin_delay = self .__configuration .relogin_delay
204- if relogin_delay :
205- logger .warning (f"Waiting { relogin_delay } s since we got logged out." )
206- await asyncio .sleep (relogin_delay )
207- logger .warning ("Logging in since we got logged out" )
208- await self .login ()
209- raise SaicApiException (error_message , return_code = return_code )
210-
211196 def logout (self ):
212197 self .api_client .user_token = None
213198 self .__token_expiration = None
214199
200+ def is_logged_in (self ) -> bool :
201+ return self .__token_expiration is not None \
202+ and self .__token_expiration > datetime .datetime .now ()
203+
215204
216205def saic_api_after_retry (retry_state ):
217206 wrapped_exception = retry_state .outcome .exception ()
@@ -231,10 +220,10 @@ def saic_api_retry_policy(retry_state):
231220 logger .debug ("Retrying since we got SaicApiRetryException" )
232221 return True
233222 elif isinstance (wrapped_exception , SaicLogoutException ):
234- logger .error ("Retrying since we got logged out" )
235- return True
223+ logger .error ("Not retrying since we got logged out" )
224+ return False
236225 elif isinstance (wrapped_exception , SaicApiException ):
237- logger .error ("NOT Retrying since we got a generic exception" )
226+ logger .error ("Not retrying since we got a generic exception" )
238227 return False
239228 else :
240229 logger .error (f"Not retrying { retry_state .args } { wrapped_exception } " )
0 commit comments