-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
95 lines (75 loc) · 3.91 KB
/
Copy pathexample.py
File metadata and controls
95 lines (75 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Example usage of the Rohlik API client.
Replace USERNAME and PASSWORD with your real credentials and run:
python example.py
It logs in and runs a product search, a cart fetch and a recipe search; the
other calls are commented out. Uncomment the ones you want to try. For a shop
other than Rohlík.cz, set SITE (see rohlik_api.SITES).
"""
import asyncio
from rohlik_api import SITES, APIRequestFailedError, InvalidCredentialsError, RohlikAPI
USERNAME = "your_email@example.com"
PASSWORD = "your_password"
SITE = SITES["cz"] # or "de" (Knuspr.de), "at", "hu", "ro"
async def main() -> None:
"""Demonstrate the service-based API of the Rohlik client."""
# The recommended pattern: an async context manager with auto-login.
# On entry it logs in; on exit it logs out and releases resources.
async with RohlikAPI(username=USERNAME, password=PASSWORD, base_url=SITE.base_url) as client:
print(f"Logged in: {client.is_logged_in}")
print(f"User ID: {client.user_id}, Address ID: {client.address_id}")
# --- Products ------------------------------------------------------
results = await client.products.search("mleko", limit=5)
if results:
for product in results.results:
print(f" {product.name} - {product.price}")
# composition = await client.products.get_composition(product_id=1425155)
# price = await client.products.get_price(product_id=1425155)
# summary = await client.products.get_ai_summary(product_id=1384964)
# cards = await client.products.get_cards([1425155, 1384964])
# deals = await client.products.get_week_sales(size=10)
# --- Cart ----------------------------------------------------------
cart = await client.cart.get_content()
print(
f"Cart total: {cart.total_price} {cart.currency or SITE.currency} "
f"({cart.total_items} items, minimum order {cart.minimum_order_price})"
)
# await client.cart.add_items([{"product_id": 1234567, "quantity": 2}])
# if cart.products:
# await client.cart.delete_item(cart.products[0].cart_item_id)
# --- Delivery & orders --------------------------------------------
# delivery = await client.delivery.get_info()
# slots = await client.delivery.get_next_slots()
# addresses = await client.delivery.get_addresses()
# next_order = await client.orders.get_next()
# history = await client.orders.get_delivered(limit=10)
# --- Account -------------------------------------------------------
# premium = await client.account.get_premium_profile()
# bags = await client.account.get_bags_info()
# shopping_list = await client.account.get_shopping_list("list-id")
# --- Recipes (Rohlík Chef) ----------------------------------------
recipes = await client.recipes.search("rajská", limit=5)
if recipes:
print(f"Found {recipes.total_hits} recipes")
# detail = await client.recipes.get_detail(recipe_id=59)
# products = await client.recipes.get_ingredient_products([102, 56], limit=5)
# --- Aggregated snapshot ------------------------------------------
# all_data = await client.get_data()
async def manual_session() -> None:
"""Demonstrate manual session management without the context manager."""
client = RohlikAPI(
username=USERNAME, password=PASSWORD, base_url=SITE.base_url, auto_login=False
)
try:
await client.login()
cart = await client.cart.get_content()
print(f"Cart total: {cart.total_price}")
await client.logout()
finally:
await client.close()
if __name__ == "__main__":
try:
asyncio.run(main())
except InvalidCredentialsError:
print("Invalid credentials - set USERNAME and PASSWORD in example.py")
except APIRequestFailedError as err:
print(f"Request failed: {err}")