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:
|
|
|
|
sidebar_position: 3
|
|
|
|
description: nonebot.drivers.httpx 模块
|
|
|
|
"""
|
2023-06-24 14:47:35 +08:00
|
|
|
|
2023-08-26 11:03:24 +08:00
|
|
|
from typing import TYPE_CHECKING
|
2023-07-17 15:56:27 +08:00
|
|
|
from typing_extensions import override
|
2021-12-26 13:42:13 +08:00
|
|
|
|
2023-01-01 15:08:00 +08:00
|
|
|
from nonebot.drivers.none import Driver as NoneDriver
|
2021-12-22 16:53:55 +08:00
|
|
|
from nonebot.drivers import (
|
|
|
|
Request,
|
|
|
|
Response,
|
|
|
|
HTTPVersion,
|
2023-08-26 11:03:24 +08:00
|
|
|
HTTPClientMixin,
|
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
|
|
|
|
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:
|
|
|
|
async with httpx.AsyncClient(
|
2022-12-20 18:13:45 +08:00
|
|
|
cookies=setup.cookies.jar,
|
2021-12-25 14:04:53 +08:00
|
|
|
http2=setup.version == HTTPVersion.H2,
|
2022-12-20 18:13:45 +08:00
|
|
|
proxies=setup.proxy,
|
2021-12-25 14:04:53 +08:00
|
|
|
follow_redirects=True,
|
2021-12-22 16:53:55 +08:00
|
|
|
) as client:
|
|
|
|
response = await client.request(
|
|
|
|
setup.method,
|
|
|
|
str(setup.url),
|
2022-12-20 18:13:45 +08:00
|
|
|
content=setup.content,
|
|
|
|
data=setup.data,
|
2021-12-24 23:15:28 +08:00
|
|
|
json=setup.json,
|
2022-12-20 18:13:45 +08:00
|
|
|
files=setup.files,
|
2021-12-22 16:53:55 +08:00
|
|
|
headers=tuple(setup.headers.items()),
|
|
|
|
timeout=setup.timeout,
|
|
|
|
)
|
|
|
|
return Response(
|
|
|
|
response.status_code,
|
2022-05-14 21:06:57 +08:00
|
|
|
headers=response.headers.multi_items(),
|
2021-12-22 16:53:55 +08:00
|
|
|
content=response.content,
|
|
|
|
request=setup,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-26 11:03:24 +08:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
|
|
|
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"""
|