refactor: modernize project and improve error handling and documentation
This commit is contained in:
parent
e27bf2a098
commit
d87d0cce11
17 changed files with 1793 additions and 188 deletions
|
|
@ -1,23 +1,41 @@
|
|||
import logging
|
||||
|
||||
import requests
|
||||
from tenacity import (
|
||||
retry,
|
||||
retry_if_exception_type,
|
||||
stop_after_attempt,
|
||||
wait_exponential,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@retry(
|
||||
stop=stop_after_attempt(3),
|
||||
wait=wait_exponential(multiplier=1, min=2, max=10),
|
||||
retry=retry_if_exception_type((requests.exceptions.RequestException,)),
|
||||
reraise=True,
|
||||
)
|
||||
def make_requests(
|
||||
url: str, headers: dict = {}, payload: dict = {}, timeout: int = 20
|
||||
url: str, headers: dict = None, params: dict = None, timeout: int = 20
|
||||
) -> dict:
|
||||
headers = headers or {}
|
||||
params = params or {}
|
||||
try:
|
||||
res = requests.get(url, headers=headers, params=payload, timeout=timeout)
|
||||
logger.debug(f"Requesting URL: {url} with params: {params}")
|
||||
res = requests.get(url, headers=headers, params=params, timeout=timeout)
|
||||
res.raise_for_status()
|
||||
return res.json()
|
||||
except requests.exceptions.HTTPError as errh:
|
||||
print("HTTP Error")
|
||||
print(errh.args[0])
|
||||
raise errh
|
||||
logger.error(f"HTTP Error: {errh}")
|
||||
raise
|
||||
except requests.ConnectionError as errc:
|
||||
print("http connection Error")
|
||||
raise errc
|
||||
logger.error(f"Connection Error: {errc}")
|
||||
raise
|
||||
except requests.exceptions.Timeout as errt:
|
||||
print("http connection timeout")
|
||||
raise errt
|
||||
logger.error(f"Timeout Error: {errt}")
|
||||
raise
|
||||
except Exception as e:
|
||||
print("unknown exception")
|
||||
raise e
|
||||
logger.error(f"Unknown Exception: {e}")
|
||||
raise
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue