2022-01-22 15:23:07 +08:00
|
|
|
"""[AIOHTTP](https://aiohttp.readthedocs.io/en/stable/) 驱动适配器。
|
|
|
|
|
|
|
|
```bash
|
|
|
|
nb driver install aiohttp
|
|
|
|
# 或者
|
|
|
|
pip install nonebot2[aiohttp]
|
|
|
|
```
|
2021-07-31 12:24:11 +08:00
|
|
|
|
2022-01-22 15:23:07 +08:00
|
|
|
:::tip 提示
|
2021-07-31 12:24:11 +08:00
|
|
|
本驱动仅支持客户端连接
|
2022-01-22 15:23:07 +08:00
|
|
|
:::
|
|
|
|
|
|
|
|
FrontMatter:
|
|
|
|
sidebar_position: 2
|
|
|
|
description: nonebot.drivers.aiohttp 模块
|
2021-06-21 01:22:33 +08:00
|
|
|
"""
|
|
|
|
|
2023-07-17 15:56:27 +08:00
|
|
|
from typing_extensions import override
|
2022-01-22 15:23:07 +08:00
|
|
|
from typing import Type, AsyncGenerator
|
2021-12-26 13:42:13 +08:00
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
2021-12-22 16:53:55 +08:00
|
|
|
from nonebot.drivers import Request, Response
|
2021-12-26 14:20:09 +08:00
|
|
|
from nonebot.exception import WebSocketClosed
|
2023-01-01 15:08:00 +08:00
|
|
|
from nonebot.drivers.none import Driver as NoneDriver
|
2021-09-25 19:14:16 +08:00
|
|
|
from nonebot.drivers import WebSocket as BaseWebSocket
|
2022-01-28 14:04:57 +08:00
|
|
|
from nonebot.drivers import HTTPVersion, ForwardMixin, ForwardDriver, combine_driver
|
2021-06-21 01:22:33 +08:00
|
|
|
|
2021-12-23 14:29:21 +08:00
|
|
|
try:
|
|
|
|
import aiohttp
|
2023-03-29 15:59:54 +08:00
|
|
|
except ModuleNotFoundError as e: # pragma: no cover
|
2021-12-23 14:29:21 +08:00
|
|
|
raise ImportError(
|
2023-06-24 14:47:35 +08:00
|
|
|
"Please install aiohttp first to use this driver. "
|
|
|
|
"Install with pip: `pip install nonebot2[aiohttp]`"
|
2023-02-09 10:24:27 +08:00
|
|
|
) from e
|
2021-12-23 14:29:21 +08:00
|
|
|
|
2021-06-21 01:22:33 +08:00
|
|
|
|
2021-12-23 17:20:26 +08:00
|
|
|
class Mixin(ForwardMixin):
|
2022-01-22 15:23:07 +08:00
|
|
|
"""AIOHTTP Mixin"""
|
|
|
|
|
2021-06-21 01:22:33 +08:00
|
|
|
@property
|
2023-07-17 15:56:27 +08:00
|
|
|
@override
|
2021-06-21 01:22:33 +08:00
|
|
|
def type(self) -> str:
|
|
|
|
return "aiohttp"
|
|
|
|
|
2023-07-17 15:56:27 +08:00
|
|
|
@override
|
2021-12-22 16:53:55 +08:00
|
|
|
async def request(self, setup: Request) -> Response:
|
|
|
|
if setup.version == HTTPVersion.H10:
|
|
|
|
version = aiohttp.HttpVersion10
|
|
|
|
elif setup.version == HTTPVersion.H11:
|
|
|
|
version = aiohttp.HttpVersion11
|
2021-07-20 15:35:56 +08:00
|
|
|
else:
|
2021-12-22 16:53:55 +08:00
|
|
|
raise RuntimeError(f"Unsupported HTTP version: {setup.version}")
|
2021-07-20 15:35:56 +08:00
|
|
|
|
2021-12-22 16:53:55 +08:00
|
|
|
timeout = aiohttp.ClientTimeout(setup.timeout)
|
2023-06-11 14:59:05 +08:00
|
|
|
|
|
|
|
data = setup.data
|
2021-12-24 23:15:28 +08:00
|
|
|
if setup.files:
|
2023-06-11 14:59:05 +08:00
|
|
|
data = aiohttp.FormData(data or {})
|
2021-12-24 23:15:28 +08:00
|
|
|
for name, file in setup.files:
|
2023-06-11 14:59:05 +08:00
|
|
|
data.add_field(name, file[1], content_type=file[2], filename=file[0])
|
2022-12-20 18:13:45 +08:00
|
|
|
|
|
|
|
cookies = {
|
|
|
|
cookie.name: cookie.value for cookie in setup.cookies if cookie.value
|
|
|
|
}
|
|
|
|
async with aiohttp.ClientSession(
|
|
|
|
cookies=cookies, version=version, trust_env=True
|
|
|
|
) as session:
|
2021-12-22 16:53:55 +08:00
|
|
|
async with session.request(
|
2021-11-22 23:21:26 +08:00
|
|
|
setup.method,
|
2021-12-22 16:53:55 +08:00
|
|
|
setup.url,
|
2023-06-11 14:59:05 +08:00
|
|
|
data=setup.content or data,
|
2021-12-24 23:15:28 +08:00
|
|
|
json=setup.json,
|
2021-12-22 16:53:55 +08:00
|
|
|
headers=setup.headers,
|
|
|
|
timeout=timeout,
|
2021-12-25 14:04:53 +08:00
|
|
|
proxy=setup.proxy,
|
2021-12-22 16:53:55 +08:00
|
|
|
) as response:
|
2022-12-20 18:13:45 +08:00
|
|
|
return Response(
|
2021-12-22 16:53:55 +08:00
|
|
|
response.status,
|
|
|
|
headers=response.headers.copy(),
|
|
|
|
content=await response.read(),
|
|
|
|
request=setup,
|
|
|
|
)
|
|
|
|
|
2023-07-17 15:56:27 +08:00
|
|
|
@override
|
2021-12-26 13:42:13 +08:00
|
|
|
@asynccontextmanager
|
|
|
|
async def websocket(self, setup: Request) -> AsyncGenerator["WebSocket", None]:
|
2021-12-22 16:53:55 +08:00
|
|
|
if setup.version == HTTPVersion.H10:
|
|
|
|
version = aiohttp.HttpVersion10
|
|
|
|
elif setup.version == HTTPVersion.H11:
|
|
|
|
version = aiohttp.HttpVersion11
|
|
|
|
else:
|
|
|
|
raise RuntimeError(f"Unsupported HTTP version: {setup.version}")
|
|
|
|
|
2021-12-27 02:26:02 +08:00
|
|
|
async with aiohttp.ClientSession(version=version, trust_env=True) as session:
|
|
|
|
async with session.ws_connect(
|
|
|
|
setup.url,
|
|
|
|
method=setup.method,
|
|
|
|
timeout=setup.timeout or 10,
|
|
|
|
headers=setup.headers,
|
|
|
|
proxy=setup.proxy,
|
|
|
|
) as ws:
|
2022-12-20 18:13:45 +08:00
|
|
|
yield WebSocket(request=setup, session=session, websocket=ws)
|
2021-07-19 01:20:17 +08:00
|
|
|
|
2021-07-31 12:24:11 +08:00
|
|
|
|
2021-07-20 15:35:56 +08:00
|
|
|
class WebSocket(BaseWebSocket):
|
2022-01-22 15:23:07 +08:00
|
|
|
"""AIOHTTP Websocket Wrapper"""
|
|
|
|
|
2021-12-22 16:53:55 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
*,
|
|
|
|
request: Request,
|
|
|
|
session: aiohttp.ClientSession,
|
|
|
|
websocket: aiohttp.ClientWebSocketResponse,
|
|
|
|
):
|
|
|
|
super().__init__(request=request)
|
|
|
|
self.session = session
|
|
|
|
self.websocket = websocket
|
2021-07-20 15:35:56 +08:00
|
|
|
|
|
|
|
@property
|
2023-07-17 15:56:27 +08:00
|
|
|
@override
|
2021-07-20 15:35:56 +08:00
|
|
|
def closed(self):
|
|
|
|
return self.websocket.closed
|
|
|
|
|
2023-07-17 15:56:27 +08:00
|
|
|
@override
|
2021-07-20 15:35:56 +08:00
|
|
|
async def accept(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2023-07-17 15:56:27 +08:00
|
|
|
@override
|
2021-07-20 15:35:56 +08:00
|
|
|
async def close(self, code: int = 1000):
|
|
|
|
await self.websocket.close(code=code)
|
2021-12-22 16:53:55 +08:00
|
|
|
await self.session.close()
|
2021-07-20 15:35:56 +08:00
|
|
|
|
2021-12-26 14:20:09 +08:00
|
|
|
async def _receive(self) -> aiohttp.WSMessage:
|
|
|
|
msg = await self.websocket.receive()
|
|
|
|
if msg.type in (aiohttp.WSMsgType.CLOSE, aiohttp.WSMsgType.CLOSING):
|
|
|
|
raise WebSocketClosed(self.websocket.close_code or 1006)
|
|
|
|
return msg
|
|
|
|
|
2023-07-17 15:56:27 +08:00
|
|
|
@override
|
2021-07-20 15:35:56 +08:00
|
|
|
async def receive(self) -> str:
|
2022-05-14 21:06:57 +08:00
|
|
|
msg = await self._receive()
|
|
|
|
if msg.type not in (aiohttp.WSMsgType.TEXT, aiohttp.WSMsgType.BINARY):
|
|
|
|
raise TypeError(
|
|
|
|
f"WebSocket received unexpected frame type: {msg.type}, {msg.data!r}"
|
|
|
|
)
|
|
|
|
return msg.data
|
|
|
|
|
2023-07-17 15:56:27 +08:00
|
|
|
@override
|
2022-05-14 21:06:57 +08:00
|
|
|
async def receive_text(self) -> str:
|
2021-12-26 14:20:09 +08:00
|
|
|
msg = await self._receive()
|
|
|
|
if msg.type != aiohttp.WSMsgType.TEXT:
|
2022-05-14 21:06:57 +08:00
|
|
|
raise TypeError(
|
|
|
|
f"WebSocket received unexpected frame type: {msg.type}, {msg.data!r}"
|
|
|
|
)
|
2021-12-26 14:20:09 +08:00
|
|
|
return msg.data
|
2021-07-20 15:35:56 +08:00
|
|
|
|
2023-07-17 15:56:27 +08:00
|
|
|
@override
|
2021-07-20 15:35:56 +08:00
|
|
|
async def receive_bytes(self) -> bytes:
|
2021-12-26 14:20:09 +08:00
|
|
|
msg = await self._receive()
|
2022-05-14 21:06:57 +08:00
|
|
|
if msg.type != aiohttp.WSMsgType.BINARY:
|
|
|
|
raise TypeError(
|
|
|
|
f"WebSocket received unexpected frame type: {msg.type}, {msg.data!r}"
|
|
|
|
)
|
2021-12-26 14:20:09 +08:00
|
|
|
return msg.data
|
2021-07-20 15:35:56 +08:00
|
|
|
|
2023-07-17 15:56:27 +08:00
|
|
|
@override
|
2022-05-14 21:06:57 +08:00
|
|
|
async def send_text(self, data: str) -> None:
|
2021-07-20 15:35:56 +08:00
|
|
|
await self.websocket.send_str(data)
|
|
|
|
|
2023-07-17 15:56:27 +08:00
|
|
|
@override
|
2021-07-20 15:35:56 +08:00
|
|
|
async def send_bytes(self, data: bytes) -> None:
|
|
|
|
await self.websocket.send_bytes(data)
|
2021-12-22 16:53:55 +08:00
|
|
|
|
|
|
|
|
2023-01-01 15:08:00 +08:00
|
|
|
Driver: Type[ForwardDriver] = combine_driver(NoneDriver, Mixin) # type: ignore
|
2022-01-22 15:23:07 +08:00
|
|
|
"""AIOHTTP Driver"""
|