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 importlib
|
||||||
import pkg_resources
|
import pkg_resources
|
||||||
from typing import Dict, Type, Optional
|
from typing import Any, Dict, Type, Optional
|
||||||
|
|
||||||
from nonebot.adapters import Bot
|
from nonebot.adapters import Bot
|
||||||
from nonebot.drivers import Driver
|
|
||||||
from nonebot.utils import escape_tag
|
from nonebot.utils import escape_tag
|
||||||
from nonebot.config import Env, Config
|
from nonebot.config import Env, Config
|
||||||
from nonebot.log import logger, default_filter
|
from nonebot.log import logger, default_filter
|
||||||
|
from nonebot.drivers import Driver, ForwardDriver, ReverseDriver
|
||||||
|
|
||||||
try:
|
try:
|
||||||
_dist: pkg_resources.Distribution = pkg_resources.get_distribution(
|
_dist: pkg_resources.Distribution = pkg_resources.get_distribution(
|
||||||
@ -76,8 +76,7 @@ def get_driver() -> Driver:
|
|||||||
return _driver
|
return _driver
|
||||||
|
|
||||||
|
|
||||||
# FIXME: app for reverse driver
|
def get_app() -> Any:
|
||||||
def get_app():
|
|
||||||
"""
|
"""
|
||||||
:说明:
|
:说明:
|
||||||
|
|
||||||
@ -99,11 +98,13 @@ def get_app():
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
driver = get_driver()
|
driver = get_driver()
|
||||||
|
assert isinstance(
|
||||||
|
driver,
|
||||||
|
ReverseDriver), "app object is only available for reverse driver"
|
||||||
return driver.server_app
|
return driver.server_app
|
||||||
|
|
||||||
|
|
||||||
# FIXME: asgi for reverse driver
|
def get_asgi() -> Any:
|
||||||
def get_asgi():
|
|
||||||
"""
|
"""
|
||||||
:说明:
|
:说明:
|
||||||
|
|
||||||
@ -125,6 +126,9 @@ def get_asgi():
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
driver = get_driver()
|
driver = get_driver()
|
||||||
|
assert isinstance(
|
||||||
|
driver,
|
||||||
|
ReverseDriver), "asgi object is only available for reverse driver"
|
||||||
return driver.asgi
|
return driver.asgi
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
import abc
|
import abc
|
||||||
import asyncio
|
import asyncio
|
||||||
from dataclasses import dataclass, field
|
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.log import logger
|
||||||
from nonebot.config import Env, Config
|
from nonebot.config import Env, Config
|
||||||
@ -199,6 +199,7 @@ class Driver(abc.ABC):
|
|||||||
asyncio.create_task(_run_hook(bot))
|
asyncio.create_task(_run_hook(bot))
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: issue #240
|
||||||
class ForwardDriver(Driver):
|
class ForwardDriver(Driver):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -210,26 +211,16 @@ class ReverseDriver(Driver):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def server_app(self):
|
def server_app(self) -> Any:
|
||||||
"""驱动 APP 对象"""
|
"""驱动 APP 对象"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def asgi(self):
|
def asgi(self) -> Any:
|
||||||
"""驱动 ASGI 对象"""
|
"""驱动 ASGI 对象"""
|
||||||
raise NotImplementedError
|
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
|
@dataclass
|
||||||
class HTTPConnection(abc.ABC):
|
class HTTPConnection(abc.ABC):
|
||||||
|
@ -118,7 +118,7 @@ class Driver(ReverseDriver):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
@overrides(ReverseDriver)
|
@overrides(ReverseDriver)
|
||||||
def asgi(self):
|
def asgi(self) -> FastAPI:
|
||||||
"""``FastAPI APP`` 对象"""
|
"""``FastAPI APP`` 对象"""
|
||||||
return self._server_app
|
return self._server_app
|
||||||
|
|
||||||
@ -175,7 +175,6 @@ class Driver(ReverseDriver):
|
|||||||
log_config=LOGGING_CONFIG,
|
log_config=LOGGING_CONFIG,
|
||||||
**kwargs)
|
**kwargs)
|
||||||
|
|
||||||
@overrides(ReverseDriver)
|
|
||||||
async def _handle_http(self, adapter: str, request: Request):
|
async def _handle_http(self, adapter: str, request: Request):
|
||||||
data = await request.body()
|
data = await request.body()
|
||||||
|
|
||||||
@ -209,7 +208,6 @@ class Driver(ReverseDriver):
|
|||||||
return Response(response and response.body,
|
return Response(response and response.body,
|
||||||
response and response.status or 200)
|
response and response.status or 200)
|
||||||
|
|
||||||
@overrides(ReverseDriver)
|
|
||||||
async def _handle_ws_reverse(self, adapter: str,
|
async def _handle_ws_reverse(self, adapter: str,
|
||||||
websocket: FastAPIWebSocket):
|
websocket: FastAPIWebSocket):
|
||||||
ws = WebSocket(websocket.scope.get("http_version",
|
ws = WebSocket(websocket.scope.get("http_version",
|
||||||
|
Loading…
Reference in New Issue
Block a user