-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcms.py
149 lines (124 loc) · 4.92 KB
/
cms.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
import logging.config
import requests
from log_config import LOGGING_CONFIG
logger = logging.getLogger(__file__)
def get_access_token(context):
cms_client_id = context.bot_data.get('cms_client_id')
cms_client_secret = context.bot_data.get('cms_client_secret')
url = 'https://api.moltin.com/oauth/access_token'
data = {
'client_id': cms_client_id,
'client_secret': cms_client_secret,
'grant_type': 'client_credentials',
}
response = requests.post(url, data=data)
response.raise_for_status()
context.bot_data['access_token'] = response.json().get('access_token')
logger.info('ACCESS TOKEN')
def get_products(access_token):
url = 'https://api.moltin.com/v2/products'
headers = {'Authorization': f'Bearer {access_token}'}
response = requests.get(url, headers=headers)
response.raise_for_status()
products = {
product.get('id'): product.get('name')
for product in response.json().get('data')
}
return products
def get_img_link(access_token, img_id):
url = f'https://api.moltin.com/v2/files/{img_id}'
headers = {'Authorization': f'Bearer {access_token}'}
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json().get('data').get('link').get('href')
def get_product_detail(access_token, product_id):
url = f'https://api.moltin.com/v2/products/{product_id}'
headers = {'Authorization': f'Bearer {access_token}'}
response = requests.get(url, headers=headers)
response.raise_for_status()
product_detail = response.json().get('data')
name = product_detail.get('name')
description = product_detail.get('description')
price = product_detail.get('meta').get(
'display_price').get('with_tax').get('formatted')
weight = product_detail.get('weight').get('kg')
img_id = product_detail.get('relationships').get(
'main_image').get('data').get('id')
img_link = get_img_link(access_token, img_id)
return (name, price, description, weight, img_link)
def get_cart_items(access_token, cart_name):
url = f'https://api.moltin.com/v2/carts/{cart_name}/items'
headers = {'Authorization': f'Bearer {access_token}'}
response = requests.get(url, headers=headers)
response.raise_for_status()
cart_items = response.json().get('data')
cart_products = []
for item in cart_items:
quantity = item.get('quantity')
name = item.get('name')
price = item.get('meta').get('display_price').get(
'with_tax').get('unit').get('formatted')
cost = item.get('meta').get('display_price').get(
'with_tax').get('value').get('formatted')
product_id = item.get('id')
product = {
'name': name, 'quantity': quantity,
'price': price, 'cost': cost,
'product_id': product_id
}
cart_products.append(product)
return cart_products
def get_total_cost_cart(access_token, cart_name):
url = f'https://api.moltin.com/v2/carts/{cart_name}'
headers = {'Authorization': f'Bearer {access_token}'}
response = requests.get(url, headers=headers)
response.raise_for_status()
cart = response.json().get('data')
cost = cart.get('meta').get('display_price').get(
'with_tax').get('formatted')
return cost
def add_product_to_cart(access_token, cart_name, product, quantity):
url = f'https://api.moltin.com/v2/carts/{cart_name}/items'
headers = {
'Authorization': f'Bearer {access_token}'
}
payload = {
'data': {
'id': product,
'type': 'cart_item',
'quantity': quantity
}
}
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
def remove_product_from_cart(access_token, cart_name, product_id):
url = f'https://api.moltin.com/v2/carts/{cart_name}/items/{product_id}'
headers = {'Authorization': f'Bearer {access_token}'}
response = requests.delete(url, headers=headers)
response.raise_for_status()
def create_customer(access_token, name, email):
url = 'https://api.moltin.com/v2/customers'
headers = {
'Authorization': f'Bearer {access_token}'
}
payload = {
'data': {
'type': 'customer',
'name': name,
'email': email,
},
}
response = requests.post(url, headers=headers, json=payload)
check_duplicate_email(response.json())
response.raise_for_status()
def check_duplicate_email(response):
if response.get('errors'):
for error in response.get('errors'):
if error.get('title') == 'Duplicate email':
raise FileExistsError
def get_customer(access_token, customer_id):
url = f'https://api.moltin.com/v2/customers/{customer_id}'
headers = {'Authorization': f'Bearer {access_token}'}
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json().get('data')