Spaces:
Sleeping
Sleeping
File size: 840 Bytes
ed4afb1 | 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 | import aiohttp
import logging
from typing import Any
from tenacity import retry, stop_after_attempt, wait_exponential
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=2, max=10)
)
async def async_fetch_json(
url: str,
headers: dict[str, str] | None = None,
params: dict[str, Any] | None = None,
timeout: int = 15
) -> Any:
async with aiohttp.ClientSession(headers=headers) as session:
try:
async with session.get(url, params=params, timeout=timeout) as response:
response.raise_for_status()
return await response.json()
except Exception as e:
logger.error(f"Fetch failed for {url}: {e}")
raise |