-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct_meli.py
44 lines (38 loc) · 1.73 KB
/
product_meli.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
from meli_sdk import Meli
from mercadolibre.exceptions import TokenExpired
from product_request_model import ProductResponseModel
from access_token_meli import AccessToken
import json
class Product:
def __init__(self, client_id, client_secret, item_id, site):
self.client_id = client_id
self.client_secret = client_secret
self.site = site
self.item_id = item_id
self.meli = Meli(
client_id = client_id,
client_secret = client_secret,
access_token = AccessToken(
client_id = client_id,
client_secret = client_secret,
site=site).get_token("access_token"))
# Get product from MercadoLibre
def get_product(self):
try:
response = self.meli.get(("items/{x}?include_attributes=all").format(x = self.item_id))
result = ProductResponseModel(response.text)
return result
except TokenExpired:
print("Your access key has expired. Generating a new one...")
return AccessToken(self.client_id, self.client_secret, self.site).get_access_token()
# Update product price from MercadoLibre
def update_product_price(self, price):
body = {"price":price}
try:
response = self.meli.put(("/items/{x}").format(x = self.item_id), body, {'access_token':self.meli.access_token})
if response.status_code == 401:
print("Invalid token! Unable to change price.")
return response.ok
except TokenExpired:
print("Your access key has expired. Generating a new one...")
return AccessToken(self.client_id, self.client_secret, self.site).get_access_token()