向后兼容httpx传参方式 (#1930)

This commit is contained in:
molanp 2025-06-20 16:53:05 +08:00 committed by GitHub
parent d6d54175f6
commit 96db5bf2a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -24,11 +24,14 @@ from zhenxun.services.log import logger
from zhenxun.utils.message import MessageUtils
from zhenxun.utils.user_agent import get_user_agent
CLIENT_KEY = ["use_proxy", "proxies", "verify", "headers"]
CLIENT_KEY = ["use_proxy", "proxies", "proxy", "verify", "headers"]
def get_async_client(
proxies: dict[str, str] | None = None, verify: bool = False, **kwargs
proxies: dict[str, str] | None = None,
proxy: str | None = None,
verify: bool = False,
**kwargs,
) -> httpx.AsyncClient:
transport = kwargs.pop("transport", None) or AsyncHTTPTransport(verify=verify)
if proxies:
@ -46,6 +49,15 @@ def get_async_client(
transport=transport,
**kwargs,
)
elif proxy:
return httpx.AsyncClient(
mounts={
"http://": AsyncHTTPTransport(proxy=Proxy(proxy)),
"https://": AsyncHTTPTransport(proxy=Proxy(proxy)),
},
transport=transport,
**kwargs,
)
return httpx.AsyncClient(transport=transport, **kwargs)
@ -66,6 +78,7 @@ class AsyncHttpx:
*,
use_proxy: bool = True,
proxies: dict[str, str] | None = None,
proxy: str | None = None,
headers: dict[str, str] | None = None,
verify: bool = False,
**kwargs,
@ -78,6 +91,7 @@ class AsyncHttpx:
参数:
use_proxy: 是否使用在类中定义的默认代理
proxies: 手动指定的代理会覆盖默认代理
proxy: 单个代理,用于兼容旧版本不再使用
headers: 需要合并到客户端的自定义请求头
verify: 是否验证 SSL 证书
**kwargs: 其他所有传递给 httpx.AsyncClient 的参数
@ -92,7 +106,11 @@ class AsyncHttpx:
final_headers.update(headers)
async with get_async_client(
proxies=proxies_to_use, verify=verify, headers=final_headers, **kwargs
proxies=proxies_to_use,
proxy=proxy,
verify=verify,
headers=final_headers,
**kwargs,
) as client:
yield client