File size: 454 Bytes
4833096
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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