ai-lawyer-agent/core/api/fetch_api_data.py
2025-12-15 23:21:10 +01:00

24 lines
1008 B
Python

import httpx
async def fetch_api_data(icon: str, url: str, params: dict, remove_keys: list = None) -> dict:
try:
print(f"🔨{icon}Input parameters: {params}")
async with httpx.AsyncClient() as client:
response = await client.get(url, params=params, timeout=10.0)
response.raise_for_status()
print(f"🖇️{icon}Request URL: {response.url}")
data = response.json()
if remove_keys and isinstance(data, dict):
for key in remove_keys:
data.pop(key, None)
print(f"🚮{icon}Successfully removed key: {key}")
return data
except httpx.HTTPStatusError as e:
return {"error": "http_error", "status_code": e.response.status_code, "detail": e.response.text}
except httpx.RequestError as e:
return {"error": "request_error", "status_code": str(e)}
except Exception as e:
return {"error": "unexpected_error", "status_code": str(e)}