S126 AstrBot原生WebAPI路由兼容
这个提交存在于:
父节点
e6e23749d3
当前提交
6a88ced1d5
共有 2 个文件被更改,包括 26 次插入 和 7 次删除
27
main.py
27
main.py
|
|
@ -178,17 +178,15 @@ def _bridge_registration_routes() -> list[dict[str, str]]:
|
|||
routes: list[dict[str, str]] = []
|
||||
seen: set[tuple[str, str]] = set()
|
||||
catch_all_paths = (
|
||||
"/{endpoint:path}",
|
||||
"/{path:path}",
|
||||
"{endpoint:path}",
|
||||
"{path:path}",
|
||||
f"/{PLUGIN_NAME}/<path:endpoint>",
|
||||
f"{PLUGIN_NAME}/<path:endpoint>",
|
||||
)
|
||||
for method in sorted({route.method for route in ROUTES}):
|
||||
for path in catch_all_paths:
|
||||
seen.add((method, path))
|
||||
routes.append({"method": method, "path": path, "endpoint": ""})
|
||||
for route in ROUTES:
|
||||
for path in (f"/{route.endpoint}", route.endpoint):
|
||||
for path in (f"/{PLUGIN_NAME}/{route.endpoint}", f"{PLUGIN_NAME}/{route.endpoint}"):
|
||||
key = (route.method, path)
|
||||
if key in seen:
|
||||
continue
|
||||
|
|
@ -199,11 +197,17 @@ def _bridge_registration_routes() -> list[dict[str, str]]:
|
|||
|
||||
def _call_web_api_registrar(registrar: Callable[..., Any], path: str, handler: Callable[..., Any], method: str) -> bool:
|
||||
methods = [method]
|
||||
desc = f"ImageStudio {method} {path}"
|
||||
attempts = (
|
||||
lambda: registrar(route=path, view_handler=handler, methods=methods, desc=desc),
|
||||
lambda: registrar(route=path, view_handler=handler, methods=methods),
|
||||
lambda: registrar(path=path, handler=handler, methods=methods),
|
||||
lambda: registrar(path=path, handler=handler, methods=methods, desc=desc),
|
||||
lambda: registrar(path=path, handler=handler, method=method),
|
||||
lambda: registrar(endpoint=path, handler=handler, methods=methods),
|
||||
lambda: registrar(path, handler, methods, desc),
|
||||
lambda: registrar(path, handler, methods=methods),
|
||||
lambda: registrar(path, handler, methods=methods, desc=desc),
|
||||
lambda: registrar(path, handler, method=method),
|
||||
lambda: registrar(path, methods, handler),
|
||||
lambda: registrar(path, method, handler),
|
||||
|
|
@ -225,6 +229,7 @@ def _make_bridge_handler(endpoint: str, method: str) -> Callable[..., Any]:
|
|||
async def handler(request: Any | None = None, **kwargs: Any) -> dict[str, Any]:
|
||||
positional_endpoint = request if isinstance(request, str) else None
|
||||
actual_request = None if positional_endpoint is not None else request or kwargs.get("request")
|
||||
actual_request = actual_request or _current_astrbot_web_request()
|
||||
actual_endpoint = endpoint or _first_text(
|
||||
positional_endpoint,
|
||||
kwargs.get("endpoint"),
|
||||
|
|
@ -245,6 +250,18 @@ def _make_bridge_handler(endpoint: str, method: str) -> Callable[..., Any]:
|
|||
return handler
|
||||
|
||||
|
||||
def _current_astrbot_web_request() -> Any | None:
|
||||
try:
|
||||
from astrbot.api.web import request as astrbot_web_request
|
||||
except Exception:
|
||||
return None
|
||||
try:
|
||||
getattr(astrbot_web_request, "method")
|
||||
except Exception:
|
||||
return None
|
||||
return astrbot_web_request
|
||||
|
||||
|
||||
def _request_context_from_astrbot_request(request: Any | None) -> RequestContext:
|
||||
username = _first_text(
|
||||
_nested_attr(request, "user", "username"),
|
||||
|
|
|
|||
|
|
@ -76,10 +76,12 @@ class PluginEntrypointTests(unittest.TestCase):
|
|||
|
||||
self.assertTrue(result["registered"])
|
||||
self.assertGreater(len(runtime.web_apis), len(ROUTES))
|
||||
self.assertTrue(any(api["path"] == "/ui/bootstrap" for api in runtime.web_apis))
|
||||
self.assertTrue(any(api["path"] == f"/{PLUGIN_NAME}/ui/bootstrap" for api in runtime.web_apis))
|
||||
|
||||
bootstrap_api = next(
|
||||
api for api in runtime.web_apis if api["path"] == "/ui/bootstrap" and api["methods"] == ["GET"]
|
||||
api
|
||||
for api in runtime.web_apis
|
||||
if api["path"] == f"/{PLUGIN_NAME}/ui/bootstrap" and api["methods"] == ["GET"]
|
||||
)
|
||||
response = asyncio.run(bootstrap_api["handler"]())
|
||||
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用