-
Notifications
You must be signed in to change notification settings - Fork 0
/
moltin.py
229 lines (193 loc) · 6.99 KB
/
moltin.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import os
from dotenv import load_dotenv
import requests
from requests.exceptions import HTTPError, ConnectionError
MOLTIN_API_URL = 'https://api.moltin.com/v2'
MOLTIN_API_OAUTH_URL = 'https://api.moltin.com/oauth/access_token'
MOLTIN_ERR_MSG = 'Moltin API returns error:'
MOLTIN_FLOW_ADDRESSES = 'addresses2'
MOLTIN_FLOW_CUSTOMERS = 'customers2'
load_dotenv()
class MoltinError(Exception):
def __init__(self, message):
self.message = message
def check_resp_json(resp):
if 'errors' in resp.json():
raise MoltinError(f'{MOLTIN_ERR_MSG} {resp.json()}')
def get_headers(func):
def wrapper(*args):
moltin_client_id = os.getenv('MOLTIN_CLIENT_ID')
moltin_client_secret = os.getenv('MOLTIN_CLIENT_SECRET')
data = {'client_id': str(moltin_client_id),
'client_secret': str(moltin_client_secret),
'grant_type': 'client_credentials'}
try:
resp = requests.post(MOLTIN_API_OAUTH_URL, data=data)
resp.raise_for_status()
check_resp_json(resp)
moltin_token = resp.json()['access_token']
headers = {
'Authorization': 'Bearer {}'.format(moltin_token),
'Content-Type': 'application/json'
}
return func(headers, *args)
except HTTPError as e:
raise MoltinError(f'{MOLTIN_ERR_MSG} {e}')
except ConnectionError as e:
raise MoltinError(f'{MOLTIN_ERR_MSG} {e}')
return wrapper
@get_headers
def get_products(headers):
resp = requests.get(f'{MOLTIN_API_URL}/products', headers=headers)
resp.raise_for_status()
check_resp_json(resp)
products = resp.json()['data']
return {product['id']: product['name'] for product in products}
@get_headers
def get_product(headers, product_id):
resp = requests.get(f'{MOLTIN_API_URL}/products/{product_id}',
headers=headers)
resp.raise_for_status()
check_resp_json(resp)
product = resp.json()['data']
name = product['name']
description = product['description']
price = product['price'][0]['amount']
id_img = product['relationships']['main_image']['data']['id']
resp_img = requests.get(f'{MOLTIN_API_URL}/files/{id_img}',
headers=headers)
resp_img.raise_for_status()
check_resp_json(resp_img)
href_img = resp_img.json()['data']['link']['href']
return name, description, price, href_img
@get_headers
def get_cart(headers, cart_id):
resp = requests.get(f'{MOLTIN_API_URL}/carts/{cart_id}/items',
headers=headers)
resp.raise_for_status()
check_resp_json(resp)
cart = resp.json()
products = '\n'.join((f"{product['name']}: {product['quantity']} шт. по {product['unit_price']['amount']} руб." for product in cart['data']))
total = int(cart['meta']['display_price']['with_tax']['formatted'])
return cart, products, total
@get_headers
def get_customer(headers, id):
resp = requests.get(f'{MOLTIN_API_URL}/flows/{MOLTIN_FLOW_CUSTOMERS}/entries/{id}',
headers=headers)
resp.raise_for_status()
check_resp_json(resp)
data = resp.json()['data']
return data['latitude'], data['longitude'], data['nearest_shop_id']
@get_headers
def get_addresses(headers):
resp = requests.get(f'{MOLTIN_API_URL}/flows/{MOLTIN_FLOW_ADDRESSES}/entries',
headers=headers)
resp.raise_for_status()
check_resp_json(resp)
return resp.json()['data']
@get_headers
def get_address(headers, id):
resp = requests.get(f'{MOLTIN_API_URL}/flows/{MOLTIN_FLOW_ADDRESSES}/entries/{id}',
headers=headers)
resp.raise_for_status()
check_resp_json(resp)
data = resp.json()['data']
return data['address'], data['courier_telegram_id']
@get_headers
def add_customer(headers, chat_id, latitude, longitude, address, nearest_shop_id):
data = {
'data': {
'type': 'entry',
'id_field': chat_id,
'latitude': latitude,
'longitude': longitude,
'address': address,
'nearest_shop_id': nearest_shop_id,
}
}
resp = requests.post(f'{MOLTIN_API_URL}/flows/{MOLTIN_FLOW_CUSTOMERS}/entries',
headers=headers, json=data)
resp.raise_for_status()
check_resp_json(resp)
return resp.json()['data']['id']
@get_headers
def add_to_cart(headers, cart_id, product_id):
data = {
'data': {
'id': product_id,
'type': 'cart_item',
'quantity': 1,
}
}
headers['X-MOLTIN-CURRENCY'] = 'RUB'
resp = requests.post(f'{MOLTIN_API_URL}/carts/{cart_id}/items',
headers=headers, json=data)
resp.raise_for_status()
check_resp_json(resp)
cart = resp.json()
products = '\n'.join((f"{product['name']}: {product['quantity']} шт. по {product['unit_price']['amount']} руб." for product in cart['data']))
total = int(cart['meta']['display_price']['with_tax']['formatted'])
return cart, products, total
@get_headers
def delete_from_cart(headers, cart_id, item_id):
resp = requests.delete(f'{MOLTIN_API_URL}/carts/{cart_id}/items/{item_id}',
headers=headers)
resp.raise_for_status()
check_resp_json(resp)
return resp.json()
@get_headers
def create_flow(headers, name):
data = {
'data': {
'type': 'flow',
'name': name,
'slug': name,
'description': '',
'enabled': True
}
}
resp = requests.post(f'{MOLTIN_API_URL}/flows', headers=headers, json=data)
resp.raise_for_status()
check_resp_json(resp)
return resp.json()
@get_headers
def create_fields(headers, flow_id, fields):
result = ''
for field, type in fields.items():
data = {
'data': {
'type': 'field',
'name': field,
'slug': field,
'field_type': type,
'description': '',
'required': False,
'unique': False,
'default': 0,
'enabled': True,
'order': 1,
'relationships': {
'flow': {
'data': {
'type': 'flow',
'id': flow_id
}
}
}
}
}
resp = requests.post(f'{MOLTIN_API_URL}/fields', headers=headers, json=data)
resp.raise_for_status()
check_resp_json(resp)
result += ', ' + str(resp.json()['data']['name'])
return f'Fields: {result} was created'
@get_headers
def delete_flow(headers, id):
resp = requests.delete(f'{MOLTIN_API_URL}/flows/{id}', headers=headers)
resp.raise_for_status()
return resp
@get_headers
def get_flow_fields(headers, name):
resp = requests.get(f'{MOLTIN_API_URL}/flows/{name}/fields', headers=headers)
resp.raise_for_status()
return resp.json()