Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions integration_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ def test_get_article_with_category_name():
picnic.get_article("s1018620", add_category_name=True)


def test_get_article_by_gtin():
response = picnic.get_article_by_gtin("4311501044209")
assert response["id"] == "s1018620"
assert response["name"] == "Gut&Günstig H-Milch 3,5%"


def test_get_article_by_gtin_unknown():
response = picnic.get_article_by_gtin("4311501040000")
assert response is None


def test_get_cart():
response = picnic.get_cart()
assert isinstance(response, dict)
Expand Down
32 changes: 24 additions & 8 deletions src/python_picnic_api2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
GLOBAL_GATEWAY_URL = "https://gateway-prod.global.picnicinternational.com"
DEFAULT_COUNTRY_CODE = "NL"
DEFAULT_API_VERSION = "15"
_HEADERS = {
"x-picnic-agent": "30100;1.15.272-15295;",
"x-picnic-did": "3C417201548B2E3B",
}


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

# Make the request, add special picnic headers if needed
headers = (
{
"x-picnic-agent": "30100;1.15.272-15295;",
"x-picnic-did": "3C417201548B2E3B",
}
if add_picnic_headers
else None
)
headers = _HEADERS if add_picnic_headers else None
response = self.session.get(url, headers=headers).json()

if self._contains_auth_error(response):
Expand Down Expand Up @@ -177,5 +174,24 @@ def print_categories(self, depth: int = 0):
tree = "\n".join(_tree_generator(self.get_categories(depth=depth)))
print(tree)

def get_article_by_gtin(self, etan: str, maxRedirects: int = 5):
# Finds the article ID for a gtin/ean (barcode).

url = "https://picnic.app/" + self._country_code.lower() + "/qr/gtin/" + etan
while maxRedirects > 0:
if url == "http://picnic.app/nl/link/store/storefront":
# gtin unknown
return None
r = self.session.get(url, headers=_HEADERS, allow_redirects=False)
maxRedirects -= 1
if ";id=" in r.url:
# found the article id
return self.get_article(r.url.split(";id=", 1)[1])
if "Location" not in r.headers:
# article id not found but also no futher redirect
return None
url = r.headers["Location"]
return None


__all__ = ["PicnicAPI"]
8 changes: 8 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ def test_search_encoding(self):
headers=PICNIC_HEADERS,
)

def test_get_article_by_gtin(self):
self.client.get_article_by_gtin("123456789")
self.session_mock().get.assert_called_with(
"https://picnic.app/nl/qr/gtin/123456789",
headers=PICNIC_HEADERS,
allow_redirects=False,
)

def test_get_cart(self):
self.client.get_cart()
self.session_mock().get.assert_called_with(
Expand Down