@@ -67,9 +67,7 @@ def token_seems_valid(token: str) -> bool:
67
67
Returns:
68
68
bool: True if ``token`` seems valid
69
69
"""
70
- return (
71
- (token is not None ) and token .isascii () and token .isprintable () and (320 < len (token ))
72
- )
70
+ return (token is not None ) and token .isascii () and token .isprintable () and (320 < len (token ))
73
71
74
72
75
73
def find_token () -> str | None :
@@ -228,76 +226,79 @@ async def call(
228
226
229
227
response_data = None
230
228
231
- session = self ._session if self ._session else aiohttp .ClientSession ()
232
-
233
229
try :
234
- with async_timeout .timeout (self ._api_timeout ):
235
- headers = self ._generate_headers ()
230
+ session = (
231
+ self ._session
232
+ if self ._session
233
+ else aiohttp .ClientSession (timeout = aiohttp .ClientTimeout (total = self ._api_timeout ))
234
+ )
236
235
237
- # use etag if available
238
- if resource in self ._etags :
239
- headers [ETAG ] = str (self ._etags .get (resource ))
240
- # logger.debug("🐾 \x1b[38;2;255;26;102m·\x1b[0m etag: %s", headers[ETAG])
236
+ headers = self ._generate_headers ()
241
237
242
- await session .options (resource , headers = headers )
243
- response : aiohttp .ClientResponse = await session .request (
244
- method , resource , headers = headers , json = data
245
- )
238
+ # use etag if available
239
+ if resource in self ._etags :
240
+ headers [ETAG ] = str (self ._etags .get (resource ))
241
+ # logger.debug("🐾 \x1b[38;2;255;26;102m·\x1b[0m etag: %s", headers[ETAG])
242
+
243
+ await session .options (resource , headers = headers )
244
+ response : aiohttp .ClientResponse = await session .request (
245
+ method , resource , headers = headers , json = data , timeout = self ._api_timeout
246
+ )
246
247
247
- if response .status == HTTPStatus .OK or response .status == HTTPStatus .CREATED :
248
- self .resources [resource ] = response_data = await response .json ()
249
-
250
- if ETAG in response .headers :
251
- self ._etags [resource ] = response .headers [ETAG ].strip ('"' )
252
-
253
- elif response .status == HTTPStatus .NOT_MODIFIED :
254
- # Etag header matched, no new data available
255
- logger .debug (
256
- "🐾 \x1b [38;2;0;255;0m·\x1b [0m %d: etag matched - no new data available" ,
257
- response .status ,
258
- )
259
-
260
- elif response .status == HTTPStatus .UNAUTHORIZED :
261
- logger .error (
262
- "🐾 \x1b [38;2;255;26;102m·\x1b [0m %s %s: %d | %s" ,
263
- method ,
264
- resource .replace ("https://" , "" ),
265
- response .status ,
266
- response ,
267
- )
268
- self ._auth_token = None
269
- if not second_try :
270
- token_refreshed = await self .get_token ()
271
- if token_refreshed :
272
- await self .call (method = "GET" , resource = resource , second_try = True )
273
-
274
- raise SurePetcareAuthenticationError ()
275
-
276
- else :
277
- logger .info (
278
- "🐾 \x1b [38;2;255;0;255m·\x1b [0m %s %s: %d | %s" ,
279
- method ,
280
- resource .replace ("https://" , "" ),
281
- response .status ,
282
- response ,
283
- )
284
-
285
- if response_data :
286
- responselen = len (response_data .get ("data" , []))
287
- else :
288
- responselen = 0
248
+ if response .status == HTTPStatus .OK or response .status == HTTPStatus .CREATED :
249
+ self .resources [resource ] = response_data = await response .json ()
250
+
251
+ if ETAG in response .headers :
252
+ self ._etags [resource ] = response .headers [ETAG ].strip ('"' )
253
+
254
+ elif response .status == HTTPStatus .NOT_MODIFIED :
255
+ # Etag header matched, no new data available
289
256
logger .debug (
290
- "🐾 \x1b [38;2;0;255;0m·\x1b [0m %s %s | %d" ,
257
+ "🐾 \x1b [38;2;0;255;0m·\x1b [0m %d: etag matched - no new data available" ,
258
+ response .status ,
259
+ )
260
+
261
+ elif response .status == HTTPStatus .UNAUTHORIZED :
262
+ logger .error (
263
+ "🐾 \x1b [38;2;255;26;102m·\x1b [0m %s %s: %d | %s" ,
291
264
method ,
292
265
resource .replace ("https://" , "" ),
293
- responselen ,
266
+ response .status ,
267
+ response ,
294
268
)
269
+ self ._auth_token = None
270
+ if not second_try :
271
+ token_refreshed = await self .get_token ()
272
+ if token_refreshed :
273
+ await self .call (method = "GET" , resource = resource , second_try = True )
274
+
275
+ raise SurePetcareAuthenticationError ()
276
+
277
+ else :
278
+ logger .info (
279
+ "🐾 \x1b [38;2;255;0;255m·\x1b [0m %s %s: %d | %s" ,
280
+ method ,
281
+ resource .replace ("https://" , "" ),
282
+ response .status ,
283
+ response ,
284
+ )
285
+
286
+ if response_data :
287
+ responselen = len (response_data .get ("data" , []))
288
+ else :
289
+ responselen = 0
290
+ logger .debug (
291
+ "🐾 \x1b [38;2;0;255;0m·\x1b [0m %s %s | %d" ,
292
+ method ,
293
+ resource .replace ("https://" , "" ),
294
+ responselen ,
295
+ )
295
296
296
- if method == "DELETE" and response .status == HTTPStatus .NO_CONTENT :
297
- # TODO: this does not return any data, is there a better way?
298
- return "DELETE 204 No Content"
297
+ if method == "DELETE" and response .status == HTTPStatus .NO_CONTENT :
298
+ # TODO: this does not return any data, is there a better way?
299
+ return "DELETE 204 No Content"
299
300
300
- return response_data
301
+ return response_data
301
302
302
303
except (asyncio .TimeoutError , aiohttp .ClientError ) as error :
303
304
logger .error ("Can not load data from %s" , resource )
0 commit comments