python-picnic-api2 2.0.0__py3-none-any.whl → 2.0.2__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 +36 -13
- python_picnic_api2/exceptions.py +27 -0
- {python_picnic_api2-2.0.0.dist-info → python_picnic_api2-2.0.2.dist-info}/METADATA +3 -3
- {python_picnic_api2-2.0.0.dist-info → python_picnic_api2-2.0.2.dist-info}/RECORD +7 -7
- {python_picnic_api2-2.0.0.dist-info → python_picnic_api2-2.0.2.dist-info}/WHEEL +1 -1
- {python_picnic_api2-2.0.0.dist-info → python_picnic_api2-2.0.2.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.2"
|
|
50
51
|
__author__ = "Mike Brink"
|
python_picnic_api2/client.py
CHANGED
|
@@ -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)
|
|
78
|
+
response = self.session.get(url, headers=headers)
|
|
79
|
+
body = self._parse_json(response, url)
|
|
60
80
|
|
|
61
|
-
if self._contains_auth_error(
|
|
81
|
+
if self._contains_auth_error(body):
|
|
62
82
|
raise PicnicAuthError("Picnic authentication error")
|
|
63
83
|
|
|
64
|
-
return
|
|
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)
|
|
93
|
+
response = self.session.post(url, **kwargs)
|
|
94
|
+
body = self._parse_json(response, url)
|
|
74
95
|
|
|
75
|
-
if self._contains_auth_error(
|
|
96
|
+
if self._contains_auth_error(body):
|
|
76
97
|
raise PicnicAuthError(
|
|
77
|
-
f"Picnic authentication error: {
|
|
98
|
+
f"Picnic authentication error: {body['error'].get('message')}"
|
|
78
99
|
)
|
|
79
100
|
|
|
80
|
-
return
|
|
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 =
|
|
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"))
|
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.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.
|
|
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
|
|
@@ -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=fuTT-aUyPOS75WCmpPgk4lTgkcVSRPtfp7Qly1-Saf0,975
|
|
2
|
+
python_picnic_api2/client.py,sha256=9hLWpDMYl8mhOllixDI416BOlCgDpsBVTtFP6-jI7lk,11816
|
|
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.2.dist-info/METADATA,sha256=VgQhZ26R-O8ACVjnPXNsiVqOxgbTmxtZO-90qKa-3Pc,8646
|
|
17
|
+
python_picnic_api2-2.0.2.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
18
|
+
python_picnic_api2-2.0.2.dist-info/licenses/LICENSE.md,sha256=CUvt2ilcBwKAfRAnoA4RNPTLcdUj3En21LPK9YkFMGg,11358
|
|
19
|
+
python_picnic_api2-2.0.2.dist-info/RECORD,,
|
{python_picnic_api2-2.0.0.dist-info → python_picnic_api2-2.0.2.dist-info}/licenses/LICENSE.md
RENAMED
|
File without changes
|