python-picnic-api2 2.0.1__py3-none-any.whl → 2.0.3__py3-none-any.whl
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.
- python_picnic_api2/__init__.py +3 -2
- python_picnic_api2/client.py +51 -17
- python_picnic_api2/exceptions.py +27 -0
- {python_picnic_api2-2.0.1.dist-info → python_picnic_api2-2.0.3.dist-info}/METADATA +9 -3
- {python_picnic_api2-2.0.1.dist-info → python_picnic_api2-2.0.3.dist-info}/RECORD +7 -7
- {python_picnic_api2-2.0.1.dist-info → python_picnic_api2-2.0.3.dist-info}/WHEEL +1 -1
- {python_picnic_api2-2.0.1.dist-info → python_picnic_api2-2.0.3.dist-info}/licenses/LICENSE.md +0 -0
python_picnic_api2/__init__.py
CHANGED
|
@@ -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.
|
|
50
|
+
__version__ = "2.0.3"
|
|
50
51
|
__author__ = "Mike Brink"
|
python_picnic_api2/client.py
CHANGED
|
@@ -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
|
-
|
|
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 =
|
|
59
|
-
response = self.session.get(url, headers=headers)
|
|
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(
|
|
92
|
+
if self._contains_auth_error(body):
|
|
62
93
|
raise PicnicAuthError("Picnic authentication error")
|
|
63
94
|
|
|
64
|
-
return
|
|
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"] =
|
|
73
|
-
response = self.session.post(url, **kwargs)
|
|
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(
|
|
107
|
+
if self._contains_auth_error(body):
|
|
76
108
|
raise PicnicAuthError(
|
|
77
|
-
f"Picnic authentication error: {
|
|
109
|
+
f"Picnic authentication error: {body['error'].get('message')}"
|
|
78
110
|
)
|
|
79
111
|
|
|
80
|
-
return
|
|
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=
|
|
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 =
|
|
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(
|
|
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
|
python_picnic_api2/exceptions.py
CHANGED
|
@@ -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",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: python-picnic-api2
|
|
3
|
-
Version: 2.0.
|
|
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.
|
|
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:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
python_picnic_api2/__init__.py,sha256=
|
|
2
|
-
python_picnic_api2/client.py,sha256=
|
|
3
|
-
python_picnic_api2/exceptions.py,sha256=
|
|
1
|
+
python_picnic_api2/__init__.py,sha256=gNGbMpwpu2-H06qbgZ6AYcNAsrvHbihX2fu7vUtQWDk,975
|
|
2
|
+
python_picnic_api2/client.py,sha256=C1Vai_PePut-KQ4xh4LwpSZV-HPyLDn8XqAgZhjmNhw,12306
|
|
3
|
+
python_picnic_api2/exceptions.py,sha256=q3ERqBze8rMoNjX0QcRfinCWhExSdob6AFs4l23iSyQ,1973
|
|
4
4
|
python_picnic_api2/helper.py,sha256=CUoxRu2SZnHE-8azyfm7MUJ_vBA7ifVkHK-IUgUKuRM,971
|
|
5
5
|
python_picnic_api2/session.py,sha256=2nhlyJ2MXR1yHQRxw85B-KXF4zt8L5__KrI-T_VvURg,2388
|
|
6
6
|
python_picnic_api2/models/__init__.py,sha256=r9bwiAHOx9mhjizM7Cb3aTupOvTDXFQHkJEAUDdyLPo,922
|
|
@@ -13,7 +13,7 @@ python_picnic_api2/models/delivery.py,sha256=i29cw3E_To3azBGtYKvYkKolRpWJypWxPql
|
|
|
13
13
|
python_picnic_api2/models/pml.py,sha256=1NAygk1u2SKBo-QwWMoeuZ4ixTJdDLHdnM7blYdgplM,5290
|
|
14
14
|
python_picnic_api2/models/search.py,sha256=kGJVXKUml2wFfDyap000inhe5udGO_EssxVji0FJ1f4,2150
|
|
15
15
|
python_picnic_api2/models/user.py,sha256=Q0qmnhYacCCx93UO_OrpHLdiE4VtCWsunRhJUmxgruY,1662
|
|
16
|
-
python_picnic_api2-2.0.
|
|
17
|
-
python_picnic_api2-2.0.
|
|
18
|
-
python_picnic_api2-2.0.
|
|
19
|
-
python_picnic_api2-2.0.
|
|
16
|
+
python_picnic_api2-2.0.3.dist-info/METADATA,sha256=_4BowMS_7hzF-1SakCTv_N72IncHZxKVsgHChQ8Sv7s,8938
|
|
17
|
+
python_picnic_api2-2.0.3.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
18
|
+
python_picnic_api2-2.0.3.dist-info/licenses/LICENSE.md,sha256=CUvt2ilcBwKAfRAnoA4RNPTLcdUj3En21LPK9YkFMGg,11358
|
|
19
|
+
python_picnic_api2-2.0.3.dist-info/RECORD,,
|
{python_picnic_api2-2.0.1.dist-info → python_picnic_api2-2.0.3.dist-info}/licenses/LICENSE.md
RENAMED
|
File without changes
|