python-picnic-api2 2.0.0__tar.gz → 2.0.2__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.0 → python_picnic_api2-2.0.2}/.github/workflows/ci.yaml +1 -1
  2. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/.github/workflows/it.yaml +1 -1
  3. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/.github/workflows/release.yml +2 -2
  4. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/PKG-INFO +3 -3
  5. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/integration_tests/test_client.py +2 -2
  6. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/pyproject.toml +5 -5
  7. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/src/python_picnic_api2/__init__.py +3 -2
  8. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/src/python_picnic_api2/client.py +36 -13
  9. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/src/python_picnic_api2/exceptions.py +27 -0
  10. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/test_client.py +46 -3
  11. python_picnic_api2-2.0.2/uv.lock +395 -0
  12. python_picnic_api2-2.0.0/uv.lock +0 -364
  13. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/.devcontainer/devcontainer.json +0 -0
  14. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/.env.example +0 -0
  15. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/.envrc +0 -0
  16. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/.github/ISSUE_TEMPLATE/bug_report.yml +0 -0
  17. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/.github/ISSUE_TEMPLATE/config.yml +0 -0
  18. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/.github/dependabot.yml +0 -0
  19. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/.github/release.yml +0 -0
  20. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/.gitignore +0 -0
  21. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/LICENSE.md +0 -0
  22. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/README.md +0 -0
  23. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/codecov.yml +0 -0
  24. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/flake.lock +0 -0
  25. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/flake.nix +0 -0
  26. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/integration_tests/__init__.py +0 -0
  27. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/integration_tests/test_helper.py +0 -0
  28. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/integration_tests/test_session.py +0 -0
  29. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/scripts/capture_fixtures.py +0 -0
  30. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/src/python_picnic_api2/helper.py +0 -0
  31. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/__init__.py +0 -0
  32. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/article.py +0 -0
  33. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/base.py +0 -0
  34. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/cart.py +0 -0
  35. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/category.py +0 -0
  36. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/common.py +0 -0
  37. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/delivery.py +0 -0
  38. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/pml.py +0 -0
  39. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/search.py +0 -0
  40. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/user.py +0 -0
  41. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/src/python_picnic_api2/session.py +0 -0
  42. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/__init__.py +0 -0
  43. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/fixtures/cart_de.json +0 -0
  44. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/fixtures/category_page_nl.json +0 -0
  45. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/fixtures/current_deliveries_de.json +0 -0
  46. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/fixtures/delivery_de.json +0 -0
  47. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/fixtures/delivery_slots_de.json +0 -0
  48. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/fixtures/product_details_bundle_de.json +0 -0
  49. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/fixtures/product_details_multipack_nl.json +0 -0
  50. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/fixtures/product_details_page_de.json +0 -0
  51. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/fixtures/product_details_page_nl.json +0 -0
  52. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/fixtures/search_page_de.json +0 -0
  53. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/fixtures/search_page_nl.json +0 -0
  54. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/fixtures/user_de.json +0 -0
  55. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/test_fixtures.py +0 -0
  56. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/test_models.py +0 -0
  57. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/test_pml.py +0 -0
  58. {python_picnic_api2-2.0.0 → python_picnic_api2-2.0.2}/tests/test_session.py +0 -0
@@ -20,7 +20,7 @@ jobs:
20
20
  - uses: actions/checkout@v7
21
21
 
22
22
  - name: Set up Python ${{ matrix.python-version }}
23
- uses: actions/setup-python@v6
23
+ uses: actions/setup-python@v7
24
24
  with:
25
25
  python-version: ${{ matrix.python-version }}
26
26
 
@@ -14,7 +14,7 @@ jobs:
14
14
  - uses: actions/checkout@v7
15
15
 
16
16
  - name: Set up Python 3.13
17
- uses: actions/setup-python@v6
17
+ uses: actions/setup-python@v7
18
18
  with:
19
19
  python-version: 3.13
20
20
 
@@ -7,7 +7,7 @@ jobs:
7
7
  - uses: actions/checkout@v7
8
8
 
9
9
  - name: Set up Python 3.13
10
- uses: actions/setup-python@v6
10
+ uses: actions/setup-python@v7
11
11
  with:
12
12
  python-version: 3.13
13
13
 
@@ -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.0
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.0
3
+ Version: 2.0.2
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.7
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
@@ -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.0"
3
+ version = "2.0.2"
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.7"
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.15.21",
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.0"
50
+ __version__ = "2.0.2"
50
51
  __author__ = "Mike Brink"
@@ -3,7 +3,7 @@ from urllib.parse import quote
3
3
 
4
4
  import typing_extensions
