@@ -128,11 +128,14 @@ def _cache_file() -> str:
128
128
129
129
# Read oauth cache
130
130
def _read_cache () -> dict :
131
+ filename = _cache_file ()
132
+ if not path .exists (filename ):
133
+ return {}
131
134
try :
132
- with open (_cache_file () , 'r' ) as cache :
135
+ with open (filename , 'r' ) as cache :
133
136
return json .loads (cache .read ())
134
137
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 } " )
136
139
return {}
137
140
138
141
@@ -150,7 +153,6 @@ def _write_token_cache(creds: ClientCredentials):
150
153
try :
151
154
cache = _read_cache ()
152
155
cache [creds .client_id ] = creds .access_token
153
-
154
156
with open (_cache_file (), 'w' ) as f :
155
157
f .write (json .dumps (cache , default = vars ))
156
158
except Exception as e :
@@ -169,6 +171,13 @@ def _get_access_token(ctx: Context, url: str) -> AccessToken:
169
171
return creds .access_token .access_token
170
172
171
173
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
+
172
181
def _request_access_token (ctx : Context , url : str ) -> AccessToken :
173
182
creds = ctx .credentials
174
183
assert isinstance (creds , ClientCredentials )
@@ -195,9 +204,9 @@ def _request_access_token(ctx: Context, url: str) -> AccessToken:
195
204
)
196
205
_print_request (req )
197
206
with _urlopen_with_retry (req , ctx .retries ) as rsp :
207
+ _log_request_response (req , rsp )
198
208
result = json .loads (rsp .read ())
199
209
token = result .get (ACCESS_KEY_TOKEN_KEY , None )
200
-
201
210
if token is not None :
202
211
expires_in = result .get (EXPIRES_IN_KEY , None )
203
212
scope = result .get (SCOPE , None )
@@ -230,7 +239,7 @@ def _urlopen_with_retry(req: Request, retries: int = 0):
230
239
return urlopen (req )
231
240
except (URLError , ConnectionError ) as e :
232
241
logger .warning (f"URL/Connection error occured { req .full_url } (attempt { attempt + 1 } /{ attempts } ). Error message: { str (e )} " )
233
-
242
+
234
243
if attempt == attempts - 1 :
235
244
logger .error (f"Failed to connect to { req .full_url } after { attempts } attempt{ 's' if attempts > 1 else '' } " )
236
245
raise e
@@ -246,12 +255,7 @@ def request(ctx: Context, method: str, url: str, headers={}, data=None, **kwargs
246
255
req = _authenticate (ctx , req )
247
256
_print_request (req )
248
257
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 )
255
259
return rsp
256
260
257
261
0 commit comments