29 lines
837 B
Python
29 lines
837 B
Python
import logging
|
|
import httpx
|
|
|
|
logging.basicConfig(
|
|
level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s"
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def fetch_user(user_id: int) -> dict[str, str]:
|
|
try:
|
|
response: httpx.Response = httpx.get(
|
|
f"https://jsonplaceholder.typicode.com/users/{user_id}"
|
|
)
|
|
response.raise_for_status()
|
|
data = response.json()
|
|
return {
|
|
"name": data["name"],
|
|
"email": data["email"],
|
|
"city": data["address"]["city"],
|
|
}
|
|
except httpx.HTTPStatusError as e:
|
|
if e.response.status_code == 404:
|
|
logger.warning(f"User with id {user_id} not found")
|
|
return {}
|
|
except Exception as e:
|
|
logger.error(f"Something went wrong. User {user_id}. Error: {e}")
|
|
return {}
|