aioamazondevices 3.4.0__tar.gz → 3.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: aioamazondevices
3
- Version: 3.4.0
3
+ Version: 3.5.0
4
4
  Summary: Python library to control Amazon devices
5
5
  License: Apache-2.0
6
6
  Author: Simone Chemelli
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "aioamazondevices"
3
- version = "3.4.0"
3
+ version = "3.5.0"
4
4
  requires-python = ">=3.12"
5
5
  description = "Python library to control Amazon devices"
6
6
  authors = [
@@ -1,6 +1,6 @@
1
1
  """aioamazondevices library."""
2
2
 
3
- __version__ = "3.4.0"
3
+ __version__ = "3.5.0"
4
4
 
5
5
 
6
6
  from .api import AmazonDevice, AmazonEchoApi
@@ -1,5 +1,6 @@
1
1
  """Support for Amazon devices."""
2
2
 
3
+ import asyncio
3
4
  import base64
4
5
  import hashlib
5
6
  import mimetypes
@@ -369,15 +370,39 @@ class AmazonEchoApi:
369
370
  self._load_website_cookies() if self._login_stored_data else self._cookies
370
371
  )
371
372
  self.session.cookie_jar.update_cookies(_cookies)
372
- try:
373
- resp = await self.session.request(
374
- method,
375
- URL(url, encoded=True),
376
- data=input_data if not json_data else orjson.dumps(input_data),
377
- headers=headers,
378
- )
379
- except (TimeoutError, ClientConnectorError) as exc:
380
- raise CannotConnect(f"Connection error during {method}") from exc
373
+
374
+ resp: ClientResponse | None = None
375
+ for delay in [0, 1, 2, 5, 8, 12, 21]:
376
+ if delay:
377
+ _LOGGER.info(
378
+ "Sleeping for %s seconds before retrying API call to %s", delay, url
379
+ )
380
+ await asyncio.sleep(delay)
381
+
382
+ try:
383
+ resp = await self.session.request(
384
+ method,
385
+ URL(url, encoded=True),
386
+ data=input_data if not json_data else orjson.dumps(input_data),
387
+ headers=headers,
388
+ )
389
+
390
+ except (TimeoutError, ClientConnectorError) as exc:
391
+ _LOGGER.warning("Connection error to %s: %s", url, repr(exc))
392
+ raise CannotConnect(f"Connection error during {method}") from exc
393
+
394
+ # Retry with a delay only for specific HTTP status
395
+ # that can benefits of a back-off
396
+ if resp.status not in [
397
+ HTTPStatus.INTERNAL_SERVER_ERROR,
398
+ HTTPStatus.SERVICE_UNAVAILABLE,
399
+ HTTPStatus.TOO_MANY_REQUESTS,
400
+ ]:
401
+ break
402
+
403
+ if resp is None:
404
+ _LOGGER.error("No response received from %s", url)
405
+ raise CannotConnect(f"No response received from {url}")
381
406
 
382
407
  if not self._csrf_cookie:
383
408
  self._csrf_cookie = resp.cookies.get(CSRF_COOKIE, Morsel()).value