-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathsync_vs_async_v4.py
32 lines (27 loc) · 1012 Bytes
/
sync_vs_async_v4.py
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 requests
import logging
logging.basicConfig(
format="%(levelname)s @ %(asctime)s : %(message)s",
datefmt="%d.%m.%Y %H:%M:%S",
level=logging.DEBUG,
handlers=[logging.FileHandler("requests.log", mode="w"), logging.StreamHandler()],
)
BASE_URL = "https://www.canbula.com/prime"
def sync_request(n: int) -> dict:
"""Synchronous request with error handling and logging"""
try:
response = requests.get(f"{BASE_URL}/{n}")
response.raise_for_status()
logging.debug(f"Request returned with status code {response.status_code}")
return response.json()
except requests.exceptions.RequestException as e:
logging.error(f"Request for {n} failed: {e}")
return {}
if __name__ == "__main__":
logging.critical("A critical error occurred")
logging.debug("A debug message")
logging.error("An error occurred")
logging.info("An info message")
logging.warning("A warning message")
result = sync_request(5)
print(result)