diff --git a/nonebot/drivers/_model.py b/nonebot/drivers/_model.py index a5998373..0484e054 100644 --- a/nonebot/drivers/_model.py +++ b/nonebot/drivers/_model.py @@ -70,6 +70,7 @@ class Request: files: FilesTypes = None, version: Union[str, HTTPVersion] = HTTPVersion.H11, timeout: Optional[float] = None, + proxy: Optional[str] = None, ): # method self.method: str = ( @@ -81,6 +82,8 @@ class Request: self.version: HTTPVersion = HTTPVersion(version) # timeout self.timeout: Optional[float] = timeout + # proxy + self.proxy: Optional[str] = proxy # url if isinstance(url, tuple): diff --git a/nonebot/drivers/aiohttp.py b/nonebot/drivers/aiohttp.py index 3f0784d9..264e78e8 100644 --- a/nonebot/drivers/aiohttp.py +++ b/nonebot/drivers/aiohttp.py @@ -40,7 +40,7 @@ class Mixin(ForwardMixin): files = aiohttp.FormData() for name, file in setup.files: files.add_field(name, file[1], content_type=file[2], filename=file[0]) - async with aiohttp.ClientSession(version=version) as session: + async with aiohttp.ClientSession(version=version, trust_env=True) as session: async with session.request( setup.method, setup.url, @@ -48,6 +48,7 @@ class Mixin(ForwardMixin): json=setup.json, headers=setup.headers, timeout=timeout, + proxy=setup.proxy, ) as response: res = Response( response.status, @@ -66,12 +67,13 @@ class Mixin(ForwardMixin): else: raise RuntimeError(f"Unsupported HTTP version: {setup.version}") - session = aiohttp.ClientSession(version=version) + session = aiohttp.ClientSession(version=version, trust_env=True) ws = await session.ws_connect( setup.url, method=setup.method, timeout=setup.timeout or 10, headers=setup.headers, + proxy=setup.proxy, ) websocket = WebSocket(request=setup, session=session, websocket=ws) return websocket diff --git a/nonebot/drivers/httpx.py b/nonebot/drivers/httpx.py index f6789ab4..742b6b6c 100644 --- a/nonebot/drivers/httpx.py +++ b/nonebot/drivers/httpx.py @@ -12,7 +12,9 @@ from nonebot.drivers import ( try: import httpx except ImportError: - raise ImportError("Please install httpx by using `pip install nonebot2[httpx]`") + raise ImportError( + "Please install httpx by using `pip install nonebot2[httpx]`" + ) from None class Mixin(ForwardMixin): @@ -24,7 +26,9 @@ class Mixin(ForwardMixin): @overrides(ForwardMixin) async def request(self, setup: Request) -> Response: async with httpx.AsyncClient( - http2=setup.version == HTTPVersion.H2, follow_redirects=True + http2=setup.version == HTTPVersion.H2, + proxies=setup.proxy, + follow_redirects=True, ) as client: response = await client.request( setup.method, diff --git a/nonebot/drivers/quart.py b/nonebot/drivers/quart.py index 68eedd67..fbd66a25 100644 --- a/nonebot/drivers/quart.py +++ b/nonebot/drivers/quart.py @@ -28,7 +28,9 @@ try: from quart.datastructures import FileStorage from quart import Websocket as QuartWebSocket except ImportError: - raise ValueError("Please install Quart by using `pip install nonebot2[quart]`") + raise ValueError( + "Please install Quart by using `pip install nonebot2[quart]`" + ) from None _AsyncCallable = TypeVar("_AsyncCallable", bound=Callable[..., Coroutine])