feat: 添加脚本相关接口文档,子配置数据获取接口独立
This commit is contained in:
@@ -38,7 +38,12 @@ async def add_task(task: TaskCreateIn = Body(...)) -> TaskCreateOut:
|
||||
try:
|
||||
task_id = await TaskManager.add_task(task.mode, task.taskId)
|
||||
except Exception as e:
|
||||
return TaskCreateOut(code=500, status="error", message=str(e), websocketId="")
|
||||
return TaskCreateOut(
|
||||
code=500,
|
||||
status="error",
|
||||
message=f"{type(e).__name__}: {str(e)}",
|
||||
websocketId="",
|
||||
)
|
||||
return TaskCreateOut(websocketId=str(task_id))
|
||||
|
||||
|
||||
@@ -48,19 +53,22 @@ async def stop_task(task: DispatchIn = Body(...)) -> OutBase:
|
||||
try:
|
||||
await TaskManager.stop_task(task.taskId)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
|
||||
@router.websocket("/ws/{taskId}")
|
||||
@router.websocket("/ws/{websocketId}")
|
||||
async def websocket_endpoint(
|
||||
websocket: WebSocket, taskId: str = Path(..., description="要连接的任务ID")
|
||||
websocket: WebSocket,
|
||||
websocketId: str = Path(..., description="要连接的WebSocket ID"),
|
||||
):
|
||||
await websocket.accept()
|
||||
try:
|
||||
uid = uuid.UUID(taskId)
|
||||
uid = uuid.UUID(websocketId)
|
||||
except ValueError:
|
||||
await websocket.close(code=1008, reason="无效的任务ID")
|
||||
await websocket.close(code=1008, reason="无效的WebSocket ID")
|
||||
return
|
||||
|
||||
if uid in TaskManager.connection_events and uid not in TaskManager.websocket_dict:
|
||||
|
||||
@@ -42,7 +42,9 @@ async def get_stage_combox(
|
||||
raw_data = await Config.get_stage_info(stage.type)
|
||||
data = [ComboBoxItem(**item) for item in raw_data] if raw_data else []
|
||||
except Exception as e:
|
||||
return ComboBoxOut(code=500, status="error", message=str(e), data=[])
|
||||
return ComboBoxOut(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}", data=[]
|
||||
)
|
||||
return ComboBoxOut(data=data)
|
||||
|
||||
|
||||
@@ -58,7 +60,9 @@ async def get_script_combox() -> ComboBoxOut:
|
||||
raw_data = await Config.get_script_combox()
|
||||
data = [ComboBoxItem(**item) for item in raw_data] if raw_data else []
|
||||
except Exception as e:
|
||||
return ComboBoxOut(code=500, status="error", message=str(e), data=[])
|
||||
return ComboBoxOut(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}", data=[]
|
||||
)
|
||||
return ComboBoxOut(data=data)
|
||||
|
||||
|
||||
@@ -74,7 +78,9 @@ async def get_task_combox() -> ComboBoxOut:
|
||||
raw_data = await Config.get_task_combox()
|
||||
data = [ComboBoxItem(**item) for item in raw_data] if raw_data else []
|
||||
except Exception as e:
|
||||
return ComboBoxOut(code=500, status="error", message=str(e), data=[])
|
||||
return ComboBoxOut(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}", data=[]
|
||||
)
|
||||
return ComboBoxOut(data=data)
|
||||
|
||||
|
||||
@@ -84,7 +90,9 @@ async def get_notice_info() -> InfoOut:
|
||||
try:
|
||||
data = await Config.get_server_info("notice")
|
||||
except Exception as e:
|
||||
return InfoOut(code=500, status="error", message=str(e), data={})
|
||||
return InfoOut(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}", data={}
|
||||
)
|
||||
return InfoOut(data=data)
|
||||
|
||||
|
||||
@@ -96,7 +104,9 @@ async def get_apps_info() -> InfoOut:
|
||||
try:
|
||||
data = await Config.get_server_info("apps_info")
|
||||
except Exception as e:
|
||||
return InfoOut(code=500, status="error", message=str(e), data={})
|
||||
return InfoOut(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}", data={}
|
||||
)
|
||||
return InfoOut(data=data)
|
||||
|
||||
|
||||
@@ -107,5 +117,10 @@ async def add_overview() -> InfoOut:
|
||||
try:
|
||||
data = await Config.get_stage_info("Info")
|
||||
except Exception as e:
|
||||
return InfoOut(code=500, status="error", message=str(e), data={"ALL": []})
|
||||
return InfoOut(
|
||||
code=500,
|
||||
status="error",
|
||||
message=f"{type(e).__name__}: {str(e)}",
|
||||
data={"ALL": []},
|
||||
)
|
||||
return InfoOut(data={"ALL": data})
|
||||
|
||||
@@ -45,7 +45,13 @@ async def get_plan(plan: PlanGetIn = Body(...)) -> PlanGetOut:
|
||||
try:
|
||||
index, data = await Config.get_plan(plan.planId)
|
||||
except Exception as e:
|
||||
return PlanGetOut(code=500, status="error", message=str(e), index=[], data={})
|
||||
return PlanGetOut(
|
||||
code=500,
|
||||
status="error",
|
||||
message=f"{type(e).__name__}: {str(e)}",
|
||||
index=[],
|
||||
data={},
|
||||
)
|
||||
return PlanGetOut(index=index, data=data)
|
||||
|
||||
|
||||
@@ -57,7 +63,9 @@ async def update_plan(plan: PlanUpdateIn = Body(...)) -> OutBase:
|
||||
try:
|
||||
await Config.update_plan(plan.planId, plan.data)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
|
||||
@@ -67,7 +75,9 @@ async def delete_plan(plan: PlanDeleteIn = Body(...)) -> OutBase:
|
||||
try:
|
||||
await Config.del_plan(plan.planId)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
|
||||
@@ -79,5 +89,7 @@ async def reorder_plan(plan: PlanReorderIn = Body(...)) -> OutBase:
|
||||
try:
|
||||
await Config.reorder_plan(plan.indexList)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
@@ -33,8 +33,17 @@ router = APIRouter(prefix="/api/queue", tags=["调度队列管理"])
|
||||
)
|
||||
async def add_queue() -> QueueCreateOut:
|
||||
|
||||
uid, config = await Config.add_queue()
|
||||
data = QueueConfig(**(await config.toDict()))
|
||||
try:
|
||||
uid, config = await Config.add_queue()
|
||||
data = QueueConfig(**(await config.toDict()))
|
||||
except Exception as e:
|
||||
return QueueCreateOut(
|
||||
code=500,
|
||||
status="error",
|
||||
message=f"{type(e).__name__}: {str(e)}",
|
||||
queueId="",
|
||||
data=QueueConfig(**{}),
|
||||
)
|
||||
return QueueCreateOut(queueId=str(uid), data=data)
|
||||
|
||||
|
||||
@@ -48,7 +57,13 @@ async def get_queues(queue: QueueGetIn = Body(...)) -> QueueGetOut:
|
||||
index = [QueueIndexItem(**_) for _ in index]
|
||||
data = {uid: QueueConfig(**cfg) for uid, cfg in config.items()}
|
||||
except Exception as e:
|
||||
return QueueGetOut(code=500, status="error", message=str(e), index=[], data={})
|
||||
return QueueGetOut(
|
||||
code=500,
|
||||
status="error",
|
||||
message=f"{type(e).__name__}: {str(e)}",
|
||||
index=[],
|
||||
data={},
|
||||
)
|
||||
return QueueGetOut(index=index, data=data)
|
||||
|
||||
|
||||
@@ -62,7 +77,9 @@ async def update_queue(queue: QueueUpdateIn = Body(...)) -> OutBase:
|
||||
queue.queueId, queue.data.model_dump(exclude_unset=True)
|
||||
)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
|
||||
@@ -72,7 +89,9 @@ async def delete_queue(queue: QueueDeleteIn = Body(...)) -> OutBase:
|
||||
try:
|
||||
await Config.del_queue(queue.queueId)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
|
||||
@@ -82,10 +101,32 @@ async def reorder_queue(script: QueueReorderIn = Body(...)) -> OutBase:
|
||||
try:
|
||||
await Config.reorder_queue(script.indexList)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
|
||||
@router.post(
|
||||
"/time/get", summary="查询定时项", response_model=TimeSetGetOut, status_code=200
|
||||
)
|
||||
async def get_time_set(time: TimeSetGetIn = Body(...)) -> TimeSetGetOut:
|
||||
|
||||
try:
|
||||
index, data = await Config.get_time_set(time.queueId, time.timeSetId)
|
||||
index = [TimeSetIndexItem(**_) for _ in index]
|
||||
data = {uid: TimeSet(**cfg) for uid, cfg in data.items()}
|
||||
except Exception as e:
|
||||
return TimeSetGetOut(
|
||||
code=500,
|
||||
status="error",
|
||||
message=f"{type(e).__name__}: {str(e)}",
|
||||
index=[],
|
||||
data={},
|
||||
)
|
||||
return TimeSetGetOut(index=index, data=data)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/time/add", summary="添加定时项", response_model=TimeSetCreateOut, status_code=200
|
||||
)
|
||||
@@ -106,7 +147,9 @@ async def update_time_set(time: TimeSetUpdateIn = Body(...)) -> OutBase:
|
||||
time.queueId, time.timeSetId, time.data.model_dump(exclude_unset=True)
|
||||
)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
|
||||
@@ -118,22 +161,46 @@ async def delete_time_set(time: TimeSetDeleteIn = Body(...)) -> OutBase:
|
||||
try:
|
||||
await Config.del_time_set(time.queueId, time.timeSetId)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
|
||||
@router.post(
|
||||
"/time/order", summary="重新排序时间设置", response_model=OutBase, status_code=200
|
||||
"/time/order", summary="重新排序定时项", response_model=OutBase, status_code=200
|
||||
)
|
||||
async def reorder_time_set(time: TimeSetReorderIn = Body(...)) -> OutBase:
|
||||
|
||||
try:
|
||||
await Config.reorder_time_set(time.queueId, time.indexList)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
|
||||
@router.post(
|
||||
"/item/get", summary="查询队列项", response_model=QueueItemGetOut, status_code=200
|
||||
)
|
||||
async def get_item(item: QueueItemGetIn = Body(...)) -> QueueItemGetOut:
|
||||
|
||||
try:
|
||||
index, data = await Config.get_queue_item(item.queueId, item.queueItemId)
|
||||
index = [QueueItemIndexItem(**_) for _ in index]
|
||||
data = {uid: QueueItem(**cfg) for uid, cfg in data.items()}
|
||||
except Exception as e:
|
||||
return QueueItemGetOut(
|
||||
code=500,
|
||||
status="error",
|
||||
message=f"{type(e).__name__}: {str(e)}",
|
||||
index=[],
|
||||
data={},
|
||||
)
|
||||
return QueueItemGetOut(index=index, data=data)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/item/add",
|
||||
summary="添加队列项",
|
||||
@@ -157,7 +224,9 @@ async def update_item(item: QueueItemUpdateIn = Body(...)) -> OutBase:
|
||||
item.queueId, item.queueItemId, item.data.model_dump(exclude_unset=True)
|
||||
)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
|
||||
@@ -169,7 +238,9 @@ async def delete_item(item: QueueItemDeleteIn = Body(...)) -> OutBase:
|
||||
try:
|
||||
await Config.del_queue_item(item.queueId, item.queueItemId)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
|
||||
@@ -181,5 +252,7 @@ async def reorder_item(item: QueueItemReorderIn = Body(...)) -> OutBase:
|
||||
try:
|
||||
await Config.reorder_queue_item(item.queueId, item.indexList)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
# Contact: DLmaster_361@163.com
|
||||
|
||||
|
||||
import uuid
|
||||
from fastapi import APIRouter, Body
|
||||
|
||||
from app.core import Config
|
||||
@@ -28,13 +29,27 @@ from app.models.schema import *
|
||||
router = APIRouter(prefix="/api/scripts", tags=["脚本管理"])
|
||||
|
||||
|
||||
SCRIPT_BOOK = {"MaaConfig": MaaConfig, "GeneralConfig": GeneralConfig}
|
||||
USER_BOOK = {"MaaConfig": MaaUserConfig, "GeneralConfig": GeneralUserConfig}
|
||||
|
||||
|
||||
@router.post(
|
||||
"/add", summary="添加脚本", response_model=ScriptCreateOut, status_code=200
|
||||
)
|
||||
async def add_script(script: ScriptCreateIn = Body(...)) -> ScriptCreateOut:
|
||||
|
||||
uid, config = await Config.add_script(script.type)
|
||||
return ScriptCreateOut(scriptId=str(uid), data=await config.toDict())
|
||||
try:
|
||||
uid, config = await Config.add_script(script.type)
|
||||
data = SCRIPT_BOOK[type(config).__name__](**(await config.toDict()))
|
||||
except Exception as e:
|
||||
return ScriptCreateOut(
|
||||
code=500,
|
||||
status="error",
|
||||
message=f"{type(e).__name__}: {str(e)}",
|
||||
scriptId="",
|
||||
data=GeneralConfig(**{}),
|
||||
)
|
||||
return ScriptCreateOut(scriptId=str(uid), data=data)
|
||||
|
||||
|
||||
@router.post(
|
||||
@@ -44,8 +59,21 @@ async def get_scripts(script: ScriptGetIn = Body(...)) -> ScriptGetOut:
|
||||
|
||||
try:
|
||||
index, data = await Config.get_script(script.scriptId)
|
||||
index = [ScriptIndexItem(**_) for _ in index]
|
||||
data = {
|
||||
uid: SCRIPT_BOOK[next((_.type for _ in index if _.uid == uid), "General")](
|
||||
**cfg
|
||||
)
|
||||
for uid, cfg in data.items()
|
||||
}
|
||||
except Exception as e:
|
||||
return ScriptGetOut(code=500, status="error", message=str(e), index=[], data={})
|
||||
return ScriptGetOut(
|
||||
code=500,
|
||||
status="error",
|
||||
message=f"{type(e).__name__}: {str(e)}",
|
||||
index=[],
|
||||
data={},
|
||||
)
|
||||
return ScriptGetOut(index=index, data=data)
|
||||
|
||||
|
||||
@@ -55,9 +83,13 @@ async def get_scripts(script: ScriptGetIn = Body(...)) -> ScriptGetOut:
|
||||
async def update_script(script: ScriptUpdateIn = Body(...)) -> OutBase:
|
||||
|
||||
try:
|
||||
await Config.update_script(script.scriptId, script.data)
|
||||
await Config.update_script(
|
||||
script.scriptId, script.data.model_dump(exclude_unset=True)
|
||||
)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
|
||||
@@ -67,7 +99,9 @@ async def delete_script(script: ScriptDeleteIn = Body(...)) -> OutBase:
|
||||
try:
|
||||
await Config.del_script(script.scriptId)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
|
||||
@@ -77,17 +111,56 @@ async def reorder_script(script: ScriptReorderIn = Body(...)) -> OutBase:
|
||||
try:
|
||||
await Config.reorder_script(script.indexList)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
|
||||
@router.post(
|
||||
"/user/get", summary="查询用户", response_model=UserGetOut, status_code=200
|
||||
)
|
||||
async def get_user(user: UserGetIn = Body(...)) -> UserGetOut:
|
||||
|
||||
try:
|
||||
index, data = await Config.get_user(user.scriptId, user.userId)
|
||||
index = [UserIndexItem(**_) for _ in index]
|
||||
data = {
|
||||
uid: USER_BOOK[
|
||||
type(Config.ScriptConfig[uuid.UUID(user.scriptId)]).__name__
|
||||
](**cfg)
|
||||
for uid, cfg in data.items()
|
||||
}
|
||||
except Exception as e:
|
||||
return UserGetOut(
|
||||
code=500,
|
||||
status="error",
|
||||
message=f"{type(e).__name__}: {str(e)}",
|
||||
index=[],
|
||||
data={},
|
||||
)
|
||||
return UserGetOut(index=index, data=data)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/user/add", summary="添加用户", response_model=UserCreateOut, status_code=200
|
||||
)
|
||||
async def add_user(user: UserInBase = Body(...)) -> UserCreateOut:
|
||||
|
||||
uid, config = await Config.add_user(user.scriptId)
|
||||
return UserCreateOut(userId=str(uid), data=await config.toDict())
|
||||
try:
|
||||
uid, config = await Config.add_user(user.scriptId)
|
||||
data = USER_BOOK[type(Config.ScriptConfig[uuid.UUID(user.scriptId)]).__name__](
|
||||
**(await config.toDict())
|
||||
)
|
||||
except Exception as e:
|
||||
return UserCreateOut(
|
||||
code=500,
|
||||
status="error",
|
||||
message=f"{type(e).__name__}: {str(e)}",
|
||||
userId="",
|
||||
data=GeneralUserConfig(**{}),
|
||||
)
|
||||
return UserCreateOut(userId=str(uid), data=data)
|
||||
|
||||
|
||||
@router.post(
|
||||
@@ -96,9 +169,13 @@ async def add_user(user: UserInBase = Body(...)) -> UserCreateOut:
|
||||
async def update_user(user: UserUpdateIn = Body(...)) -> OutBase:
|
||||
|
||||
try:
|
||||
await Config.update_user(user.scriptId, user.userId, user.data)
|
||||
await Config.update_user(
|
||||
user.scriptId, user.userId, user.data.model_dump(exclude_unset=True)
|
||||
)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
|
||||
@@ -110,7 +187,9 @@ async def delete_user(user: UserDeleteIn = Body(...)) -> OutBase:
|
||||
try:
|
||||
await Config.del_user(user.scriptId, user.userId)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
|
||||
@@ -122,5 +201,7 @@ async def reorder_user(user: UserReorderIn = Body(...)) -> OutBase:
|
||||
try:
|
||||
await Config.reorder_user(user.scriptId, user.indexList)
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
@@ -36,7 +36,10 @@ async def get_scripts() -> SettingGetOut:
|
||||
data = await Config.get_setting()
|
||||
except Exception as e:
|
||||
return SettingGetOut(
|
||||
code=500, status="error", message=str(e), data=GlobalConfig(**{})
|
||||
code=500,
|
||||
status="error",
|
||||
message=f"{type(e).__name__}: {str(e)}",
|
||||
data=GlobalConfig(**{}),
|
||||
)
|
||||
return SettingGetOut(data=GlobalConfig(**data))
|
||||
|
||||
@@ -48,5 +51,7 @@ async def update_script(script: SettingUpdateIn = Body(...)) -> OutBase:
|
||||
try:
|
||||
await Config.update_setting(script.data.model_dump(exclude_unset=True))
|
||||
except Exception as e:
|
||||
return OutBase(code=500, status="error", message=str(e))
|
||||
return OutBase(
|
||||
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
|
||||
)
|
||||
return OutBase()
|
||||
|
||||
Reference in New Issue
Block a user