use dynamic routing in quart driver

This commit is contained in:
Mix 2021-02-06 11:37:57 +08:00
parent c537841bc1
commit 4f7a033b9c

View File

@ -32,7 +32,8 @@ try:
from quart import request as _request from quart import request as _request
from quart import websocket as _websocket from quart import websocket as _websocket
except ImportError: except ImportError:
raise ValueError('Quart not fount, please install quart first') raise ValueError(
'Please install Quart by using `pip install nonebot2[quart]`')
_AsyncCallable = TypeVar("_AsyncCallable", bound=Callable[..., Coroutine]) _AsyncCallable = TypeVar("_AsyncCallable", bound=Callable[..., Coroutine])
@ -43,7 +44,7 @@ class Driver(BaseDriver):
:上报地址: :上报地址:
* ``/{adapter name}/http/``: HTTP POST 上报 * ``/{adapter name}/http``: HTTP POST 上报
* ``/{adapter name}/ws``: WebSocket 上报 * ``/{adapter name}/ws``: WebSocket 上报
""" """
@ -52,22 +53,11 @@ class Driver(BaseDriver):
super().__init__(env, config) super().__init__(env, config)
self._server_app = Quart(self.__class__.__qualname__) self._server_app = Quart(self.__class__.__qualname__)
self._server_app.add_url_rule('/<adapter>/http',
@overrides(BaseDriver) methods=['POST'],
def register_adapter(self, name: str, adapter: Type["Bot"], **kwargs): view_func=self._handle_http)
"""向 Quart 路由添加对应 adapter 响应的 handler""" self._server_app.add_websocket('/<adapter>/ws',
if name in self._adapters: view_func=self._handle_ws_reverse)
return
super().register_adapter(name, adapter, **kwargs)
@self.server_app.route(f'/{name}/http', endpoint=name + '_http')
async def _http_handler():
await self._handle_http(name)
@self.server_app.websocket(f'/{name}/ws', endpoint=name + '_ws')
async def _ws_handler():
await self._handle_ws_reverse(name)
@property @property
@overrides(BaseDriver) @overrides(BaseDriver)