python-picnic-api2 2.0.1__tar.gz → 2.0.3__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.
Files changed (58) hide show
  1. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/.github/workflows/release.yml +1 -1
  2. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/PKG-INFO +9 -3
  3. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/README.md +6 -0
  4. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/integration_tests/test_client.py +2 -2
  5. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/pyproject.toml +5 -5
  6. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/src/python_picnic_api2/__init__.py +3 -2
  7. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/src/python_picnic_api2/client.py +51 -17
  8. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/src/python_picnic_api2/exceptions.py +27 -0
  9. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/test_client.py +62 -5
  10. python_picnic_api2-2.0.3/uv.lock +395 -0
  11. python_picnic_api2-2.0.1/uv.lock +0 -364
  12. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/.devcontainer/devcontainer.json +0 -0
  13. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/.env.example +0 -0
  14. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/.envrc +0 -0
  15. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/.github/ISSUE_TEMPLATE/bug_report.yml +0 -0
  16. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/.github/ISSUE_TEMPLATE/config.yml +0 -0
  17. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/.github/dependabot.yml +0 -0
  18. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/.github/release.yml +0 -0
  19. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/.github/workflows/ci.yaml +0 -0
  20. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/.github/workflows/it.yaml +0 -0
  21. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/.gitignore +0 -0
  22. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/LICENSE.md +0 -0
  23. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/codecov.yml +0 -0
  24. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/flake.lock +0 -0
  25. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/flake.nix +0 -0
  26. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/integration_tests/__init__.py +0 -0
  27. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/integration_tests/test_helper.py +0 -0
  28. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/integration_tests/test_session.py +0 -0
  29. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/scripts/capture_fixtures.py +0 -0
  30. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/src/python_picnic_api2/helper.py +0 -0
  31. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/__init__.py +0 -0
  32. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/article.py +0 -0
  33. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/base.py +0 -0
  34. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/cart.py +0 -0
  35. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/category.py +0 -0
  36. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/common.py +0 -0
  37. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/delivery.py +0 -0
  38. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/pml.py +0 -0
  39. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/search.py +0 -0
  40. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/user.py +0 -0
  41. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/src/python_picnic_api2/session.py +0 -0
  42. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/__init__.py +0 -0
  43. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/fixtures/cart_de.json +0 -0
  44. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/fixtures/category_page_nl.json +0 -0
  45. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/fixtures/current_deliveries_de.json +0 -0
  46. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/fixtures/delivery_de.json +0 -0
  47. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/fixtures/delivery_slots_de.json +0 -0
  48. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/fixtures/product_details_bundle_de.json +0 -0
  49. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/fixtures/product_details_multipack_nl.json +0 -0
  50. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/fixtures/product_details_page_de.json +0 -0
  51. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/fixtures/product_details_page_nl.json +0 -0
  52. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/fixtures/search_page_de.json +0 -0
  53. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/fixtures/search_page_nl.json +0 -0
  54. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/fixtures/user_de.json +0 -0
  55. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/test_fixtures.py +0 -0
  56. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/test_models.py +0 -0
  57. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/test_pml.py +0 -0
  58. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.3}/tests/test_session.py +0 -0
@@ -46,4 +46,4 @@ jobs:
46
46
  path: dist/
47
47
 
48
48
  - name: Publish release distributions to PyPI
49
- uses: pypa/gh-action-pypi-publish@v1.14.2
49
+ uses: pypa/gh-action-pypi-publish@v1.14.3
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: python-picnic-api2
3
- Version: 2.0.1
3
+ Version: 2.0.3
4
4
  Project-URL: homepage, https://github.com/codesalatdev/python-picnic-api
5
5
  Project-URL: repository, https://github.com/codesalatdev/python-picnic-api
6
6
  Author-email: Mike Brink <mjh.brink@icloud.com>, CodeSalat <pypi@codesalat.dev>
@@ -8,7 +8,7 @@ Maintainer-email: CodeSalat <pypi@codesalat.dev>
8
8
  License: Apache-2.0
9
9
  License-File: LICENSE.md
10
10
  Requires-Python: >=3.13
11
- Requires-Dist: pydantic>=2.13.4
11
+ Requires-Dist: pydantic>=2.14.4
12
12
  Requires-Dist: requests>=2.34.2
13
13
  Requires-Dist: typing-extensions>=4.16.0
14
14
  Description-Content-Type: text/markdown
