Skip to content

Commit 7aae918

Browse files
Update requests-with-retry.md
1 parent a1995a1 commit 7aae918

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

recipes/python/requests-with-retry.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ from requests.adapters import HTTPAdapter
88

99

1010
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:
1616
"""
1717
Return a requests.Session object with a retry strategy.
18-
18+
1919
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.
2424
2525
Returns:
2626
requests.Session: A session object configured for retries.
@@ -35,16 +35,16 @@ def requests_retry_session(
3535
status_forcelist=status_forcelist,
3636
)
3737
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)
4040

4141
return session
4242

4343

4444
# Example usage
4545
try:
4646
s = requests_retry_session()
47-
response = s.get('http://httpbin.org/status/500')
47+
response = s.get("http://httpbin.org/status/500")
4848
response.raise_for_status()
4949
print("Success!")
5050
except requests.exceptions.RequestException as e:

0 commit comments

Comments
 (0)