Skip to content

Commit cfd0b78

Browse files
committed
Add helpers for retrieving the images from picnic and add a test for this
1 parent 11abb34 commit cfd0b78

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

integration_tests/test_helper.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from python_picnic_api.helper import get_image, get_recipe_image
2+
import requests
3+
4+
def test_get_image():
5+
id = "8560e1f1c2d2811dfefbbb2342ef0d95250533f2131416aca459bde35d73e901"
6+
size = "tile-medium"
7+
suffix = "webp"
8+
url = get_image(id, size=size, suffix=suffix)
9+
10+
response = requests.get(url)
11+
12+
# Check if the response status code indicates success
13+
assert response.status_code == 200, "Failed to fetch URL"
14+
15+
# Check if the response content type is an image format
16+
content_type = response.headers.get("content-type")
17+
assert content_type.startswith("image/"), "URL does not return an image"
18+
19+
20+
def test_get_recipe_image():
21+
id = "5c4cc7cb7a0429695da708394eb0cae1bd9b92935ac76c8fda63bbc57ad5b826"
22+
size = "medium"
23+
url = get_recipe_image(id, size=size)
24+
print(url)
25+
26+
response = requests.get(url)
27+
28+
# Check if the response status code indicates success
29+
assert response.status_code == 200, "Failed to fetch URL"
30+
31+
# Check if the response content type is an image format
32+
content_type = response.headers.get("content-type")
33+
assert content_type.startswith("image/"), "URL does not return an image"

python_picnic_api/helper.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
tee = "├── "
66
last = "└── "
77

8+
IMAGE_SIZES = ["small", "medium", "regualr", "large", "extra-large"]
9+
IMAGE_BASE_URL = "https://storefront-prod.nl.picnicinternational.com/static/images"
810

911
def _tree_generator(response: list, prefix: str = ""):
1012
"""A recursive tree generator,
@@ -23,3 +25,19 @@ def _tree_generator(response: list, prefix: str = ""):
2325

2426
def _url_generator(url: str, country_code: str, api_version: str):
2527
return url.format(country_code.lower(), api_version)
28+
29+
30+
def get_recipe_image(id: str, size="regular"):
31+
sizes = IMAGE_SIZES + ["1250x1250"]
32+
assert size in sizes, "size must be one of: " + ", ".join(sizes)
33+
return f"{IMAGE_BASE_URL}/recipes/{id}/{size}.png"
34+
35+
36+
def get_image(id: str, size="regular", suffix="webp"):
37+
assert suffix in ["webp", "png"], "suffix must be webp or png"
38+
sizes = IMAGE_SIZES + [f"tile-{size}" for size in IMAGE_SIZES]
39+
40+
assert size in sizes, (
41+
"size must be one of: " + ", ".join(sizes)
42+
)
43+
return f"{IMAGE_BASE_URL}/{id}/{size}.{suffix}"

0 commit comments

Comments
 (0)