-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
120 lines (104 loc) · 3.86 KB
/
Copy pathutils.py
File metadata and controls
120 lines (104 loc) · 3.86 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
108
109
110
111
112
113
114
115
116
117
118
119
120
import requests
import streamlit as st
import jwt
API_URL = "http://localhost:8000/seller/v2/catalogo"
def get_attributes():
access_token = st.session_state.get("token", "")
if not access_token:
st.error("Token não encontrado. Faça login novamente.")
return {}
try:
decoded = jwt.decode(access_token, options={"verify_signature": False})
sellers_str = decoded.get("sellers", "")
sellers_list = [s.strip() for s in sellers_str.split(",")] if isinstance(sellers_str, str) else []
return {"sellers": sellers_list}
except Exception as e:
st.error(f"Erro ao decodificar token: {e}")
return {}
def get_headers():
token = st.session_state.get("token", "")
seller_id = st.session_state.get("sellerid")
headers = {
"x-seller-id": seller_id,
"Authorization": f"Bearer {token}"
}
return headers
def get_produtos(name_like=None, limit=50, offset=0, sort=None):
try:
params = {
"_limit": limit,
"_offset": offset
}
if name_like:
params["name_like"] = name_like
if sort:
params["_sort"] = sort
resp = requests.get(API_URL, headers=get_headers(), params=params)
if resp.status_code == 200:
return resp.json().get("results", [])
return []
except Exception as e:
print(f"Erro ao buscar produtos: {e}")
return []
def get_produto_por_sku(sku):
try:
resp = requests.get(f"{API_URL}/{sku}", headers=get_headers())
if resp.status_code == 200:
return resp.json()
return None
except Exception as e:
print(f"Erro ao buscar produto {sku}: {e}")
return None
def cadastrar_produto(sku, nome):
try:
payload = {"sku": sku, "name": nome}
resp = requests.post(API_URL, headers=get_headers(), json=payload)
if resp.status_code == 201:
return True, None
else:
try:
data = resp.json()
detalhes = data.get("details", [])
if detalhes and isinstance(detalhes, list):
mensagem_erro = detalhes[0].get("message", "Erro desconhecido")
else:
mensagem_erro = data.get("message", "Erro desconhecido")
except Exception:
mensagem_erro = "Erro desconhecido ao processar a resposta da API"
return False, mensagem_erro
except Exception as e:
print(f"Erro ao cadastrar produto: {e}")
return False, str(e)
def atualizar_produto(sku, nome=None, description=None):
try:
payload = {}
if nome is not None:
payload["name"] = nome
if description is not None:
payload["description"] = description
if not payload:
return False, "Nenhuma alteração fornecida."
resp = requests.patch(f"{API_URL}/{sku}", headers=get_headers(), json=payload)
if resp.status_code == 202:
return True, None
else:
try:
data = resp.json()
detalhes = data.get("details", [])
if detalhes and isinstance(detalhes, list):
mensagem_erro = detalhes[0].get("message", "Erro desconhecido")
else:
mensagem_erro = data.get("message", "Erro desconhecido")
except Exception:
mensagem_erro = "Erro desconhecido ao processar a resposta da API"
return False, mensagem_erro
except Exception as e:
print(f"Erro ao atualizar produto {sku}: {e}")
return False, str(e)
def excluir_produto(sku):
try:
resp = requests.delete(f"{API_URL}/{sku}", headers=get_headers())
return resp.status_code == 204
except Exception as e:
print(f"Erro ao excluir produto: {e}")
return False