@@ -8,19 +8,19 @@ from requests.adapters import HTTPAdapter
8
8
9
9
10
10
def requests_retry_session (
11
- retries = 3 ,
12
- backoff_factor = 0.3 ,
13
- status_forcelist = (500 , 502 , 503 , 504 ),
14
- session = None ,
15
- ):
11
+ retries : int = 3 ,
12
+ backoff_factor : float = 0.3 ,
13
+ status_forcelist : tuple = (500 , 502 , 503 , 504 ),
14
+ session : requests.Session | None = None ,
15
+ ) -> requests.Session :
16
16
"""
17
17
Return a requests.Session object with a retry strategy.
18
-
18
+
19
19
Args:
20
- retries (int) : Number of retries to attempt.
21
- backoff_factor (float) : A backoff factor to apply between attempts.
22
- status_forcelist (tuple) : A set of HTTP status codes that we should retry on.
23
- session (requests.Session) : An existing session object to mount the adapter on.
20
+ retries: Number of retries to attempt.
21
+ backoff_factor: A backoff factor to apply between attempts.
22
+ status_forcelist: A set of HTTP status codes that we should retry on.
23
+ session: An optional existing session object to mount the adapter on.
24
24
25
25
Returns:
26
26
requests.Session: A session object configured for retries.
@@ -35,16 +35,16 @@ def requests_retry_session(
35
35
status_forcelist = status_forcelist,
36
36
)
37
37
adapter = HTTPAdapter(max_retries = retry)
38
- session.mount(' http://' , adapter)
39
- session.mount(' https://' , adapter)
38
+ session.mount(" http://" , adapter)
39
+ session.mount(" https://" , adapter)
40
40
41
41
return session
42
42
43
43
44
44
# Example usage
45
45
try :
46
46
s = requests_retry_session()
47
- response = s.get(' http://httpbin.org/status/500' )
47
+ response = s.get(" http://httpbin.org/status/500" )
48
48
response.raise_for_status()
49
49
print (" Success!" )
50
50
except requests.exceptions.RequestException as e:
0 commit comments