Repository navigation
Expand file tree
/
Copy pathclient.py
More file actions
107 lines (83 loc) · 3.2 KB
/
Copy pathclient.py
File metadata and controls
107 lines (83 loc) · 3.2 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
96
97
98
99
100
101
102
103
104
105
106
107
from .session import PicnicAPISession
from .config_handler import ConfigHandler
from .helper import _tree_generator
class PicnicAPI:
def __init__(
self,
username: str = None,
password: str = None,
country_code: str = None,
store: bool = False,
):
config = ConfigHandler(
username=username, password=password, country_code=country_code, store=store
)
self._base_url = self._url(config)
self._username = config["username"]
self._password = config["password"]
if username and password:
self._username = username
self._password = password
if store:
config.set_username(username)
config.set_password(password)
config.set_country_code(country_code)
elif "username" in config.keys() and "password" in config.keys():
self._username = config["username"]
self._password = config["password"]
else:
raise Exception("No username and/or password set")
self.session = PicnicAPISession()
self.session.login(self._username, self._password, self._base_url)
def _url(self, config):
return (
config["base_url"].format(config["country_code"].lower())
+ config["api_version"]
)
def _get(self, path: str):
url = self._base_url + path
return self.session.get(url).json()
def _post(self, path: str, data=None):
url = self._base_url + path
return self.session.post(url, json=data).json()
def get_user(self):
return self._get("/user")
def search(self, term: str):
path = "/search?search_term=" + term
return self._get(path)
def get_lists(self, listId: str = None):
if listId:
path = "/lists/" + listId
else:
path = "/lists"
return self._get(path)
def get_cart(self):
return self._get("/cart")
def add_product(self, productId: str, count: int = 1):
data = {"product_id": productId, "count": count}
return self._post("/cart/add_product", data)
def remove_product(self, productId: str, count: int = 1):
data = {"product_id": productId, "count": count}
return self._post("/cart/remove_product", data)
def clear_cart(self):
return self._post("/cart/clear")
def get_delivery_slots(self):
return self._get("/cart/delivery_slots")
def get_delivery(self, deliveryId: str):
path = "/deliveries/" + deliveryId
data = []
return self._post(path, data=data)
def get_deliveries(self, summary: bool = False):
data = []
if summary:
return self._post("/deliveries/summary", data=data)
return self._post("/deliveries", data=data)
def get_current_deliveries(self):
data = ["CURRENT"]
return self._post("/deliveries", data=data)
def get_categories(self, depth: int = 0):
return self._get(f"/my_store?depth={depth}")["catalog"]
def print_categories(self, depth: int = 0):
tree = "\n".join(_tree_generator(self.get_categories(depth=depth)))
print(tree)
__all__ = ["PicnicAPI"]