5
5
 
6
- from .exceptions import PicnicParseError
6
+ from .exceptions import PicnicAPIError, PicnicParseError
7
7
  from .helper import _url_generator
8
8
  from .models import (
9
9
  Article,
@@ -51,17 +51,37 @@ class PicnicAPI:
51
51
  if not self.session.authenticated and username and password:
52
52
  self.login(username, password)
53
53
 
54
+ @staticmethod
55
+ def _parse_json(response, url: str):
56
+ """Decode a response body, raising a descriptive error if it is not JSON.
57
+
58
+ Picnic's edge can answer with an HTML or plain-text page instead of the
59
+ expected JSON (e.g. an HTTP 403 on ``/cart``). Without this the body
60
+ error surfaces as a bare ``JSONDecodeError`` that hides the status code.
61
+ """
62
+ try:
63
+ return response.json()
64
+ except ValueError as error:
65
+ body = response.text[:200]
66
+ raise PicnicAPIError(
67
+ "Picnic API returned a non-JSON response "
68
+ f"(HTTP {response.status_code}) for {url}: {body!r}",
69
+ status_code=response.status_code,
70
+ body=body,
71
+ ) from error
72
+
54
73
  def _get(self, path: str, add_picnic_headers=False):
55
74
  url = self._base_url + path
56
75
 
57
76
  # Make the request, add special picnic headers if needed
58
77
  headers = _HEADERS if add_picnic_headers else None
59
- response = self.session.get(url, headers=headers).json()
78
+ response = self.session.get(url, headers=headers)
79
+ body = self._parse_json(response, url)
60
80
 
61
- if self._contains_auth_error(response):
81
+ if self._contains_auth_error(body):
62
82
  raise PicnicAuthError("Picnic authentication error")
63
83
 
64
- return response
84
+ return body
65
85
 
66
86
  def _post(
67
87
  self, path: str, data=None, base_url_override=None, add_picnic_headers=False
@@ -70,14 +90,15 @@ class PicnicAPI:
70
90
  kwargs = {"json": data}
71
91
  if add_picnic_headers:
72
92
  kwargs["headers"] = _HEADERS
73
- response = self.session.post(url, **kwargs).json()
93
+ response = self.session.post(url, **kwargs)
94
+ body = self._parse_json(response, url)
74
95
 
75
- if self._contains_auth_error(response):
96
+ if self._contains_auth_error(body):
76
97
  raise PicnicAuthError(
77
- f"Picnic authentication error: {response['error'].get('message')}"
98
+ f"Picnic authentication error: {body['error'].get('message')}"
78
99
  )
79
100
 
80
- return response
101
+ return body
81
102
 
82
103
  @staticmethod
83
104
  def _contains_auth_error(response):
@@ -120,7 +141,7 @@ class PicnicAPI:
120
141
  if response.status_code == 204 or not response.content:
121
142
  return None
122
143
 
123
- json_body = response.json()
144
+ json_body = self._parse_json(response, url)
124
145
 
125
146
  # This should not happen because password auth is already done
126
147
  # at this point, but just in case.
@@ -176,7 +197,7 @@ class PicnicAPI:
176
197
  return SearchResult.from_page(raw_results)
177
198
 
178
199
  def get_cart(self) -> Cart:
179
- return Cart.from_api(self._get("/cart"))
200
+ return Cart.from_api(self._get("/cart", add_picnic_headers=True))
180
201
 
181
202
  def get_article(self, article_id: str, add_category=False) -> Article | None:
182
203
  path = f"/pages/product-details-page-root?id={article_id}" + \
@@ -212,14 +233,16 @@ class PicnicAPI:
212
233
 
213
234
  def add_product(self, product_id: str, count: int = 1) -> Cart:
214
235
  data = {"product_id": product_id, "count": count}
215
- return Cart.from_api(self._post("/cart/add_product", data))
236
+ return Cart.from_api(self._post("/cart/add_product", data,
237
+ add_picnic_headers=True))
216
238
 
217
239
  def remove_product(self, product_id: str, count: int = 1) -> Cart:
218
240
  data = {"product_id": product_id, "count": count}
219
- return Cart.from_api(self._post("/cart/remove_product", data))
241
+ return Cart.from_api(self._post("/cart/remove_product", data,
242
+ add_picnic_headers=True))
220
243
 
221
244
  def clear_cart(self) -> Cart:
222
- return Cart.from_api(self._post("/cart/clear"))
245
+ return Cart.from_api(self._post("/cart/clear", add_picnic_headers=True))
223
246
 
224
247
  def get_delivery_slots(self) -> DeliverySlots:
225
248
  return DeliverySlots.from_api(self._get("/cart/delivery_slots"))
@@ -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,
@@ -24,7 +24,13 @@ class TestClient(unittest.TestCase):
24
24
  self.status_code = status_code
25
25
  self.content = content
26
26
 
27
+ @property
28
+ def text(self):
29
+ return self.content.decode("utf-8", errors="replace")
30
+
27
31
  def json(self):
32
+ if isinstance(self.json_data, Exception):
33
+ raise self.json_data
28
34
  return self.json_data
29
35
 
30
36
  def setUp(self) -> None:
@@ -254,7 +260,8 @@ class TestClient(unittest.TestCase):
254
260
  )
255
261
  cart = self.client.get_cart()
256
262
  self.session_mock().get.assert_called_with(
257
- self.expected_base_url + "/cart", headers=None
263
+ self.expected_base_url + "/cart",
264
+ headers=PICNIC_HEADERS,
258
265
  )
259
266
  self.assertEqual(cart.type, "ORDER")
260
267
  self.assertEqual(cart.total_count, 3)
@@ -267,6 +274,7 @@ class TestClient(unittest.TestCase):
267
274
  self.session_mock().post.assert_called_with(
268
275
  self.expected_base_url + "/cart/add_product",
269
276
  json={"product_id": "p3f2qa", "count": 1},
277
+ headers=PICNIC_HEADERS,
270
278
  )
271
279
  self.assertEqual(cart.type, "ORDER")
272
280
 
@@ -278,6 +286,7 @@ class TestClient(unittest.TestCase):
278
286
  self.session_mock().post.assert_called_with(
279
287
  self.expected_base_url + "/cart/add_product",
280
288
  json={"product_id": "gs4puhf3a", "count": 5},
289
+ headers=PICNIC_HEADERS,
281
290
  )
282
291
 
283
292
  def test_remove_product(self):
@@ -288,6 +297,7 @@ class TestClient(unittest.TestCase):
288
297
  self.session_mock().post.assert_called_with(
289
298
  self.expected_base_url + "/cart/remove_product",
290
299
  json={"product_id": "gs4puhf3a", "count": 5},
300
+ headers=PICNIC_HEADERS,
291
301
  )
292
302
 
293
303
  def test_clear_cart(self):
@@ -296,7 +306,9 @@ class TestClient(unittest.TestCase):
296
306
  )
