-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWooCommerceAPI.py
More file actions
76 lines (61 loc) · 2.72 KB
/
Copy pathWooCommerceAPI.py
File metadata and controls
76 lines (61 loc) · 2.72 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
import requests
import json
from requests.auth import HTTPBasicAuth
from requests.exceptions import HTTPError, ConnectionError, Timeout
class WooCommerceAPI:
def __init__(self, url, consumer_key, consumer_secret, timeout=10, max_retries=3):
self.url = url.rstrip('/')
self.auth = (consumer_key, consumer_secret)
self.headers = {
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
self.timeout = timeout
self.max_retries = max_retries
def _request(self, method, endpoint, data=None, params=None):
url = f"{self.url}/wp-json/wc/v3/{endpoint}"
for attempt in range(self.max_retries):
try:
response = requests.request(
method,
url,
auth=self.auth,
headers=self.headers,
timeout=self.timeout,
data=json.dumps(data) if data else None,
params=params
)
response.raise_for_status()
response.encoding = 'utf-8' # اطمینان از کدگذاری صحیح
return response.json()
except (HTTPError, ConnectionError, Timeout) as e:
print(f"Attempt {attempt + 1} failed: {e}")
if attempt + 1 == self.max_retries:
raise Exception(f"API request failed after {self.max_retries} attempts")
def get(self, endpoint, params=None):
return self._request("GET", endpoint, params=params)
def post(self, endpoint, data):
return self._request("POST", endpoint, data)
def put(self, endpoint, data):
return self._request("PUT", endpoint, data)
def delete(self, endpoint):
return self._request("DELETE", endpoint)
def get_all_products(self, per_page=100):
products = []
page = 1
while True:
params = {'per_page': per_page, 'page': page}
page_products = self.get("products", params=params)
if not page_products:
break
products.extend(page_products)
page += 1
return products
def get_products(self):
return self.get("products")
def create_product(self, product_data):
return self.post("products", product_data)
def update_product(self, product_id, product_data):
return self.put(f"products/{product_id}", product_data)
def delete_product(self, product_id, force=True):
return self.delete(f"products/{product_id}?force={str(force).lower()}")