python-picnic-api2 2.0.1__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.1 → python_picnic_api2-2.0.2}/.github/workflows/release.yml +1 -1
  2. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/PKG-INFO +3 -3
  3. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/integration_tests/test_client.py +2 -2
  4. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/pyproject.toml +5 -5
  5. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/src/python_picnic_api2/__init__.py +3 -2
  6. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/src/python_picnic_api2/client.py +30 -9
  7. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/src/python_picnic_api2/exceptions.py +27 -0
  8. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/test_client.py +38 -1
  9. python_picnic_api2-2.0.2/uv.lock +395 -0
  10. python_picnic_api2-2.0.1/uv.lock +0 -364
  11. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/.devcontainer/devcontainer.json +0 -0
  12. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/.env.example +0 -0
  13. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/.envrc +0 -0
  14. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/.github/ISSUE_TEMPLATE/bug_report.yml +0 -0
  15. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/.github/ISSUE_TEMPLATE/config.yml +0 -0
  16. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/.github/dependabot.yml +0 -0
  17. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/.github/release.yml +0 -0
  18. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/.github/workflows/ci.yaml +0 -0
  19. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/.github/workflows/it.yaml +0 -0
  20. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/.gitignore +0 -0
  21. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/LICENSE.md +0 -0
  22. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/README.md +0 -0
  23. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/codecov.yml +0 -0
  24. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/flake.lock +0 -0
  25. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/flake.nix +0 -0
  26. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/integration_tests/__init__.py +0 -0
  27. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/integration_tests/test_helper.py +0 -0
  28. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/integration_tests/test_session.py +0 -0
  29. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/scripts/capture_fixtures.py +0 -0
  30. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/src/python_picnic_api2/helper.py +0 -0
  31. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/__init__.py +0 -0
  32. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/article.py +0 -0
  33. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/base.py +0 -0
  34. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/cart.py +0 -0
  35. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/category.py +0 -0
  36. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/common.py +0 -0
  37. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/delivery.py +0 -0
  38. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/pml.py +0 -0
  39. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/search.py +0 -0
  40. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/src/python_picnic_api2/models/user.py +0 -0
  41. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/src/python_picnic_api2/session.py +0 -0
  42. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/__init__.py +0 -0
  43. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/fixtures/cart_de.json +0 -0
  44. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/fixtures/category_page_nl.json +0 -0
  45. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/fixtures/current_deliveries_de.json +0 -0
  46. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/fixtures/delivery_de.json +0 -0
  47. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/fixtures/delivery_slots_de.json +0 -0
  48. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/fixtures/product_details_bundle_de.json +0 -0
  49. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/fixtures/product_details_multipack_nl.json +0 -0
  50. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/fixtures/product_details_page_de.json +0 -0
  51. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/fixtures/product_details_page_nl.json +0 -0
  52. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/fixtures/search_page_de.json +0 -0
  53. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/fixtures/search_page_nl.json +0 -0
  54. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/fixtures/user_de.json +0 -0
  55. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/test_fixtures.py +0 -0
  56. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/test_models.py +0 -0
  57. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/tests/test_pml.py +0 -0
  58. {python_picnic_api2-2.0.1 → python_picnic_api2-2.0.2}/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.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.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
@@ -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.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.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.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.
@@ -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:
@@ -394,6 +400,37 @@ class TestClient(unittest.TestCase):
394
400
  self.assertEqual(category.l2_id, 1000)
395
401
  self.assertEqual(category.l3_id, 22193)
396
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
+
397
434
  def test_get_auth_exception(self):
398
435
  self.session_mock().get.return_value = self.MockResponse(
399
436
  {"error": {"code": "AUTH_ERROR"}}, 400