mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-12-18 09:25:46 +08:00
⚗️ add proxy support
This commit is contained in:
parent
e62ce93011
commit
188eb110c5
@ -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):
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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])
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user