2022-01-22 15:23:07 +08:00
|
|
|
"""[HTTPX](https://www.python-httpx.org/) 驱动适配
|
|
|
|
|
|
|
|
```bash
|
|
|
|
nb driver install httpx
|
|
|
|
# 或者
|
|
|
|
pip install nonebot2[httpx]
|
|
|
|
```
|
|
|
|
|
|
|
|
:::tip 提示
|
|
|
|
本驱动仅支持客户端 HTTP 连接
|
|
|
|
:::
|
|
|
|
|
|
|
|
FrontMatter:
|
2024-10-22 10:33:48 +08:00
|
|
|
mdx:
|
|
|
|
format: md
|
2022-01-22 15:23:07 +08:00
|
|
|
sidebar_position: 3
|
|
|
|
description: nonebot.drivers.httpx 模块
|
|
|
|
"""
|
2023-06-24 14:47:35 +08:00
|
|
|
|
2023-07-17 15:56:27 +08:00
|
|
|
from typing_extensions import override
|
2024-04-05 21:11:05 +08:00
|
|
|
from typing import TYPE_CHECKING, Union, Optional
|
|
|
|
|
|
|
|
from multidict import CIMultiDict
|
2021-12-26 13:42:13 +08:00
|
|
|
|
2023-01-01 15:08:00 +08:00
|
|
|
from nonebot.drivers.none import Driver as NoneDriver
|
2024-04-05 21:11:05 +08:00
|
|
|
from nonebot.internal.driver import Cookies, QueryTypes, CookieTypes, HeaderTypes
|
2021-12-22 16:53:55 +08:00
|
|
|
from nonebot.drivers import (
|
2024-04-05 21:11:05 +08:00
|
|
|
URL,
|
2021-12-22 16:53:55 +08:00
|
|
|
Request,
|
|
|
|
Response,
|
|
|
|
HTTPVersion,
|
2023-08-26 11:03:24 +08:00
|
|
|
HTTPClientMixin,
|
2024-04-05 21:11:05 +08:00
|
|
|
HTTPClientSession,
|
2021-12-22 16:53:55 +08:00
|
|
|
combine_driver,
|
|
|
|
)
|
|
|
|
|
2021-12-23 17:20:26 +08:00
|
|
|
try:
|
|
|
|
import httpx
|
2023-03-29 15:59:54 +08:00
|
|
|
except ModuleNotFoundError as e: # pragma: no cover
|
2021-12-25 14:04:53 +08:00
|
|
|
raise ImportError(
|
2023-06-24 14:47:35 +08:00
|
|
|
"Please install httpx first to use this driver. "
|
|
|
|
"Install with pip: `pip install nonebot2[httpx]`"
|
2023-02-09 10:24:27 +08:00
|
|
|
) from e
|
2021-12-23 17:20:26 +08:00
|
|
|
|
2021-12-22 16:53:55 +08:00
|
|
|
|
2024-04-05 21:11:05 +08:00
|
|
|
class Session(HTTPClientSession):
|
|
|
|
@override
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
params: QueryTypes = None,
|
|
|
|
headers: HeaderTypes = None,
|
|
|
|
cookies: CookieTypes = None,
|
|
|
|
version: Union[str, HTTPVersion] = HTTPVersion.H11,
|
|
|
|
timeout: Optional[float] = None,
|
|
|
|
proxy: Optional[str] = None,
|
|
|
|
):
|
|
|
|
self._client: Optional[httpx.AsyncClient] = None
|
|
|
|
|
|
|
|
self._params = (
|
|
|
|
tuple(URL.build(query=params).query.items()) if params is not None else None
|
|
|
|
)
|
|
|
|
self._headers = (
|
|
|
|
tuple(CIMultiDict(headers).items()) if headers is not None else None
|
|
|
|
)
|
|
|
|
self._cookies = Cookies(cookies)
|
|
|
|
self._version = HTTPVersion(version)
|
|
|
|
self._timeout = timeout
|
|
|
|
self._proxy = proxy
|
|
|
|
|
|
|
|
@property
|
|
|
|
def client(self) -> httpx.AsyncClient:
|
|
|
|
if self._client is None:
|
|
|
|
raise RuntimeError("Session is not initialized")
|
|
|
|
return self._client
|
|
|
|
|
|
|
|
@override
|
|
|
|
async def request(self, setup: Request) -> Response:
|
|
|
|
response = await self.client.request(
|
|
|
|
setup.method,
|
|
|
|
str(setup.url),
|
|
|
|
content=setup.content,
|
|
|
|
data=setup.data,
|
|
|
|
files=setup.files,
|
|
|
|
json=setup.json,
|
|
|
|
headers=tuple(setup.headers.items()),
|
|
|
|
cookies=setup.cookies.jar,
|
|
|
|
timeout=setup.timeout,
|
|
|
|
)
|
|
|
|
return Response(
|
|
|
|
response.status_code,
|
|
|
|
headers=response.headers.multi_items(),
|
|
|
|
content=response.content,
|
|
|
|
request=setup,
|
|
|
|
)
|
|
|
|
|
|
|
|
@override
|
|
|
|
async def setup(self) -> None:
|
2024-04-08 20:17:03 +08:00
|
|
|
if self._client is not None:
|
|
|
|
raise RuntimeError("Session has already been initialized")
|
2024-04-05 21:11:05 +08:00
|
|
|
self._client = httpx.AsyncClient(
|
|
|
|
params=self._params,
|
|
|
|
headers=self._headers,
|
|
|
|
cookies=self._cookies.jar,
|
|
|
|
http2=self._version == HTTPVersion.H2,
|
|
|
|
proxies=self._proxy,
|
|
|
|
follow_redirects=True,
|
|
|
|
)
|
|
|
|
await self._client.__aenter__()
|
|
|
|
|
|
|
|
@override
|
|
|
|
async def close(self) -> None:
|
|
|
|
try:
|
|
|
|
if self._client is not None:
|
|
|
|
await self._client.aclose()
|
|
|
|
finally:
|
|
|
|
self._client = None
|
|
|
|
|
|
|
|
|
2023-08-26 11:03:24 +08:00
|
|
|
class Mixin(HTTPClientMixin):
|
2022-01-22 15:23:07 +08:00
|
|
|
"""HTTPX Mixin"""
|
|
|
|
|
2021-12-22 16:53:55 +08:00
|
|
|
@property
|
2023-07-17 15:56:27 +08:00
|
|
|
@override
|
2021-12-22 16:53:55 +08:00
|
|
|
def type(self) -> str:
|
|
|
|
return "httpx"
|
|
|
|
|
2023-07-17 15:56:27 +08:00
|
|
|
@override
|
2021-12-22 16:53:55 +08:00
|
|
|
async def request(self, setup: Request) -> Response:
|
2024-04-05 21:11:05 +08:00
|
|
|
async with self.get_session(
|
|
|
|
version=setup.version, proxy=setup.proxy
|
|
|
|
) as session:
|
|
|
|
return await session.request(setup)
|
|
|
|
|
|
|
|
@override
|
|
|
|
def get_session(
|
|
|
|
self,
|
|
|
|
params: QueryTypes = None,
|
|
|
|
headers: HeaderTypes = None,
|
|
|
|
cookies: CookieTypes = None,
|
|
|
|
version: Union[str, HTTPVersion] = HTTPVersion.H11,
|
|
|
|
timeout: Optional[float] = None,
|
|
|
|
proxy: Optional[str] = None,
|
|
|
|
) -> Session:
|
|
|
|
return Session(
|
|
|
|
params=params,
|
|
|
|
headers=headers,
|
|
|
|
cookies=cookies,
|
|
|
|
version=version,
|
|
|
|
timeout=timeout,
|
|
|
|
proxy=proxy,
|
|
|
|
)
|
2021-12-22 16:53:55 +08:00
|
|
|
|
|
|
|
|
2023-08-26 11:03:24 +08:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
2024-02-06 12:48:23 +08:00
|
|
|
class Driver(Mixin, NoneDriver): ...
|
2021-12-22 16:53:55 +08:00
|
|
|
|
2023-08-26 11:03:24 +08:00
|
|
|
else:
|
|
|
|
Driver = combine_driver(NoneDriver, Mixin)
|
|
|
|
"""HTTPX Driver"""
|