From ea21d6ffcc73a0d4fbbf08c9b925b83ab38ee15a Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Sun, 5 Jul 2020 20:39:34 +0800 Subject: [PATCH] add fastapi routes, adapters --- nonebot/__init__.py | 8 ++++---- nonebot/adapters/__init__.py | 8 ++++++++ nonebot/adapters/coolq.py | 10 ++++++++++ nonebot/drivers/__init__.py | 9 +++++++++ nonebot/drivers/fastapi.py | 25 ++++++++++++++++++++++--- nonebot/matcher.py | 8 ++++---- nonebot/message.py | 6 +++--- nonebot/plugin.py | 6 +++--- nonebot/rule.py | 2 +- 9 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 nonebot/adapters/__init__.py create mode 100644 nonebot/adapters/coolq.py diff --git a/nonebot/__init__.py b/nonebot/__init__.py index 35f6be00..ab5ac5a8 100644 --- a/nonebot/__init__.py +++ b/nonebot/__init__.py @@ -6,9 +6,9 @@ import importlib from typing import Optional from ipaddress import IPv4Address -from .log import logger -from .config import Env, Config -from .drivers import BaseDriver +from nonebot.log import logger +from nonebot.config import Env, Config +from nonebot.drivers import BaseDriver _driver: Optional[BaseDriver] = None @@ -48,4 +48,4 @@ def run(host: Optional[IPv4Address] = None, get_driver().run(host, port, *args, **kwargs) -from .plugin import load_plugins, get_loaded_plugins +from nonebot.plugin import load_plugins, get_loaded_plugins diff --git a/nonebot/adapters/__init__.py b/nonebot/adapters/__init__.py new file mode 100644 index 00000000..9d0346af --- /dev/null +++ b/nonebot/adapters/__init__.py @@ -0,0 +1,8 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + + +class BaseBot(object): + + def __init__(self): + raise NotImplementedError diff --git a/nonebot/adapters/coolq.py b/nonebot/adapters/coolq.py new file mode 100644 index 00000000..113be4a3 --- /dev/null +++ b/nonebot/adapters/coolq.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from nonebot.adapters import BaseBot + + +class Bot(BaseBot): + + def __init__(self): + pass diff --git a/nonebot/drivers/__init__.py b/nonebot/drivers/__init__.py index b40b7f52..15d8a8f1 100644 --- a/nonebot/drivers/__init__.py +++ b/nonebot/drivers/__init__.py @@ -7,6 +7,9 @@ from ipaddress import IPv4Address class BaseDriver(object): + def __init__(self): + raise NotImplementedError + @property def server_app(self): raise NotImplementedError @@ -25,3 +28,9 @@ class BaseDriver(object): *args, **kwargs): raise NotImplementedError + + async def _handle_http(self): + raise NotImplementedError + + async def _handle_ws_reverse(self): + raise NotImplementedError diff --git a/nonebot/drivers/fastapi.py b/nonebot/drivers/fastapi.py index 0795cf15..fde0c146 100644 --- a/nonebot/drivers/fastapi.py +++ b/nonebot/drivers/fastapi.py @@ -1,15 +1,16 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -import sys +import json import logging from typing import Optional from ipaddress import IPv4Address import uvicorn -from fastapi import FastAPI +from fastapi import Body, FastAPI, WebSocket -from . import BaseDriver +from nonebot.log import logger +from nonebot.drivers import BaseDriver class Driver(BaseDriver): @@ -24,6 +25,9 @@ class Driver(BaseDriver): self.config = config + self._server_app.post("/coolq/")(self._handle_http) + self._server_app.websocket("/coolq/ws")(self._handle_ws_reverse) + @property def server_app(self): return self._server_app @@ -76,3 +80,18 @@ class Driver(BaseDriver): debug=self.config.debug, log_config=LOGGING_CONFIG, **kwargs) + + async def _handle_http(self, data: dict = Body(...)): + logger.debug(f"Received message: {data}") + return {"status": 200, "message": "success"} + + async def _handle_ws_reverse(self, websocket: WebSocket): + await websocket.accept() + while True: + try: + data = await websocket.receive_json() + except json.decoder.JSONDecodeError as e: + logger.exception(e) + continue + + logger.debug(f"Received message: {data}") diff --git a/nonebot/matcher.py b/nonebot/matcher.py index 4355a42f..43914679 100644 --- a/nonebot/matcher.py +++ b/nonebot/matcher.py @@ -5,10 +5,10 @@ from functools import wraps from collections import defaultdict from typing import Type, List, Dict, Optional, Callable -from .event import Event -from .typing import Handler -from .rule import Rule, user -from .exception import PausedException, RejectedException, FinishedException +from nonebot.event import Event +from nonebot.typing import Handler +from nonebot.rule import Rule, user +from nonebot.exception import PausedException, RejectedException, FinishedException matchers: Dict[int, List[Type["Matcher"]]] = defaultdict(list) diff --git a/nonebot/message.py b/nonebot/message.py index 4fb3b8ed..f554261e 100644 --- a/nonebot/message.py +++ b/nonebot/message.py @@ -1,9 +1,9 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from .log import logger -from .event import Event -from .matcher import matchers +from nonebot.log import logger +from nonebot.event import Event +from nonebot.matcher import matchers async def handle_message(bot, event: Event): diff --git a/nonebot/plugin.py b/nonebot/plugin.py index 7f071425..5b724763 100644 --- a/nonebot/plugin.py +++ b/nonebot/plugin.py @@ -7,9 +7,9 @@ import importlib from types import ModuleType from typing import Set, Dict, Type, Optional -from .log import logger -from .rule import Rule -from .matcher import Matcher +from nonebot.rule import Rule +from nonebot.log import logger +from nonebot.matcher import Matcher plugins: Dict[str, "Plugin"] = {} diff --git a/nonebot/rule.py b/nonebot/rule.py index 82c68a8c..814069b9 100644 --- a/nonebot/rule.py +++ b/nonebot/rule.py @@ -4,7 +4,7 @@ import re from typing import Union, Callable, Optional -from .event import Event +from nonebot.event import Event class Rule: