-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
26 lines (20 loc) · 762 Bytes
/
Copy pathutils.py
File metadata and controls
26 lines (20 loc) · 762 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
import time
import requests
class NetworkError(Exception):
pass
def retry_network_call(func, retries=3, delay=2, *args, **kwargs):
for attempt in range(retries):
try:
response = func(*args, **kwargs)
if response.status_code == 200:
return response
raise NetworkError(f'Error: {response.status_code}')
except (requests.exceptions.RequestException, NetworkError) as e:
print(f'Attempt {attempt + 1} failed: {e}')
time.sleep(delay)
raise Exception('All retry attempts failed')
def get_game_data(url):
return retry_network_call(requests.get, url=url)
# Example usage:
# response = get_game_data('https://api.example.com/game')
# print(response.json())