-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.py
More file actions
24 lines (22 loc) · 860 Bytes
/
Copy pathvalidators.py
File metadata and controls
24 lines (22 loc) · 860 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
import time
import requests
from typing import Callable, Any
def retry_request(max_retries: int = 3, backoff_factor: float = 0.5) -> Callable:
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
def wrapper(*args, **kwargs) -> Any:
last_exception = None
for attempt in range(max_retries):
try:
return func(*args, **kwargs)
except requests.RequestException as e:
last_exception = e
wait_time = backoff_factor * (2 ** attempt)
time.sleep(wait_time)
raise last_exception
return wrapper
return decorator
@retry_request(max_retries=5, backoff_factor=1)
def make_request(url: str) -> Any:
response = requests.get(url)
response.raise_for_status()
return response.json()