mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-09-07 04:26:45 +00:00
🎨 update typing support
This commit is contained in:
@ -6,10 +6,13 @@
|
||||
"""
|
||||
|
||||
import abc
|
||||
from typing import Dict, Type, Optional, Callable, TYPE_CHECKING
|
||||
|
||||
from nonebot.log import logger
|
||||
from nonebot.config import Env, Config
|
||||
from nonebot.typing import Bot, Dict, Type, Union, Optional, Callable
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nonebot.adapters import BaseBot as Bot
|
||||
|
||||
|
||||
class BaseDriver(abc.ABC):
|
||||
@ -17,7 +20,7 @@ class BaseDriver(abc.ABC):
|
||||
Driver 基类。将后端框架封装,以满足适配器使用。
|
||||
"""
|
||||
|
||||
_adapters: Dict[str, Type[Bot]] = {}
|
||||
_adapters: Dict[str, Type["Bot"]] = {}
|
||||
"""
|
||||
:类型: ``Dict[str, Type[Bot]]``
|
||||
:说明: 已注册的适配器列表
|
||||
@ -41,14 +44,14 @@ class BaseDriver(abc.ABC):
|
||||
:类型: ``Config``
|
||||
:说明: 配置对象
|
||||
"""
|
||||
self._clients: Dict[str, Bot] = {}
|
||||
self._clients: Dict[str, "Bot"] = {}
|
||||
"""
|
||||
:类型: ``Dict[str, Bot]``
|
||||
:说明: 已连接的 Bot
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def register_adapter(cls, name: str, adapter: Type[Bot]):
|
||||
def register_adapter(cls, name: str, adapter: Type["Bot"]):
|
||||
"""
|
||||
:说明:
|
||||
|
||||
@ -88,7 +91,7 @@ class BaseDriver(abc.ABC):
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def bots(self) -> Dict[str, Bot]:
|
||||
def bots(self) -> Dict[str, "Bot"]:
|
||||
"""
|
||||
:类型: ``Dict[str, Bot]``
|
||||
:说明: 获取当前所有已连接的 Bot
|
||||
|
@ -8,34 +8,22 @@ FastAPI 驱动适配
|
||||
https://fastapi.tiangolo.com/
|
||||
"""
|
||||
|
||||
import hmac
|
||||
import json
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import Optional, Callable
|
||||
|
||||
import uvicorn
|
||||
from fastapi.responses import Response
|
||||
from fastapi import Body, status, Header, Request, FastAPI, Depends, HTTPException
|
||||
from fastapi import Body, status, Request, FastAPI, HTTPException
|
||||
from starlette.websockets import WebSocketDisconnect, WebSocket as FastAPIWebSocket
|
||||
|
||||
from nonebot.log import logger
|
||||
from nonebot.typing import overrides
|
||||
from nonebot.config import Env, Config
|
||||
from nonebot.utils import DataclassEncoder
|
||||
from nonebot.exception import RequestDenied
|
||||
from nonebot.drivers import BaseDriver, BaseWebSocket
|
||||
from nonebot.typing import Optional, Callable, overrides
|
||||
|
||||
|
||||
def get_auth_bearer(access_token: Optional[str] = Header(
|
||||
None, alias="Authorization")):
|
||||
if not access_token:
|
||||
return None
|
||||
scheme, _, param = access_token.partition(" ")
|
||||
if scheme.lower() not in ["bearer", "token"]:
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Not authenticated",
|
||||
headers={"WWW-Authenticate": "Bearer"})
|
||||
return param
|
||||
|
||||
|
||||
class Driver(BaseDriver):
|
||||
|
Reference in New Issue
Block a user