weatherhack-api / services /api_client.py
Ig0tU
Performance optimization: Added caching (24h loc, 5m weather) and connection pooling
4833096
raw
history blame contribute delete
454 Bytes
from contextlib import asynccontextmanager
from typing import Optional
import httpx
_client: Optional[httpx.AsyncClient] = None
@asynccontextmanager
async def lifespan(app):
global _client
_client = httpx.AsyncClient()
yield
await _client.aclose()
def get_client() -> httpx.AsyncClient:
global _client
if _client is None:
raise RuntimeError("HTTP Client not initialized. Ensure lifespan is used.")
return _client