297
307
  cart = self.client.clear_cart()
298
308
  self.session_mock().post.assert_called_with(
299
- self.expected_base_url + "/cart/clear", json=None
309
+ self.expected_base_url + "/cart/clear",
310
+ json=None,
311
+ headers=PICNIC_HEADERS,
300
312
  )
301
313
  self.assertEqual(cart.type, "ORDER")
302
314
 
@@ -388,6 +400,37 @@ class TestClient(unittest.TestCase):
388
400
  self.assertEqual(category.l2_id, 1000)
389
401
  self.assertEqual(category.l3_id, 22193)
390
402
 
403
+ def test_get_non_json_response(self):
404
+ body = b"<html><body>403 Forbidden</body></html>"
405
+ self.session_mock().get.return_value = self.MockResponse(
406
+ ValueError("Expecting value: line 1 column 1 (char 0)"), 403, content=body
407
+ )
408
+
409
+ with self.assertRaises(PicnicAPIError) as ctx:
410
+ self.client.get_cart()
411
+
412
+ self.assertEqual(str(ctx.exception), (
413
+ "Picnic API returned a non-JSON response (HTTP 403) for "
414
+ f"{self.expected_base_url}/cart: {body.decode()!r}"
415
+ ))
416
+ self.assertEqual(ctx.exception.status_code, 403)
417
+ self.assertEqual(ctx.exception.body, body.decode())
418
+ # Callers rely on ValueError handling for transient body errors
419
+ self.assertIsInstance(ctx.exception, ValueError)
420
+
421
+ def test_post_non_json_response(self):
422
+ body = b"Rate limited"
423
+ self.session_mock().post.return_value = self.MockResponse(
424
+ ValueError("Expecting value: line 1 column 1 (char 0)"), 429,
425
+ content=body,
426
+ )
427
+
428
+ with self.assertRaises(PicnicAPIError) as ctx:
429
+ self.client.clear_cart()
430
+
431
+ self.assertEqual(ctx.exception.status_code, 429)
432
+ self.assertEqual(ctx.exception.body, body.decode())
433
+
391
434
  def test_get_auth_exception(self):
392
435
  self.session_mock().get.return_value = self.MockResponse(
393
436
  {"error": {"code": "AUTH_ERROR"}}, 400