@@ -47,6 +47,12 @@ picnic = PicnicAPI(username='username', password='password', country_code="NL")
47
47
 
48
48
  The country_code parameter defaults to `NL`, but you have to change it if you live in a different country than the Netherlands (ISO 3166-1 Alpha-2). This obviously only works for countries that picnic services.
49
49
 
50
+ Picnic identifies each app install by a device ID (the `x-picnic-did` header). `PicnicAPI` generates a random one per instance. To keep the same ID across restarts, store `picnic.device_id` and pass it back in:
51
+
52
+ ```python
53
+ picnic = PicnicAPI(auth_token=token, device_id=stored_device_id)
54
+ ```
55
+
50
56
  ### Two-factor authentication (2FA)
51
57
 
52
58
  For new logins, Picnic may require two-factor authentication. When 2FA is required, logging in raises a `Picnic2FARequired` exception. You then need to request a code and verify it:
@@ -32,6 +32,12 @@ picnic = PicnicAPI(username='username', password='password', country_code="NL")
32
32
 
33
33
  The country_code parameter defaults to `NL`, but you have to change it if you live in a different country than the Netherlands (ISO 3166-1 Alpha-2). This obviously only works for countries that picnic services.
34
34
 
35
+ Picnic identifies each app install by a device ID (the `x-picnic-did` header). `PicnicAPI` generates a random one per instance. To keep the same ID across restarts, store `picnic.device_id` and pass it back in:
36
+
37
+ ```python
38
+ picnic = PicnicAPI(auth_token=token, device_id=stored_device_id)
39
+ ```
40
+
35
41
  ### Two-factor authentication (2FA)
36
42
 
37
43
  For new logins, Picnic may require two-factor authentication. When 2FA is required, logging in raises a `Picnic2FARequired` exception. You then need to request a code and verify it:
@@ -59,7 +59,7 @@ def test_get_article():
59
59
  response = picnic.get_article("s1018620")
60
60
  assert isinstance(response, Article)
61
61
  assert response.id == "s1018620"
62
- assert response.name == "Gut&Günstig H-Milch 3,5%"
62
+ assert response.name == "Gut&Günstig H-Vollmilch 3,5%"
63
63
 
64
64
 
65
65
  def test_get_article_with_category_name():
@@ -72,7 +72,7 @@ def test_get_article_with_category_name():
72
72
  def test_get_article_by_gtin():
73
73
  response = picnic.get_article_by_gtin("4311501044209")
74
74
  assert response.id == "s1018620"
75
- assert response.name == "Gut&Günstig H-Milch 3,5%"
75
+ assert response.name == "Gut&Günstig H-Vollmilch 3,5%"
76
76
 
77
77
 
78
78
  def test_get_article_by_gtin_unknown():
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "python-picnic-api2"
3
- version = "2.0.1"
3
+ version = "2.0.3"
4
4
  description = ""
5
5
  readme = "README.md"
6
6
  license = {text = "Apache-2.0"}
@@ -16,7 +16,7 @@ requires-python = ">=3.13"
16
16
  dependencies = [
17
17
  "requests>=2.34.2",
18
18
  "typing_extensions>=4.16.0",
19
- "pydantic>=2.13.4"
19
+ "pydantic>=2.14.4"
20
20
  ]
21
21
 
22
22
  [tool.ruff]
