mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-28 05:16:48 +08:00
⚗️ add proxy support
This commit is contained in:
parent
e62ce93011
commit
188eb110c5
@ -70,6 +70,7 @@ class Request:
|
|||||||
files: FilesTypes = None,
|
files: FilesTypes = None,
|
||||||
version: Union[str, HTTPVersion] = HTTPVersion.H11,
|
version: Union[str, HTTPVersion] = HTTPVersion.H11,
|
||||||
timeout: Optional[float] = None,
|
timeout: Optional[float] = None,
|
||||||
|
proxy: Optional[str] = None,
|
||||||
):
|
):
|
||||||
# method
|
# method
|
||||||
self.method: str = (
|
self.method: str = (
|
||||||
@ -81,6 +82,8 @@ class Request:
|
|||||||
self.version: HTTPVersion = HTTPVersion(version)
|
self.version: HTTPVersion = HTTPVersion(version)
|
||||||
# timeout
|
# timeout
|
||||||
self.timeout: Optional[float] = timeout
|
self.timeout: Optional[float] = timeout
|
||||||
|
# proxy
|
||||||
|
self.proxy: Optional[str] = proxy
|
||||||
|
|
||||||
# url
|
# url
|
||||||
if isinstance(url, tuple):
|
if isinstance(url, tuple):
|
||||||
|
@ -40,7 +40,7 @@ class Mixin(ForwardMixin):
|
|||||||
files = aiohttp.FormData()
|
files = aiohttp.FormData()
|
||||||
for name, file in setup.files:
|
for name, file in setup.files:
|
||||||
files.add_field(name, file[1], content_type=file[2], filename=file[0])
|
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(
|
async with session.request(
|
||||||
setup.method,
|
setup.method,
|
||||||
setup.url,
|
setup.url,
|
||||||
@ -48,6 +48,7 @@ class Mixin(ForwardMixin):
|
|||||||
json=setup.json,
|
json=setup.json,
|
||||||
headers=setup.headers,
|
headers=setup.headers,
|
||||||
timeout=timeout,
|
timeout=timeout,
|
||||||
|
proxy=setup.proxy,
|
||||||
) as response:
|
) as response:
|
||||||
res = Response(
|
res = Response(
|
||||||
response.status,
|
response.status,
|
||||||
@ -66,12 +67,13 @@ class Mixin(ForwardMixin):
|
|||||||
else:
|
else:
|
||||||
raise RuntimeError(f"Unsupported HTTP version: {setup.version}")
|
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(
|
ws = await session.ws_connect(
|
||||||
setup.url,
|
setup.url,
|
||||||
method=setup.method,
|
method=setup.method,
|
||||||
timeout=setup.timeout or 10,
|
timeout=setup.timeout or 10,
|
||||||
headers=setup.headers,
|
headers=setup.headers,
|
||||||
|
proxy=setup.proxy,
|
||||||
)
|
)
|
||||||
websocket = WebSocket(request=setup, session=session, websocket=ws)
|
websocket = WebSocket(request=setup, session=session, websocket=ws)
|
||||||
return websocket
|
return websocket
|
||||||
|
@ -12,7 +12,9 @@ from nonebot.drivers import (
|
|||||||
try:
|
try:
|
||||||
import httpx
|
import httpx
|
||||||
except ImportError:
|
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):
|
class Mixin(ForwardMixin):
|
||||||
@ -24,7 +26,9 @@ class Mixin(ForwardMixin):
|
|||||||
@overrides(ForwardMixin)
|
@overrides(ForwardMixin)
|
||||||
async def request(self, setup: Request) -> Response:
|
async def request(self, setup: Request) -> Response:
|
||||||
async with httpx.AsyncClient(
|
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:
|
) as client:
|
||||||
response = await client.request(
|
response = await client.request(
|
||||||
setup.method,
|
setup.method,
|
||||||
|
@ -28,7 +28,9 @@ try:
|
|||||||
from quart.datastructures import FileStorage
|
from quart.datastructures import FileStorage
|
||||||
from quart import Websocket as QuartWebSocket
|
from quart import Websocket as QuartWebSocket
|
||||||
except ImportError:
|
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])
|
_AsyncCallable = TypeVar("_AsyncCallable", bound=Callable[..., Coroutine])
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user