python-picnic-api2 2.0.2__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 (57) hide show
  1. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/PKG-INFO +7 -1
  2. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/README.md +6 -0
  3. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/pyproject.toml +1 -1
  4. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/src/python_picnic_api2/__init__.py +1 -1
  5. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/src/python_picnic_api2/client.py +21 -8
  6. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/test_client.py +24 -4
  7. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/uv.lock +1 -1
  8. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/.devcontainer/devcontainer.json +0 -0
  9. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/.env.example +0 -0
  10. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/.envrc +0 -0
  11. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/.github/ISSUE_TEMPLATE/bug_report.yml +0 -0
  12. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/.github/ISSUE_TEMPLATE/config.yml +0 -0
  13. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/.github/dependabot.yml +0 -0
  14. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/.github/release.yml +0 -0
  15. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/.github/workflows/ci.yaml +0 -0
  16. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/.github/workflows/it.yaml +0 -0
  17. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/.github/workflows/release.yml +0 -0
  18. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/.gitignore +0 -0
  19. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/LICENSE.md +0 -0
  20. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/codecov.yml +0 -0
  21. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/flake.lock +0 -0
  22. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/flake.nix +0 -0
  23. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/integration_tests/__init__.py +0 -0
  24. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/integration_tests/test_client.py +0 -0
  25. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/integration_tests/test_helper.py +0 -0
  26. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/integration_tests/test_session.py +0 -0
  27. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/scripts/capture_fixtures.py +0 -0
  28. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/src/python_picnic_api2/exceptions.py +0 -0
  29. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/src/python_picnic_api2/helper.py +0 -0
  30. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/__init__.py +0 -0
  31. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/article.py +0 -0
  32. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/base.py +0 -0
  33. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/cart.py +0 -0
  34. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/category.py +0 -0
  35. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/common.py +0 -0
  36. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/delivery.py +0 -0
  37. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/pml.py +0 -0
  38. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/search.py +0 -0
  39. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/src/python_picnic_api2/models/user.py +0 -0
  40. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/src/python_picnic_api2/session.py +0 -0
  41. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/__init__.py +0 -0
  42. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/fixtures/cart_de.json +0 -0
  43. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/fixtures/category_page_nl.json +0 -0
  44. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/fixtures/current_deliveries_de.json +0 -0
  45. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/fixtures/delivery_de.json +0 -0
  46. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/fixtures/delivery_slots_de.json +0 -0
  47. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/fixtures/product_details_bundle_de.json +0 -0
  48. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/fixtures/product_details_multipack_nl.json +0 -0
  49. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/fixtures/product_details_page_de.json +0 -0
  50. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/fixtures/product_details_page_nl.json +0 -0
  51. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/fixtures/search_page_de.json +0 -0
  52. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/fixtures/search_page_nl.json +0 -0
  53. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/fixtures/user_de.json +0 -0
  54. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/test_fixtures.py +0 -0
  55. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/test_models.py +0 -0
  56. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/test_pml.py +0 -0
  57. {python_picnic_api2-2.0.2 → python_picnic_api2-2.0.3}/tests/test_session.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: python-picnic-api2
3
- Version: 2.0.2
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>
@@ -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:
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "python-picnic-api2"
3
- version = "2.0.2"
3
+ version = "2.0.3"
4
4
  description = ""
5
5
  readme = "README.md"
6
6
  license = {text = "Apache-2.0"}
@@ -47,5 +47,5 @@ __all__ = [
47
47
  "OrderArticle",
48
48
  ]
49
49
  __title__ = "python-picnic-api"
50
- __version__ = "2.0.2"
50
+ __version__ = "2.0.3"
51
51
  __author__ = "Mike Brink"
@@ -1,3 +1,4 @@
1
+ import secrets
1
2
  from hashlib import md5
2
3
  from urllib.parse import quote
3
4
 
@@ -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,18 +37,31 @@ 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
+
54
65
  @staticmethod
55
66
  def _parse_json(response, url: str):
56
67
  """Decode a response body, raising a descriptive error if it is not JSON.
@@ -74,7 +85,7 @@ class PicnicAPI:
74
85
  url = self._base_url + path
75
86
 
76
87
  # Make the request, add special picnic headers if needed
77
- headers = _HEADERS if add_picnic_headers else None
88
+ headers = self._picnic_headers if add_picnic_headers else None
78
89
  response = self.session.get(url, headers=headers)
79
90
  body = self._parse_json(response, url)
80
91
 
@@ -89,7 +100,7 @@ class PicnicAPI:
89
100
  url = (base_url_override if base_url_override else self._base_url) + path
90
101
  kwargs = {"json": data}
91
102
  if add_picnic_headers:
92
- kwargs["headers"] = _HEADERS
103
+ kwargs["headers"] = self._picnic_headers
93
104
  response = self.session.post(url, **kwargs)
94
105
  body = self._parse_json(response, url)
95
106
 
@@ -136,7 +147,7 @@ class PicnicAPI:
136
147
  def _post_2fa(self, path: str, data=None):
137
148
  """POST for 2FA endpoints that may return empty (204) or JSON error bodies."""
138
149
  url = self._base_url + path
139
- response = self.session.post(url, json=data, headers=_HEADERS)
150
+ response = self.session.post(url, json=data, headers=self._picnic_headers)
140
151
 
141
152
  if response.status_code == 204 or not response.content:
142
153
  return None
@@ -315,7 +326,9 @@ class PicnicAPI:
315
326
  if url == "http://picnic.app/nl/link/store/storefront":
316
327
  # gtin unknown
317
328
  return None
318
- 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
+ )
319
332
  maxRedirects -= 1
320
333
  if ";id=" in r.url:
321
334
  # found the article id
@@ -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
 
@@ -37,7 +38,9 @@ class TestClient(unittest.TestCase):
37
38
  self.session_patcher = patch(
38
39
  "python_picnic_api2.client.PicnicAPISession")
39
40
  self.session_mock = self.session_patcher.start()
40
- 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
+ )
41
44
  self.expected_base_url = DEFAULT_URL.format("nl", "15")
42
45
 
43
46
  def tearDown(self) -> None:
@@ -45,7 +48,7 @@ class TestClient(unittest.TestCase):
45
48
 
46
49
  def test_login_credentials(self):
47
50
  self.session_mock().authenticated = False
48
- PicnicAPI(username="test@test.nl", password="test")
51
+ PicnicAPI(username="test@test.nl", password="test", device_id=DEVICE_ID)
49
52
  self.session_mock().post.assert_called_with(
50
53
  self.expected_base_url + "/user/login",
51
54
  json={
@@ -56,6 +59,23 @@ class TestClient(unittest.TestCase):
56
59
  headers=PICNIC_HEADERS,
57
60
  )
58
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
+
59
79
  def test_login_auth_token(self):
60
80
  self.session_mock().authenticated = True
61
81
  PicnicAPI(
@@ -293,7 +293,7 @@ wheels = [
293
293
 
294
294
  [[package]]
295
295
  name = "python-picnic-api2"
296
- version = "2.0.2"
296
+ version = "2.0.3"
297
297
  source = { editable = "." }
298
298
  dependencies = [
299
299
  { name = "pydantic" },