@@ -44,10 +44,10 @@ select = [
44
44
 
45
45
  [dependency-groups]
46
46
  dev = [
47
- "pytest>=9.1.1",
47
+ "pytest>=9.2.0",
48
48
  "pytest-cov>=7.1.0",
49
- "python-dotenv>=1.2.2",
50
- "ruff>=0.16.0",
49
+ "python-dotenv>=1.2.4",
50
+ "ruff>=0.17.6",
51
51
  ]
52
52
 
53
53
  [build-system]
@@ -1,5 +1,5 @@
1
1
  from .client import PicnicAPI
2
- from .exceptions import PicnicParseError
2
+ from .exceptions import PicnicAPIError, PicnicParseError
3
3
  from .models import (
4
4
  Address,
5
5
  Article,
@@ -26,6 +26,7 @@ __all__ = [
26
26
  "PicnicAuthError",
27
27
  "Picnic2FAError",
28
28
  "Picnic2FARequired",
29
+ "PicnicAPIError",
29
30
  "PicnicParseError",
30
31
  "PicnicModel",
31
32
  "Article",
@@ -46,5 +47,5 @@ __all__ = [
46
47
  "OrderArticle",
47
48
  ]
48
49
  __title__ = "python-picnic-api"
49
- __version__ = "2.0.1"
50
+ __version__ = "2.0.3"
50
51
  __author__ = "Mike Brink"
@@ -1,9 +1,10 @@
1
+ import secrets
1
2
  from hashlib import md5
2
3
  from urllib.parse import quote
3
4
 
4
5
  import typing_extensions
5
6
 
6
- from .exceptions import PicnicParseError
7
+ from .exceptions import PicnicAPIError, PicnicParseError
7
8
  from .helper import _url_generator
8
9
  from .models import (
9
10
  Article,
@@ -26,10 +27,7 @@ from .session import (
26
27
  DEFAULT_URL = "https://storefront-prod.{}.picnicinternational.com/api/{}"
27
28
  DEFAULT_COUNTRY_CODE = "NL"
28
29
  DEFAULT_API_VERSION = "15"
29
- _HEADERS = {
30
- "x-picnic-agent": "30100;1.206.1-#15408",
31
- "x-picnic-did": "598F770380CA54B6",
32
- }
30
+ _PICNIC_AGENT = "30100;1.252.0-#15626;"
33
31
 
34
32
 
35
33
  class PicnicAPI:
@@ -39,29 +37,62 @@ class PicnicAPI:
39
37
  password: str = None,
40
38
  country_code: str = DEFAULT_COUNTRY_CODE,
41
39
  auth_token: str = None,
40
+ device_id: str | None = None,
42
41
  ):
43
42
  self._country_code = country_code
44
43
  self._base_url = _url_generator(
45
44
  DEFAULT_URL, self._country_code, DEFAULT_API_VERSION
46
45
  )
47
46
 
47
+ # Picnic seems to start blocking static device ids
48
+ self._device_id = device_id or secrets.token_hex(8).upper()
49
+ self._picnic_headers = {
50
+ "x-picnic-agent": _PICNIC_AGENT,
51
+ "x-picnic-did": self._device_id,
52
+ }
53
+
48
54
  self.session = PicnicAPISession(auth_token=auth_token)
49
55
 
50
56
  # Login if not authenticated
51
57
  if not self.session.authenticated and username and password:
52
58
  self.login(username, password)
53
59
 
60
+ @property
61
+ def device_id(self) -> str:
62
+ """The ``x-picnic-did`` value sent to Picnic; persist it to reuse it."""
63
+ return self._device_id
64
+
65
+ @staticmethod
66
+ def _parse_json(response, url: str):
67
+ """Decode a response body, raising a descriptive error if it is not JSON.
68
+
69
+ Picnic's edge can answer with an HTML or plain-text page instead of the
70
+ expected JSON (e.g. an HTTP 403 on ``/cart``). Without this the body
71
+ error surfaces as a bare ``JSONDecodeError`` that hides the status code.
72
+ """
73
+ try:
74
+ return response.json()
75
+ except ValueError as error:
76
+ body = response.text[:200]
77
+ raise PicnicAPIError(
78
+ "Picnic API returned a non-JSON response "
79
+ f"(HTTP {response.status_code}) for {url}: {body!r}",
80
+ status_code=response.status_code,
81
+ body=body,
82
+ ) from error
83
+
54
84
  def _get(self, path: str, add_picnic_headers=False):
55
85
  url = self._base_url + path
56
86
 
57
87
  # Make the request, add special picnic headers if needed
58
- headers = _HEADERS if add_picnic_headers else None
59
- response = self.session.get(url, headers=headers).json()
88
+ headers = self._picnic_headers if add_picnic_headers else None
89
+ response = self.session.get(url, headers=headers)
90
+ body = self._parse_json(response, url)
60
91
 
61
- if self._contains_auth_error(response):
92
+ if self._contains_auth_error(body):
62
93
  raise PicnicAuthError("Picnic authentication error")
63
94
 
64
- return response
95
+ return body
65
96
 
66
97
  def _post(
67
98
  self, path: str, data=None, base_url_override=None, add_picnic_headers=False
@@ -69,15 +100,16 @@ class PicnicAPI:
69
100
  url = (base_url_override if base_url_override else self._base_url) + path
70
101
  kwargs = {"json": data}
71
102
  if add_picnic_headers:
72
- kwargs["headers"] = _HEADERS
73
- response = self.session.post(url, **kwargs).json()
103
+ kwargs["headers"] = self._picnic_headers
104
+ response = self.session.post(url, **kwargs)
105
+ body = self._parse_json(response, url)
74
106
 
75
- if self._contains_auth_error(response):
107
+ if self._contains_auth_error(body):
76
108
  raise PicnicAuthError(
77
- f"Picnic authentication error: {response['error'].get('message')}"
109
+ f"Picnic authentication error: {body['error'].get('message')}"
78
110
  )
79
111
 
80
- return response
112
+ return body
81
113
 
82
114
  @staticmethod
83
115
  def _contains_auth_error(response):
@@ -115,12 +147,12 @@ class PicnicAPI:
115
147
  def _post_2fa(self, path: str, data=None):
116
148
  """POST for 2FA endpoints that may return empty (204) or JSON error bodies."""
117
149
  url = self._base_url + path
118
- response = self.session.post(url, json=data, headers=_HEADERS)
150
+ response = self.session.post(url, json=data, headers=self._picnic_headers)
119
151
 
120
152
  if response.status_code == 204 or not response.content:
121
153
  return None
122
154
 
123
- json_body = response.json()
155
+ json_body = self._parse_json(response, url)
124
156
 
125
157
  # This should not happen because password auth is already done
126
158
  # at this point, but just in case.
@@ -294,7 +326,9 @@ class PicnicAPI:
294
326
  if url == "http://picnic.app/nl/link/store/storefront":
295
327
  # gtin unknown
296
328
  return None
297
- r = self.session.get(url, headers=_HEADERS, allow_redirects=False)
329
+ r = self.session.get(
330
+ url, headers=self._picnic_headers, allow_redirects=False
331
+ )
298
332
  maxRedirects -= 1
299
333
  if ";id=" in r.url:
300
334
  # found the article id
@@ -8,6 +8,32 @@ imported from a single place.
8
8
  from .session import Picnic2FAError, Picnic2FARequired, PicnicAuthError
9
9
 
10
10
 
11
+ class PicnicAPIError(ValueError):
12
+ """Raised when the Picnic API returns a body that is not JSON.
13
+
14
+ Picnic normally returns JSON, including for errors, but its edge/WAF can
15
+ answer with an HTML or plain-text page (e.g. a 403 on ``/cart``). Raised
16
+ instead of a bare ``JSONDecodeError`` so the HTTP status and the start of
17
+ the body end up in the message.
18
+
19
+ Subclasses :class:`ValueError` because callers treat an undecodable body as
20
+ a transient response error: Home Assistant's Picnic coordinator maps
21
+ ``ValueError`` to ``UpdateFailed`` and suppresses it for endpoints that
22
+ legitimately return an empty body.
23
+ """
24
+
25
+ def __init__(
26
+ self,
27
+ message: str,
28
+ *,
29
+ status_code: int | None = None,
30
+ body: str | None = None,
31
+ ):
32
+ super().__init__(message)
33
+ self.status_code = status_code
34
+ self.body = body
35
+
36
+
11
37
  class PicnicParseError(Exception):
12
38
  """Raised when a Picnic PML ("layout") response cannot be parsed into a model.
13
39
 
@@ -25,6 +51,7 @@ class PicnicParseError(Exception):
25
51
 
26
52
 
27
53
  __all__ = [
54
+ "PicnicAPIError",
28
55
  "PicnicParseError",
29
56
  "PicnicAuthError",
30
57
  "Picnic2FARequired",
@@ -3,7 +3,7 @@ from unittest.mock import patch
3
3
 
4
4
  import pytest
5
5
 
6
- from python_picnic_api2 import Category, PicnicAPI
6
+ from python_picnic_api2 import Category, PicnicAPI, PicnicAPIError
7
7
  from python_picnic_api2.client import DEFAULT_URL
8
8
  from python_picnic_api2.session import (
9
9
  Picnic2FAError,
@@ -11,9 +11,10 @@ from python_picnic_api2.session import (
11
11
  PicnicAuthError,
12
12
  )
13
13
 
14
+ DEVICE_ID = "0123456789ABCDEF"
14
15
  PICNIC_HEADERS = {
15
- "x-picnic-agent": "30100;1.206.1-#15408",
16
- "x-picnic-did": "598F770380CA54B6",
16
+ "x-picnic-agent": "30100;1.252.0-#15626;",
17
+ "x-picnic-did": DEVICE_ID,
17
18
  }
18
19
 
19
20
 
@@ -24,14 +25,22 @@ class TestClient(unittest.TestCase):
24
25
  self.status_code = status_code
25
26
  self.content = content
26
27
 
28
+ @property
29
+ def text(self):
30
+ return self.content.decode("utf-8", errors="replace")
31
+
27
32
  def json(self):
33
+ if isinstance(self.json_data, Exception):
34
+ raise self.json_data
28
35
  return self.json_data
29
36
 
30
37
  def setUp(self) -> None:
31
38
  self.session_patcher = patch(
32
39
  "python_picnic_api2.client.PicnicAPISession")
33
40
  self.session_mock = self.session_patcher.start()
34
- self.client = PicnicAPI(username="test@test.nl", password="test")
41
+ self.client = PicnicAPI(
42
+ username="test@test.nl", password="test", device_id=DEVICE_ID
43
+ )
35
44
  self.expected_base_url = DEFAULT_URL.format("nl", "15")
36
45
 
37
46
  def tearDown(self) -> None:
@@ -39,7 +48,7 @@ class TestClient(unittest.TestCase):
39
48
 
40
49
  def test_login_credentials(self):
41
50
  self.session_mock().authenticated = False
42
- PicnicAPI(username="test@test.nl", password="test")
51
+ PicnicAPI(username="test@test.nl", password="test", device_id=DEVICE_ID)
43
52
  self.session_mock().post.assert_called_with(
44
53
  self.expected_base_url + "/user/login",
45
54
  json={
@@ -50,6 +59,23 @@ class TestClient(unittest.TestCase):
50
59
  headers=PICNIC_HEADERS,
51
60
  )
52
61
 
62
+ def test_device_id_generated_per_client(self):
63
+ first, second = PicnicAPI(), PicnicAPI()
64
+ self.assertRegex(first.device_id, r"^[0-9A-F]{16}$")
65
+ self.assertNotEqual(first.device_id, second.device_id)
66
+
67
+ self.session_mock().get.return_value = self.MockResponse(
68
+ {"type": "ORDER", "id": "shopping_cart"}, 200
69
+ )
70
+ first.get_cart()
71
+ self.session_mock().get.assert_called_with(
72
+ self.expected_base_url + "/cart",
73
+ headers={
74
+ "x-picnic-agent": PICNIC_HEADERS["x-picnic-agent"],
75
+ "x-picnic-did": first.device_id,
76
+ },
77
+ )
78
+
53
79
  def test_login_auth_token(self):
54
80
  self.session_mock().authenticated = True
55
81
  PicnicAPI(
@@ -394,6 +420,37 @@ class TestClient(unittest.TestCase):
394
420
  self.assertEqual(category.l2_id, 1000)
395
421
  self.assertEqual(category.l3_id, 22193)
396
422
 
423
+ def test_get_non_json_response(self):
424
+ body = b"<html><body>403 Forbidden</body></html>"
425
+ self.session_mock().get.return_value = self.MockResponse(
426
+ ValueError("Expecting value: line 1 column 1 (char 0)"), 403, content=body
427
+ )
428
+
429
+ with self.assertRaises(PicnicAPIError) as ctx:
430
+ self.client.get_cart()
431
+
432
+ self.assertEqual(str(ctx.exception), (
433
+ "Picnic API returned a non-JSON response (HTTP 403) for "
434
+ f"{self.expected_base_url}/cart: {body.decode()!r}"
435
+ ))
436
+ self.assertEqual(ctx.exception.status_code, 403)
437
+ self.assertEqual(ctx.exception.body, body.decode())
438
+ # Callers rely on ValueError handling for transient body errors
439
+ self.assertIsInstance(ctx.exception, ValueError)
440
+
441
+ def test_post_non_json_response(self):
442
+ body = b"Rate limited"
443
+ self.session_mock().post.return_value = self.MockResponse(
444
+ ValueError("Expecting value: line 1 column 1 (char 0)"), 429,
445
+ content=body,
446
+ )
447
+
448
+ with self.assertRaises(PicnicAPIError) as ctx:
449
+ self.client.clear_cart()
450
+
451
+ self.assertEqual(ctx.exception.status_code, 429)
452
+ self.assertEqual(ctx.exception.body, body.decode())
453
+
397
454
  def test_get_auth_exception(self):
398
455
  self.session_mock().get.return_value = self.MockResponse(
399
456
  {"error": {"code": "AUTH_ERROR"}}, 400