nonebot2/nonebot/drivers/websockets.py

143 lines
3.8 KiB
Python
Raw Permalink Normal View History

2022-01-22 15:23:07 +08:00
"""[websockets](https://websockets.readthedocs.io/) 驱动适配
```bash
nb driver install websockets
# 或者
pip install nonebot2[websockets]
```
:::tip 提示
本驱动仅支持客户端 WebSocket 连接
:::
FrontMatter:
mdx:
format: md
2022-01-22 15:23:07 +08:00
sidebar_position: 4
description: nonebot.drivers.websockets 模块
"""
2021-12-22 16:53:55 +08:00
import logging
from functools import wraps
from contextlib import asynccontextmanager
from typing_extensions import ParamSpec, override
from collections.abc import Coroutine, AsyncGenerator
from typing import TYPE_CHECKING, Any, Union, TypeVar, Callable
2021-12-22 16:53:55 +08:00
from nonebot.drivers import Request
2021-12-22 16:53:55 +08:00
from nonebot.log import LoguruHandler
from nonebot.exception import WebSocketClosed
from nonebot.drivers.none import Driver as NoneDriver
2021-12-22 16:53:55 +08:00
from nonebot.drivers import WebSocket as BaseWebSocket
from nonebot.drivers import WebSocketClientMixin, combine_driver
2021-12-22 16:53:55 +08:00
try:
from websockets.exceptions import ConnectionClosed
from websockets.legacy.client import Connect, WebSocketClientProtocol
except ModuleNotFoundError as e: # pragma: no cover
raise ImportError(
"Please install websockets first to use this driver. "
"Install with pip: `pip install nonebot2[websockets]`"
) from e
T = TypeVar("T")
P = ParamSpec("P")
2021-12-22 16:53:55 +08:00
logger = logging.Logger("websockets.client", "INFO")
logger.addHandler(LoguruHandler())
def catch_closed(
func: Callable[P, Coroutine[Any, Any, T]]
) -> Callable[P, Coroutine[Any, Any, T]]:
@wraps(func)
async def decorator(*args: P.args, **kwargs: P.kwargs) -> T:
try:
return await func(*args, **kwargs)
except ConnectionClosed as e:
raise WebSocketClosed(e.code, e.reason)
return decorator
class Mixin(WebSocketClientMixin):
2022-01-22 15:23:07 +08:00
"""Websockets Mixin"""
2021-12-22 16:53:55 +08:00
@property
@override
2021-12-22 16:53:55 +08:00
def type(self) -> str:
return "websockets"
@override
@asynccontextmanager
async def websocket(self, setup: Request) -> AsyncGenerator["WebSocket", None]:
if setup.proxy is not None:
logger.warning("proxy is not supported by websockets driver")
connection = Connect(
2021-12-22 16:53:55 +08:00
str(setup.url),
extra_headers={**setup.headers, **setup.cookies.as_header(setup)},
2021-12-22 16:53:55 +08:00
open_timeout=setup.timeout,
)
async with connection as ws:
yield WebSocket(request=setup, websocket=ws)
2021-12-22 16:53:55 +08:00
class WebSocket(BaseWebSocket):
2022-01-22 15:23:07 +08:00
"""Websockets WebSocket Wrapper"""
@override
2021-12-22 16:53:55 +08:00
def __init__(self, *, request: Request, websocket: WebSocketClientProtocol):
super().__init__(request=request)
self.websocket = websocket
@property
@override
2021-12-22 16:53:55 +08:00
def closed(self) -> bool:
return self.websocket.closed
@override
2021-12-22 16:53:55 +08:00
async def accept(self):
raise NotImplementedError
@override
2021-12-22 16:53:55 +08:00
async def close(self, code: int = 1000, reason: str = ""):
await self.websocket.close(code, reason)
@override
@catch_closed
async def receive(self) -> Union[str, bytes]:
return await self.websocket.recv()
@override
@catch_closed
async def receive_text(self) -> str:
2021-12-22 16:53:55 +08:00
msg = await self.websocket.recv()
if isinstance(msg, bytes):
raise TypeError("WebSocket received unexpected frame type: bytes")
return msg
@override
@catch_closed
2021-12-22 16:53:55 +08:00
async def receive_bytes(self) -> bytes:
msg = await self.websocket.recv()
if isinstance(msg, str):
raise TypeError("WebSocket received unexpected frame type: str")
return msg
@override
async def send_text(self, data: str) -> None:
2021-12-22 16:53:55 +08:00
await self.websocket.send(data)
@override
2021-12-22 16:53:55 +08:00
async def send_bytes(self, data: bytes) -> None:
await self.websocket.send(data)
if TYPE_CHECKING:
class Driver(Mixin, NoneDriver): ...
else:
Driver = combine_driver(NoneDriver, Mixin)
"""Websockets Driver"""