10000 Added support of session request + retry · rooprob/wc-api-python@4e5f0db · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e5f0db

Browse files
committed
Added support of session request + retry
1 parent dd19213 commit 4e5f0db

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

woocommerce/api.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55
"""
66

77
__title__ = "woocommerce-api"
8-
__version__ = "3.0.0"
9-
__author__ = "Claudio Sanches @ Automattic"
8+
__version__ = "3.0.1"
9+
__author__ = "Claudio Sanches & Antoine C"
1010
__license__ = "MIT"
1111

12-
from requests import request
1312
from json import dumps as jsonencode
1413
from time import time
15-
from woocommerce.oauth import OAuth
16-
from requests.auth import HTTPBasicAuth
1714
from urllib.parse import urlencode 8000
1815

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+
1922

2023
class API(object):
2124
""" API Class """
@@ -31,6 +34,10 @@ def __init__(self, url, consumer_key, consumer_secret, **kwargs):
3134
self.verify_ssl = kwargs.get("verify_ssl", True)
3235
self.query_string_auth = kwargs.get("query_string_auth", False)
3336
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()
3441

3542
def __is_ssl(self):
3643
""" Check if url use HTTPS """
@@ -73,6 +80,16 @@ def __request(self, method, endpoint, data, params=None, **kwargs):
7380
"accept": "application/json"
7481
}
7582

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+
7693
if self.is_ssl is True and self.query_string_auth is False:
7794
auth = HTTPBasicAuth(self.consumer_key, self.consumer_secret)
7895
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):
89106
data = jsonencode(data, ensure_ascii=False).encode('utf-8')
90107
headers["content-type"] = "application/json;charset=utf-8"
91108

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(
93115
method=method,
94116
url=url,
95117
verify=self.verify_ssl,

0 commit comments

Comments
 (0)
0