Skip to content

Commit

Permalink
🐛 修复 API 开关不生效的 Bug (#288)
Browse files Browse the repository at this point in the history
* 🐛 修复 API 开关不生效的 Bug

* ✨ 新增没有可用 API 的处理
  • Loading branch information
djkcyl authored Jan 16, 2025
1 parent 8f748c5 commit b02d3b0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 13 additions & 6 deletions nonebot_plugin_bilichat/request_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
import sys

from nonebot.log import logger
from yarl import URL
Expand All @@ -9,12 +10,15 @@

request_apis: list[RequestAPI] = []
for api in config.api.request_api:
try:
request_api = RequestAPI(URL(api.api), api.token, api.weight, api.note)
request_apis.append(request_api)
logger.info(f"API {api.api} 初始化成功, 权重: {api.weight}, 备注: {api.note}")
except Exception as e: # noqa: PERF203
logger.exception(f"API {api.api} 初始化失败, 跳过: {e}")
if api.enabled:
try:
request_api = RequestAPI(URL(api.api), api.token, api.weight, api.note)
request_apis.append(request_api)
logger.info(f"API {api.api} 初始化成功, 权重: {api.weight}, 备注: {api.note}")
except Exception as e:
logger.exception(f"API {api.api} 初始化失败, 跳过: {e}")
else:
logger.warning(f"API {api.api} 未启用")

if config.api.local_api_config is not None and config.api.local_api_config.enable:
from .local import LOCAL_REQUEST_API_PATH, LOCAL_REQUEST_API_TOKEN
Expand All @@ -23,6 +27,9 @@
RequestAPI(URL(LOCAL_REQUEST_API_PATH), LOCAL_REQUEST_API_TOKEN, 0, "本地 API", skip_version_checking=True)
)

if not request_apis:
[logger.error("未找到可用 API!!!!!!!!!!!!!!!!!") for _ in range(10)]
sys.exit()

def get_request_api() -> RequestAPI:
if not request_apis:
Expand Down
2 changes: 1 addition & 1 deletion nonebot_plugin_bilichat/static/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ api:
- api: "https://api.example.com" # API 地址
token: "your_api_token" # API Token, 服务端未设置则留空
weight: 1 # 权重, 用于负载均衡, 越大越优先
enable: true # 是否启用该 API
enable: false # 是否启用该 API
# 可以根据需要添加更多 API 配置
local_api_config: # 本地 API 配置
enable: false # 是否启用本地 API
Expand Down

0 comments on commit b02d3b0

Please sign in to comment.