-
Notifications
You must be signed in to change notification settings - Fork 2
/
EtsyAPI.py
183 lines (156 loc) · 6.17 KB
/
EtsyAPI.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
import pprint
import requests
from bson.objectid import ObjectId
from requests import HTTPError, Response
from requests_oauthlib import OAuth1Session
# from async_oauthlib import OAuth1Session
from EtsyAPISession import EtsyAPISession
from schemas import ReceiptStatus
from config import ETSY_API_BASE_URI
ETSYAPI_REFERENCE = {
"findUserProfile": {
"HTTP Method": "GET",
"URI": "/users/:user_id/profile",
"Parameters": "user_id"
}
}
LIMIT = 100
async def create_etsy_api_with_etsy_connection(db, etsy_connection_id: str, want: int = 2):
_id: ObjectId = ObjectId(etsy_connection_id)
etsy_connection = await db["EtsyShopConnections"].find_one({"_id": _id})
etsy_api_session = EtsyAPISession(etsy_connection["etsy_oauth_token"], etsy_connection["etsy_oauth_token_secret"],
client_key=etsy_connection["app_key"],
client_secret=etsy_connection["app_secret"])
etsy_api = EtsyAPI(etsy_api_session.get_etsy_api_session())
if want == 1:
return etsy_api
else:
return etsy_connection, etsy_api
class EtsyAPI:
def __init__(self, session: OAuth1Session):
self.__session = session
def get_session(self):
return self.__session
def findUserProfile(self):
response = self.__session.get(f"{ETSY_API_BASE_URI}/users/__SELF__/profile")
return response
def findAllUserShops(self):
response = self.__session.get(f"{ETSY_API_BASE_URI}/users/__SELF__/shops")
if response.status_code == 200:
return response
raise HTTPError("User shops not found")
def getUser(self):
response = self.__session.get(f"{ETSY_API_BASE_URI}/users/__SELF__")
return response
def getShop_Receipt2(self, receipt_id: int):
response = self.__session.get(f"{ETSY_API_BASE_URI}/receipts/{receipt_id}")
print(response.text)
print(response.status_code)
res_json = response.json()
pprint.pprint(res_json)
return res_json["results"]
def findAllShopReceipts(self, shop_id: str,
**kwargs):
current_page: int = 1
base_url: str = f"{ETSY_API_BASE_URI}/shops/{shop_id}/receipts?limit={LIMIT}"
for k, v in kwargs.items():
if v is not None:
if k == "was_shipped" or k == "was_paid":
try:
v = str(v).lower()
except KeyError:
pass
base_url = base_url + f"&{k}={v}"
print(base_url)
base_url = base_url + "&page={current_page}"
response = self.__session.get(
base_url.format(current_page=current_page)
# f"{ETSY_API_BASE_URI}/shops/{shop_id}/receipts?limit={LIMIT}&was_shipped=false"
)
try:
response.raise_for_status()
except requests.exceptions.HTTPError as e:
# Whoops it wasn't a 200
return "Error: " + str(e)
# pprint.pprint(response)
res_json = response.json()
# pprint.pprint(res_json)
results = {
"results": res_json["results"]
}
# print(res_json)
# print(results["results"])
while res_json["pagination"]["next_page"] is not None:
current_page = res_json["pagination"]["next_page"]
print(base_url.format(current_page=current_page))
response = self.__session.get(
base_url.format(current_page=current_page)
# f"{ETSY_API_BASE_URI}/shops/{shop_id}/receipts?limit={LIMIT}&page={current_page}&was_shipped=false"
)
res_json = response.json()
results["results"].extend(res_json["results"])
print(f"COUNT: {len(results['results'])}")
# await self.__session.close()
# print(results)
return results
def findAllShopTransactions(self, shop_id: str):
response = self.__session.get(f"{ETSY_API_BASE_URI}/shops/{shop_id}/transactions")
return response
def findAllShop_Receipt2Transactions(self, receipt_id):
response = self.__session.get(f"{ETSY_API_BASE_URI}/receipts/{receipt_id}/transactions?limit={LIMIT}")
res_json = response.json()
results = {
"results": res_json["results"]
}
while res_json["pagination"]["next_page"] is not None:
next_page = res_json["pagination"]["next_page"]
response = self.__session.get(
f"{ETSY_API_BASE_URI}/receipts/{receipt_id}/transactions?limit={LIMIT}&page={next_page}")
res_json = response.json()
results["results"].extend(res_json['results'])
return results
def getImage_Listing(self, listing_id: str, listing_image_id: str) -> Response:
response: Response = self.__session.get(f"{ETSY_API_BASE_URI}/listings/{listing_id}/images/{listing_image_id}")
return response
"""
Synopsis: Searches the set of Receipt objects associated to a Shop by a query
HTTP Method: GET
URI: /shops/:shop_id/receipts/search
Parameters:
Name Required Default Type
shop_id Y shop_id_or_name
search_query Y string
limit N 25 int
offset N 0 int
page N int
"""
def searchAllShopReceipts(self, shop_id: str):
search_query: str = '14957 Clemson Dr'
response = self.__session.get(
f"{ETSY_API_BASE_URI}/shops/{shop_id}/receipts/search?limit={LIMIT}&search_query={search_query}"
)
res_json = response.json()
results = {
"results": res_json["results"]
}
pprint.pprint(res_json)
while res_json["pagination"]["next_page"] is not None:
next_page = res_json["pagination"]["next_page"]
response = self.__session.get(
f"{ETSY_API_BASE_URI}/shops/{shop_id}/receipts/search?limit={LIMIT}&page={next_page}")
res_json = response.json()
results["results"].extend(res_json['results'])
return results
def getListing(self, listing_id):
response = self.__session.get(f"{ETSY_API_BASE_URI}/listings/{listing_id}")
res_json = response.json()
pprint.pprint(res_json)
def getShippingTemplate(self, shipping_template_id):
response = self.__session.get(f"{ETSY_API_BASE_URI}/shipping/templates/{shipping_template_id}")
res_json = response.json()
pprint.pprint(res_json)
def findAllShopReceiptsByStatus(self, shop_id: str, status: ReceiptStatus):
response = self.__session.get(f"{ETSY_API_BASE_URI}/shops/{shop_id}/receipts/{status}")
res_json = response.json()
pprint.pprint(res_json)
print(res_json["count"])