Skip to content

Commit a87006d

Browse files
committed
Add tests for ean search
1 parent 23d8783 commit a87006d

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

integration_tests/test_client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ def test_get_article_with_category_name():
5656
picnic.get_article("s1018620", add_category_name=True)
5757

5858

59+
def test_get_article_by_gtin():
60+
response = picnic.get_article_by_gtin("4311501044209")
61+
assert response["id"] == "s1018620"
62+
assert response["name"] == "Gut&Günstig H-Milch 3,5%"
63+
64+
65+
def test_get_article_by_gtin_unknown():
66+
response = picnic.get_article_by_gtin("4311501040000")
67+
assert response is None
68+
69+
5970
def test_get_cart():
6071
response = picnic.get_cart()
6172
assert isinstance(response, dict)

src/python_picnic_api2/client.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
GLOBAL_GATEWAY_URL = "https://gateway-prod.global.picnicinternational.com"
1616
DEFAULT_COUNTRY_CODE = "NL"
1717
DEFAULT_API_VERSION = "15"
18+
_HEADERS = {
19+
"x-picnic-agent": "30100;1.15.272-15295;",
20+
"x-picnic-did": "3C417201548B2E3B",
21+
}
1822

1923

2024
class PicnicAPI:
@@ -47,14 +51,7 @@ def _get(self, path: str, add_picnic_headers=False):
4751
url = self._base_url + path
4852

4953
# Make the request, add special picnic headers if needed
50-
headers = (
51-
{
52-
"x-picnic-agent": "30100;1.15.272-15295;",
53-
"x-picnic-did": "3C417201548B2E3B",
54-
}
55-
if add_picnic_headers
56-
else None
57-
)
54+
headers = _HEADERS if add_picnic_headers else None
5855
response = self.session.get(url, headers=headers).json()
5956

6057
if self._contains_auth_error(response):
@@ -177,27 +174,21 @@ def print_categories(self, depth: int = 0):
177174
tree = "\n".join(_tree_generator(self.get_categories(depth=depth)))
178175
print(tree)
179176

180-
def get_product_from_gtin(self, etan: str, maxRedirects: int = 5):
177+
def get_article_by_gtin(self, etan: str, maxRedirects: int = 5):
178+
# Finds the article ID for a gtin/ean (barcode).
181179

182-
# Finds the product ID for a gtin/ean (barcode).
183-
headers = (
184-
{
185-
"x-picnic-agent": "30100;1.15.272-15295;",
186-
"x-picnic-did": "3C417201548B2E3B",
187-
}
188-
)
189180
url = "https://picnic.app/" + self._country_code.lower() + "/qr/gtin/" + etan
190181
while maxRedirects > 0:
191182
if url == "http://picnic.app/nl/link/store/storefront":
192183
# gtin unknown
193184
return None
194-
r = self.session.get(url, headers=headers, allow_redirects=False)
185+
r = self.session.get(url, headers=_HEADERS, allow_redirects=False)
195186
maxRedirects -= 1
196187
if ";id=" in r.url:
197-
# found the product id
198-
return r.url.split(";id=",1)[1]
188+
# found the article id
189+
return self.get_article(r.url.split(";id=", 1)[1])
199190
if "Location" not in r.headers:
200-
# product id not found but also no futher redirect
191+
# article id not found but also no futher redirect
201192
return None
202193
url = r.headers["Location"]
203194
return None

0 commit comments

Comments
 (0)