import os import asyncio from http import HTTPMethod import httpx REQUEST_TIMEOUT = 60 def get_env(key: str) -> str: v = os.environ.get(key, "") if not v: raise EnvironmentError(f"Missing env: {key}") return v async def http_request( method: HTTPMethod, url: str, *, label: str, headers: dict | None = None, params: dict | None = None, json_body: dict | None = None, timeout: int = REQUEST_TIMEOUT, max_retries: int = 0, ) -> httpx.Response | None: async with httpx.AsyncClient() as client: for attempt in range(max_retries + 1): try: resp = await client.request(method, url, headers=headers, params=params, json=json_body, timeout=timeout) return resp except httpx.RequestError as e: if attempt < max_retries: print(f" [retry] {label} → {e}, attempt {attempt + 1}") await asyncio.sleep((attempt + 1) * 2) else: print(f" [error] {label} → {e}") return None return None