-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
32 lines (28 loc) · 888 Bytes
/
Copy pathhandler.py
File metadata and controls
32 lines (28 loc) · 888 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
29
30
31
32
import time
import requests
class NetworkError(Exception):
pass
def retry(retries=3, delay=2):
def decorator(func):
def wrapper(*args, **kwargs):
for attempt in range(retries):
try:
return func(*args, **kwargs)
except (requests.ConnectionError, requests.Timeout) as e:
if attempt < retries - 1:
time.sleep(delay)
else:
raise NetworkError(f'Network operation failed: {e}')
return wrapper
return decorator
@retry(retries=5, delay=1)
def fetch_data(url):
response = requests.get(url)
response.raise_for_status()
return response.json()
if __name__ == '__main__':
try:
data = fetch_data('https://api.example.com/data')
print(data)
except NetworkError as e:
print(e)