@@ -128,11 +128,14 @@ def _cache_file() -> str:
128128
129129# Read oauth cache
130130def _read_cache () -> dict :
131+ filename = _cache_file ()
132+ if not path .exists (filename ):
133+ return {}
131134 try :
132- with open (_cache_file () , 'r' ) as cache :
135+ with open (filename , 'r' ) as cache :
133136 return json .loads (cache .read ())
134137 except Exception as e :
135- logger .warning ( f'Failed to read token cache { _cache_file () } : { e } ' )
138+ logger .error ( f"can't read token cache { filename } : { e } " )
136139 return {}
137140
138141
@@ -150,7 +153,6 @@ def _write_token_cache(creds: ClientCredentials):
150153 try :
151154 cache = _read_cache ()
152155 cache [creds .client_id ] = creds .access_token
153-
154156 with open (_cache_file (), 'w' ) as f :
155157 f .write (json .dumps (cache , default = vars ))
156158 except Exception as e :
@@ -169,6 +171,13 @@ def _get_access_token(ctx: Context, url: str) -> AccessToken:
169171 return creds .access_token .access_token
170172
171173
174+ def _log_request_response (req , rsp ):
175+ content_type = req .headers ["Content-Type" ] if "Content-Type" in req .headers else ""
176+ agent = req .headers ["User-Agent" ] if "User-Agent" in req .headers else ""
177+ request_id = rsp .headers ["X-Request-ID" ] if "X-Request-ID" in rsp .headers else ""
178+ logger .debug (f"{ rsp ._method } HTTP/{ rsp .version } { content_type } { rsp .url } { rsp .status } { agent } { request_id } " )
179+
180+
172181def _request_access_token (ctx : Context , url : str ) -> AccessToken :
173182 creds = ctx .credentials
174183 assert isinstance (creds , ClientCredentials )
@@ -195,9 +204,9 @@ def _request_access_token(ctx: Context, url: str) -> AccessToken:
195204 )
196205 _print_request (req )
197206 with _urlopen_with_retry (req , ctx .retries ) as rsp :
207+ _log_request_response (req , rsp )
198208 result = json .loads (rsp .read ())
199209 token = result .get (ACCESS_KEY_TOKEN_KEY , None )
200-
201210 if token is not None :
202211 expires_in = result .get (EXPIRES_IN_KEY , None )
203212 scope = result .get (SCOPE , None )
@@ -230,7 +239,7 @@ def _urlopen_with_retry(req: Request, retries: int = 0):
230239 return urlopen (req )
231240 except (URLError , ConnectionError ) as e :
232241 logger .warning (f"URL/Connection error occured { req .full_url } (attempt { attempt + 1 } /{ attempts } ). Error message: { str (e )} " )
233-
242+
234243 if attempt == attempts - 1 :
235244 logger .error (f"Failed to connect to { req .full_url } after { attempts } attempt{ 's' if attempts > 1 else '' } " )
236245 raise e
@@ -246,12 +255,7 @@ def request(ctx: Context, method: str, url: str, headers={}, data=None, **kwargs
246255 req = _authenticate (ctx , req )
247256 _print_request (req )
248257 rsp = _urlopen_with_retry (req , ctx .retries )
249-
250- # logging
251- content_type = headers ["Content-Type" ] if "Content-Type" in headers else ""
252- agent = headers ["User-Agent" ] if "User-Agent" in headers else ""
253- request_id = rsp .headers ["X-Request-ID" ] if "X-Request-ID" in rsp .headers else ""
254- logger .debug (f"{ rsp ._method } HTTP/{ rsp .version } { content_type } { rsp .url } { rsp .status } { agent } { request_id } " )
258+ _log_request_response (req , rsp )
255259 return rsp
256260
257261
0 commit comments