aioamazondevices 3.0.4__tar.gz → 3.0.5__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.0.4
3
+ Version: 3.0.5
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
  [tool.poetry]
2
2
  name = "aioamazondevices"
3
- version = "3.0.4"
3
+ version = "3.0.5"
4
4
  description = "Python library to control Amazon devices"
5
5
  authors = ["Simone Chemelli <simone.chemelli@gmail.com>"]
6
6
  license = "Apache-2.0"
@@ -1,6 +1,6 @@
1
1
  """aioamazondevices library."""
2
2
 
3
- __version__ = "3.0.4"
3
+ __version__ = "3.0.5"
4
4
 
5
5
 
6
6
  from .api import AmazonDevice, AmazonEchoApi
@@ -9,7 +9,7 @@ from dataclasses import dataclass
9
9
  from datetime import UTC, datetime, timedelta
10
10
  from enum import StrEnum
11
11
  from http import HTTPStatus
12
- from http.cookies import Morsel
12
+ from http.cookies import Morsel, SimpleCookie
13
13
  from pathlib import Path
14
14
  from typing import Any, cast
15
15
  from urllib.parse import parse_qs, urlencode
@@ -19,7 +19,7 @@ from aiohttp import ClientResponse, ClientSession
19
19
  from babel import Locale
20
20
  from bs4 import BeautifulSoup, Tag
21
21
  from httpx import URL as HTTPX_URL
22
- from multidict import MultiDictProxy
22
+ from multidict import CIMultiDictProxy, MultiDictProxy
23
23
  from yarl import URL as YARL_URL
24
24
 
25
25
  from .const import (
@@ -50,7 +50,7 @@ from .exceptions import CannotAuthenticate, CannotRegisterDevice, WrongMethod
50
50
  from .httpx import HttpxClientResponseWrapper, HttpxClientSession
51
51
 
52
52
  # Values: "aiohttp", or "httpx"
53
- LIBRARY = "httpx"
53
+ LIBRARY = "aiohttp"
54
54
 
55
55
 
56
56
  @dataclass
@@ -262,6 +262,23 @@ class AmazonEchoApi:
262
262
  cookies=self._cookies,
263
263
  )
264
264
 
265
+ async def _parse_cookies_from_headers(
266
+ self, headers: CIMultiDictProxy[str]
267
+ ) -> dict[str, str]:
268
+ """Parse cookies with a value from headers."""
269
+ cookies_with_value: dict[str, str] = {}
270
+
271
+ for value in headers.getall("Set-Cookie", ()):
272
+ cookie = SimpleCookie()
273
+ cookie.load(value)
274
+
275
+ for name, morsel in cookie.items():
276
+ if morsel.value and morsel.value not in ("-", ""):
277
+ cookies_with_value[name] = morsel.value
278
+
279
+ _LOGGER.debug("Cookies from headers: %s", cookies_with_value)
280
+ return cookies_with_value
281
+
265
282
  async def _session_request(
266
283
  self,
267
284
  method: str,
@@ -289,6 +306,9 @@ class AmazonEchoApi:
289
306
  _LOGGER.debug("Adding %s to headers", json_header)
290
307
  headers.update(json_header)
291
308
 
309
+ _cookies = (
310
+ self._load_website_cookies() if self._login_stored_data else self._cookies
311
+ )
292
312
  _url: YARL_URL | str = url
293
313
  if LIBRARY == "aiohttp":
294
314
  _url = YARL_URL(url, encoded=True)
@@ -297,9 +317,11 @@ class AmazonEchoApi:
297
317
  method,
298
318
  _url,
299
319
  data=input_data if not json_data else orjson.dumps(input_data),
300
- cookies=self._load_website_cookies(),
320
+ cookies=_cookies,
301
321
  headers=headers,
302
322
  )
323
+ self._cookies.update(**await self._parse_cookies_from_headers(resp.headers))
324
+
303
325
  content_type: str = resp.headers.get("Content-Type", "")
304
326
  _LOGGER.debug(
305
327
  "Response %s for url %s with content type: %s",