From 9d047daef5b90c902f1398b57780a7c1a80d95ed Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Tue, 12 Jan 2021 17:55:27 +0800 Subject: [PATCH] :construction: move cqhttp config into adapter --- nonebot/adapters/cqhttp/bot.py | 13 +++++++++---- nonebot/adapters/cqhttp/config.py | 12 ++++++++++++ nonebot/config.py | 3 +++ 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 nonebot/adapters/cqhttp/config.py diff --git a/nonebot/adapters/cqhttp/bot.py b/nonebot/adapters/cqhttp/bot.py index d62ff829..efc9a2d8 100644 --- a/nonebot/adapters/cqhttp/bot.py +++ b/nonebot/adapters/cqhttp/bot.py @@ -14,6 +14,7 @@ from nonebot.adapters import Bot as BaseBot from nonebot.exception import RequestDenied from .utils import log, escape +from .config import Config as CQHTTPConfig from .message import Message, MessageSegment from .event import Reply, Event, MessageEvent, get_event_model from .exception import NetworkError, ApiNotAvailable, ActionFailed @@ -226,6 +227,8 @@ class Bot(BaseBot): *, websocket: Optional["WebSocket"] = None): + self.cqhttp_config = CQHTTPConfig(**config.dict()) + super().__init__(driver, connection_type, config, @@ -252,6 +255,7 @@ class Bot(BaseBot): x_self_id = headers.get("x-self-id") x_signature = headers.get("x-signature") token = get_auth_bearer(headers.get("authorization")) + cqhttp_config = CQHTTPConfig(**driver.config.dict()) # 检查连接方式 if connection_type not in ["http", "websocket"]: @@ -264,7 +268,7 @@ class Bot(BaseBot): raise RequestDenied(400, "Missing X-Self-ID Header") # 检查签名 - secret = driver.config.secret + secret = cqhttp_config.cqhttp_secret if secret and connection_type == "http": if not x_signature: log("WARNING", "Missing Signature Header") @@ -275,7 +279,7 @@ class Bot(BaseBot): log("WARNING", "Signature Header is invalid") raise RequestDenied(403, "Signature is invalid") - access_token = driver.config.access_token + access_token = cqhttp_config.cqhttp_access_token if access_token and access_token != token: log( "WARNING", "Authorization Header is invalid" @@ -374,8 +378,9 @@ class Bot(BaseBot): api_root += "/" headers = {} - if self.config.access_token is not None: - headers["Authorization"] = "Bearer " + self.config.access_token + if self.cqhttp_config.cqhttp_access_token is not None: + headers[ + "Authorization"] = "Bearer " + self.cqhttp_config.cqhttp_access_token try: async with httpx.AsyncClient(headers=headers) as client: diff --git a/nonebot/adapters/cqhttp/config.py b/nonebot/adapters/cqhttp/config.py new file mode 100644 index 00000000..ff30f172 --- /dev/null +++ b/nonebot/adapters/cqhttp/config.py @@ -0,0 +1,12 @@ +from typing import Optional + +from pydantic import Field, BaseSettings + + +class Config(BaseSettings): + cqhttp_access_token: Optional[str] = Field(default=None, + alias="access_token") + cqhttp_secret: Optional[str] = Field(default=None, alias="secret") + + class Config: + extra = "ignore" diff --git a/nonebot/config.py b/nonebot/config.py index af5f67c1..ae5b6c4b 100644 --- a/nonebot/config.py +++ b/nonebot/config.py @@ -276,6 +276,9 @@ class Config(BaseConfig): SESSION_EXPIRE_TIMEOUT=P[DD]DT[HH]H[MM]M[SS]S # ISO 8601 """ + # adapter configs + # adapter configs are defined in adapter/config.py + # custom configs # custom configs can be assigned during nonebot.init # or from env file using json loads