mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-01-19 01:18:19 +08:00
🎨 change reverse driver abstract methods
This commit is contained in:
parent
8e6f8fada8
commit
76c830739c
@ -30,13 +30,13 @@
|
||||
|
||||
import importlib
|
||||
import pkg_resources
|
||||
from typing import Dict, Type, Optional
|
||||
from typing import Any, Dict, Type, Optional
|
||||
|
||||
from nonebot.adapters import Bot
|
||||
from nonebot.drivers import Driver
|
||||
from nonebot.utils import escape_tag
|
||||
from nonebot.config import Env, Config
|
||||
from nonebot.log import logger, default_filter
|
||||
from nonebot.drivers import Driver, ForwardDriver, ReverseDriver
|
||||
|
||||
try:
|
||||
_dist: pkg_resources.Distribution = pkg_resources.get_distribution(
|
||||
@ -76,8 +76,7 @@ def get_driver() -> Driver:
|
||||
return _driver
|
||||
|
||||
|
||||
# FIXME: app for reverse driver
|
||||
def get_app():
|
||||
def get_app() -> Any:
|
||||
"""
|
||||
:说明:
|
||||
|
||||
@ -99,11 +98,13 @@ def get_app():
|
||||
|
||||
"""
|
||||
driver = get_driver()
|
||||
assert isinstance(
|
||||
driver,
|
||||
ReverseDriver), "app object is only available for reverse driver"
|
||||
return driver.server_app
|
||||
|
||||
|
||||
# FIXME: asgi for reverse driver
|
||||
def get_asgi():
|
||||
def get_asgi() -> Any:
|
||||
"""
|
||||
:说明:
|
||||
|
||||
@ -125,6 +126,9 @@ def get_asgi():
|
||||
|
||||
"""
|
||||
driver = get_driver()
|
||||
assert isinstance(
|
||||
driver,
|
||||
ReverseDriver), "asgi object is only available for reverse driver"
|
||||
return driver.asgi
|
||||
|
||||
|
||||
|
@ -8,7 +8,7 @@
|
||||
import abc
|
||||
import asyncio
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Set, Dict, Type, Optional, Callable, TYPE_CHECKING
|
||||
from typing import Any, Set, Dict, Type, Optional, Callable, TYPE_CHECKING
|
||||
|
||||
from nonebot.log import logger
|
||||
from nonebot.config import Env, Config
|
||||
@ -199,6 +199,7 @@ class Driver(abc.ABC):
|
||||
asyncio.create_task(_run_hook(bot))
|
||||
|
||||
|
||||
# TODO: issue #240
|
||||
class ForwardDriver(Driver):
|
||||
pass
|
||||
|
||||
@ -210,26 +211,16 @@ class ReverseDriver(Driver):
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def server_app(self):
|
||||
def server_app(self) -> Any:
|
||||
"""驱动 APP 对象"""
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def asgi(self):
|
||||
def asgi(self) -> Any:
|
||||
"""驱动 ASGI 对象"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
async def _handle_http(self, *args, **kwargs):
|
||||
"""用于处理 HTTP 类型请求的函数"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
async def _handle_ws_reverse(self, *args, **kwargs):
|
||||
"""用于处理 WebSocket 类型请求的函数"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@dataclass
|
||||
class HTTPConnection(abc.ABC):
|
||||
|
@ -118,7 +118,7 @@ class Driver(ReverseDriver):
|
||||
|
||||
@property
|
||||
@overrides(ReverseDriver)
|
||||
def asgi(self):
|
||||
def asgi(self) -> FastAPI:
|
||||
"""``FastAPI APP`` 对象"""
|
||||
return self._server_app
|
||||
|
||||
@ -175,7 +175,6 @@ class Driver(ReverseDriver):
|
||||
log_config=LOGGING_CONFIG,
|
||||
**kwargs)
|
||||
|
||||
@overrides(ReverseDriver)
|
||||
async def _handle_http(self, adapter: str, request: Request):
|
||||
data = await request.body()
|
||||
|
||||
@ -209,7 +208,6 @@ class Driver(ReverseDriver):
|
||||
return Response(response and response.body,
|
||||
response and response.status or 200)
|
||||
|
||||
@overrides(ReverseDriver)
|
||||
async def _handle_ws_reverse(self, adapter: str,
|
||||
websocket: FastAPIWebSocket):
|
||||
ws = WebSocket(websocket.scope.get("http_version",
|
||||
|
Loading…
Reference in New Issue
Block a user