5
5
"""
6
6
7
7
__title__ = "woocommerce-api"
8
- __version__ = "3.0.0 "
9
- __author__ = "Claudio Sanches @ Automattic "
8
+ __version__ = "3.0.1 "
9
+ __author__ = "Claudio Sanches & Antoine C "
10
10
__license__ = "MIT"
11
11
12
- from requests import request
13
12
from json import dumps as jsonencode
14
13
from time import time
15
- from woocommerce .oauth import OAuth
16
- from requests .auth import HTTPBasicAuth
17
14
from urllib .parse import urlencode
8000
18
15
16
+ from requests import session
17
+ from requests .adapters import HTTPAdapter , Retry
18
+ from requests .auth import HTTPBasicAuth
19
+
20
+ from woocommerce .oauth import OAuth
21
+
19
22
20
23
class API (object ):
21
24
""" API Class """
@@ -31,6 +34,10 @@ def __init__(self, url, consumer_key, consumer_secret, **kwargs):
31
34
self .verify_ssl = kwargs .get ("verify_ssl" , True )
32
35
self .query_string_auth = kwargs .get ("query_string_auth" , False )
33
36
self .user_agent = kwargs .get ("user_agent" , f"WooCommerce-Python-REST-API/{ __version__ } " )
37
+ self .retries = kwargs .get ("retries" , 3 )
38
+ self .backoff_factor = kwargs .get ("backoff_factor" , 0.3 )
39
+ self .status_forcelist = kwargs .get ("status_forcelist" , [500 , 502 , 503 , 504 , 429 ])
40
+ self .session = session ()
34
41
35
42
def __is_ssl (self ):
36
43
""" Check if url use HTTPS """
@@ -73,6 +80,16 @@ def __request(self, method, endpoint, data, params=None, **kwargs):
73
80
"accept" : "application/json"
74
81
}
75
82
83
+ retry = Retry (
84
+ total = self .retries ,
85
+ read = self .retries ,
86
+ connect = self .retries ,
87
+ backoff_factor = self .backoff_factor ,
88
+ status_forcelist = self .status_forcelist ,
89
+ )
90
+
91
+ adapter = HTTPAdapter (max_retries = retry )
92
+
76
93
if self .is_ssl is True and self .query_string_auth is False :
77
94
auth = HTTPBasicAuth (self .consumer_key , self .consumer_secret )
78
95
elif self .is_ssl is True and self .query_string_auth is True :
@@ -89,7 +106,12 @@ def __request(self, method, endpoint, data, params=None, **kwargs):
89
106
data = jsonencode (data , ensure_ascii = False ).encode ('utf-8' )
90
107
headers ["content-type" ] = "application/json;charset=utf-8"
91
108
92
- return request (
109
+ if self .is_ssl :
110
+ self .session .mount ("https://" , adapter )
111
+ else :
112
+ self .session .mount ("http://" , adapter )
113
+
114
+ return self .session .request (
93
115
method = method ,
94
116
url = url ,
95
117
verify = self .verify_ssl ,
0 commit comments