-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
28 lines (26 loc) · 977 Bytes
/
utils.py
File metadata and controls
28 lines (26 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import time
import requests
from requests.exceptions import RequestException
def retry_request(url, max_retries=5, backoff_factor=0.3):
"""
Perform a GET request to the given URL with retry logic.
Args:
url (str): The URL to send the request to.
max_retries (int): Maximum number of retry attempts.
backoff_factor (float): Backoff factor for sleep time.
Returns:
Response object from requests library.
"""
retries = 0
while retries < max_retries:
try:
response = requests.get(url)
response.raise_for_status() # Raise an error for bad status codes
return response
except RequestException as e:
print(f"Request failed: {e}")
retries += 1
sleep_time = backoff_factor * (2 ** retries)
print(f"Retrying in {sleep_time:.1f} seconds...")
time.sleep(sleep_time)
raise Exception("Maximum retries exceeded")