nonebot2/nonebot/internal/adapter/adapter.py

112 lines
3.7 KiB
Python
Raw Normal View History

2021-12-06 22:19:05 +08:00
import abc
from contextlib import asynccontextmanager
from typing import Any, Dict, AsyncGenerator
2021-12-06 22:19:05 +08:00
from nonebot.config import Config
2022-02-06 17:08:11 +08:00
from nonebot.internal.driver import (
2021-12-06 22:19:05 +08:00
Driver,
2021-12-20 00:28:02 +08:00
Request,
Response,
ASGIMixin,
2021-12-20 00:28:02 +08:00
WebSocket,
HTTPClientMixin,
2021-12-06 22:19:05 +08:00
HTTPServerSetup,
WebSocketClientMixin,
2021-12-06 22:19:05 +08:00
WebSocketServerSetup,
)
from .bot import Bot
2022-01-15 21:27:43 +08:00
2021-12-06 22:19:05 +08:00
class Adapter(abc.ABC):
2022-01-20 14:49:46 +08:00
"""协议适配器基类。
通常 Adapter 中编写协议通信相关代码: 建立通信连接处理接收与发送 data
参数:
driver: {ref}`nonebot.drivers.Driver` 实例
kwargs: 其他由 {ref}`nonebot.drivers.Driver.register_adapter` 传入的额外参数
"""
2021-12-06 22:19:05 +08:00
def __init__(self, driver: Driver, **kwargs: Any):
2021-12-07 02:16:18 +08:00
self.driver: Driver = driver
2022-01-20 14:49:46 +08:00
"""{ref}`nonebot.drivers.Driver` 实例"""
2021-12-06 22:19:05 +08:00
self.bots: Dict[str, Bot] = {}
"""本协议适配器已建立连接的 {ref}`nonebot.adapters.Bot` 实例"""
2021-12-06 22:19:05 +08:00
def __repr__(self) -> str:
return f"Adapter(name={self.get_name()!r})"
2021-12-06 22:19:05 +08:00
@classmethod
@abc.abstractmethod
def get_name(cls) -> str:
2022-01-20 14:49:46 +08:00
"""当前协议适配器的名称"""
2021-12-06 22:19:05 +08:00
raise NotImplementedError
@property
def config(self) -> Config:
2022-01-20 14:49:46 +08:00
"""全局 NoneBot 配置"""
2021-12-06 22:19:05 +08:00
return self.driver.config
2021-12-21 18:22:14 +08:00
def bot_connect(self, bot: Bot) -> None:
"""告知 NoneBot 建立了一个新的 {ref}`nonebot.adapters.Bot` 连接。
2022-01-20 14:49:46 +08:00
当有新的 {ref}`nonebot.adapters.Bot` 实例连接建立成功时调用
2022-01-20 14:49:46 +08:00
参数:
bot: {ref}`nonebot.adapters.Bot` 实例
2022-01-20 14:49:46 +08:00
"""
2021-12-06 22:19:05 +08:00
self.driver._bot_connect(bot)
self.bots[bot.self_id] = bot
2021-12-21 18:22:14 +08:00
def bot_disconnect(self, bot: Bot) -> None:
"""告知 NoneBot {ref}`nonebot.adapters.Bot` 连接已断开。
2022-01-20 14:49:46 +08:00
当有 {ref}`nonebot.adapters.Bot` 实例连接断开时调用
2022-01-20 14:49:46 +08:00
参数:
bot: {ref}`nonebot.adapters.Bot` 实例
2022-01-20 14:49:46 +08:00
"""
if self.bots.pop(bot.self_id, None) is None:
raise RuntimeError(f"{bot} not found in adapter {self.get_name()}")
2021-12-06 22:19:05 +08:00
self.driver._bot_disconnect(bot)
def setup_http_server(self, setup: HTTPServerSetup):
2022-01-20 14:49:46 +08:00
"""设置一个 HTTP 服务器路由配置"""
if not isinstance(self.driver, ASGIMixin):
2021-12-06 22:19:05 +08:00
raise TypeError("Current driver does not support http server")
self.driver.setup_http_server(setup)
def setup_websocket_server(self, setup: WebSocketServerSetup):
2022-01-20 14:49:46 +08:00
"""设置一个 WebSocket 服务器路由配置"""
if not isinstance(self.driver, ASGIMixin):
2021-12-06 22:19:05 +08:00
raise TypeError("Current driver does not support websocket server")
self.driver.setup_websocket_server(setup)
2021-12-20 00:28:02 +08:00
async def request(self, setup: Request) -> Response:
2022-01-20 14:49:46 +08:00
"""进行一个 HTTP 客户端请求"""
if not isinstance(self.driver, HTTPClientMixin):
2021-12-20 00:28:02 +08:00
raise TypeError("Current driver does not support http client")
return await self.driver.request(setup)
@asynccontextmanager
async def websocket(self, setup: Request) -> AsyncGenerator[WebSocket, None]:
2022-01-20 14:49:46 +08:00
"""建立一个 WebSocket 客户端连接请求"""
if not isinstance(self.driver, WebSocketClientMixin):
2021-12-20 00:28:02 +08:00
raise TypeError("Current driver does not support websocket client")
async with self.driver.websocket(setup) as ws:
yield ws
2021-12-20 00:28:02 +08:00
2021-12-06 22:19:05 +08:00
@abc.abstractmethod
2022-01-20 14:49:46 +08:00
async def _call_api(self, bot: Bot, api: str, **data: Any) -> Any:
"""`Adapter` 实际调用 api 的逻辑实现函数,实现该方法以调用 api。
2021-12-06 22:19:05 +08:00
2022-01-12 18:31:12 +08:00
参数:
2022-01-12 19:10:29 +08:00
api: API 名称
2022-01-20 14:49:46 +08:00
data: API 数据
2021-12-06 22:19:05 +08:00
"""
raise NotImplementedError
2022-01-20 14:49:46 +08:00
__autodoc__ = {"Adapter._call_api": True}