mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
向后兼容httpx传参方式 (#1930)
This commit is contained in:
parent
d6d54175f6
commit
96db5bf2a5
@ -24,11 +24,14 @@ from zhenxun.services.log import logger
|
|||||||
from zhenxun.utils.message import MessageUtils
|
from zhenxun.utils.message import MessageUtils
|
||||||
from zhenxun.utils.user_agent import get_user_agent
|
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(
|
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:
|
) -> httpx.AsyncClient:
|
||||||
transport = kwargs.pop("transport", None) or AsyncHTTPTransport(verify=verify)
|
transport = kwargs.pop("transport", None) or AsyncHTTPTransport(verify=verify)
|
||||||
if proxies:
|
if proxies:
|
||||||
@ -46,6 +49,15 @@ def get_async_client(
|
|||||||
transport=transport,
|
transport=transport,
|
||||||
**kwargs,
|
**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)
|
return httpx.AsyncClient(transport=transport, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
@ -66,6 +78,7 @@ class AsyncHttpx:
|
|||||||
*,
|
*,
|
||||||
use_proxy: bool = True,
|
use_proxy: bool = True,
|
||||||
proxies: dict[str, str] | None = None,
|
proxies: dict[str, str] | None = None,
|
||||||
|
proxy: str | None = None,
|
||||||
headers: dict[str, str] | None = None,
|
headers: dict[str, str] | None = None,
|
||||||
verify: bool = False,
|
verify: bool = False,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
@ -78,6 +91,7 @@ class AsyncHttpx:
|
|||||||
参数:
|
参数:
|
||||||
use_proxy: 是否使用在类中定义的默认代理。
|
use_proxy: 是否使用在类中定义的默认代理。
|
||||||
proxies: 手动指定的代理,会覆盖默认代理。
|
proxies: 手动指定的代理,会覆盖默认代理。
|
||||||
|
proxy: 单个代理,用于兼容旧版本,不再使用
|
||||||
headers: 需要合并到客户端的自定义请求头。
|
headers: 需要合并到客户端的自定义请求头。
|
||||||
verify: 是否验证 SSL 证书。
|
verify: 是否验证 SSL 证书。
|
||||||
**kwargs: 其他所有传递给 httpx.AsyncClient 的参数。
|
**kwargs: 其他所有传递给 httpx.AsyncClient 的参数。
|
||||||
@ -92,7 +106,11 @@ class AsyncHttpx:
|
|||||||
final_headers.update(headers)
|
final_headers.update(headers)
|
||||||
|
|
||||||
async with get_async_client(
|
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:
|
) as client:
|
||||||
yield client
|
yield client
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user