8
8
import requests
9
9
10
10
from datetime import datetime , timezone
11
+ from requests .adapters import HTTPAdapter
12
+ from requests .packages .urllib3 .util .retry import Retry
11
13
12
14
from laceworksdk import version
13
15
from laceworksdk .config import (
@@ -43,7 +45,7 @@ def __init__(self, account, subaccount, api_key, api_secret):
43
45
super (HttpSession , self ).__init__ ()
44
46
45
47
# Create a requests session
46
- self ._session = requests . Session ()
48
+ self ._session = self . _retry_session ()
47
49
48
50
# Set the base parameters
49
51
self ._api_key = api_key
@@ -54,6 +56,36 @@ def __init__(self, account, subaccount, api_key, api_secret):
54
56
# Get an access token
55
57
self ._check_access_token ()
56
58
59
+ def _retry_session (self ,
60
+ retries = 3 ,
61
+ backoff_factor = 0.3 ,
62
+ status_forcelist = (500 , 502 , 504 )):
63
+ """
64
+ A method to set up automatic retries on HTTP requests that fail.
65
+ """
66
+
67
+ # Create a new requests session
68
+ session = requests .Session ()
69
+
70
+ # Establish the retry criteria
71
+ retry = Retry (
72
+ total = retries ,
73
+ read = retries ,
74
+ connect = retries ,
75
+ status = retries ,
76
+ backoff_factor = backoff_factor ,
77
+ status_forcelist = status_forcelist ,
78
+ )
79
+
80
+ # Build the adapter with the retry criteria
81
+ adapter = HTTPAdapter (max_retries = retry )
82
+
83
+ # Bind the adapter to HTTP/HTTPS calls
84
+ session .mount ('http://' , adapter )
85
+ session .mount ('https://' , adapter )
86
+
87
+ return session
88
+
57
89
def _check_access_token (self ):
58
90
"""
59
91
A method to check the validity of the access token.
0 commit comments