MCPcopy Create free account
hub / github.com/su-kaka/gcli2api / HttpxClientManager

Class HttpxClientManager

src/httpx_client.py:16–56  ·  view source on GitHub ↗

通用HTTP客户端管理器

Source from the content-addressed store, hash-verified

14
15
16class HttpxClientManager:
17 """通用HTTP客户端管理器"""
18
19 async def get_client_kwargs(self, timeout: float = 30.0, **kwargs) -> Dict[str, Any]:
20 """获取httpx客户端的通用配置参数"""
21 client_kwargs = {"timeout": timeout, **kwargs}
22
23 # 动态读取代理配置,支持热更新
24 current_proxy_config = await get_proxy_config()
25 if current_proxy_config:
26 client_kwargs["proxy"] = current_proxy_config
27
28 return client_kwargs
29
30 @asynccontextmanager
31 async def get_client(
32 self, timeout: float = 30.0, **kwargs
33 ) -> AsyncGenerator[httpx.AsyncClient, None]:
34 """获取配置好的异步HTTP客户端"""
35 client_kwargs = await self.get_client_kwargs(timeout=timeout, **kwargs)
36
37 async with httpx.AsyncClient(**client_kwargs) as client:
38 yield client
39
40 @asynccontextmanager
41 async def get_streaming_client(
42 self, timeout: float = None, **kwargs
43 ) -> AsyncGenerator[httpx.AsyncClient, None]:
44 """获取用于流式请求的HTTP客户端(无超时限制)"""
45 client_kwargs = await self.get_client_kwargs(timeout=timeout, **kwargs)
46
47 # 创建独立的客户端实例用于流式处理
48 client = httpx.AsyncClient(**client_kwargs)
49 try:
50 yield client
51 finally:
52 # 确保无论发生什么都关闭客户端
53 try:
54 await client.aclose()
55 except Exception as e:
56 log.warning(f"Error closing streaming client: {e}")
57
58
59# 全局HTTP客户端管理器实例

Callers 1

httpx_client.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected