fix:路由优化
This commit is contained in:
@@ -894,6 +894,57 @@ async def list_scripts(
|
||||
}
|
||||
|
||||
|
||||
@router.get("/api/v1/scripts/{script_id}/content")
|
||||
async def get_script_content(
|
||||
script_id: str,
|
||||
request: Request,
|
||||
context: RequestContext = Depends(request_context),
|
||||
session: AsyncSession = Depends(database_session),
|
||||
) -> dict[str, Any]:
|
||||
"""获取脚本内容(用于只读预览)。
|
||||
|
||||
适用于被锁定的脚本,非所有者只能查看内容,不能编辑。
|
||||
该接口不检查锁状态,调用方需自行判断权限。
|
||||
"""
|
||||
script = await session.scalar(
|
||||
select(Scripts)
|
||||
.where(
|
||||
Scripts.script_id == script_id,
|
||||
Scripts.workspace_id == context.workspace.workspace_id,
|
||||
Scripts.status == "active",
|
||||
)
|
||||
)
|
||||
if script is None:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, "script not found")
|
||||
|
||||
# 获取 Jupyter 路径
|
||||
jupyter_name = _jupyter_path(script.script_type, script.script_id)
|
||||
|
||||
# 从 Jupyter 读取内容
|
||||
runtime_client = request.app.state.runtime_client
|
||||
try:
|
||||
content_data = await runtime_client.get_file(
|
||||
context.workspace.workspace_id,
|
||||
name=jupyter_name,
|
||||
)
|
||||
except RuntimeClientError as exc:
|
||||
raise HTTPException(
|
||||
status_code=exc.status_code,
|
||||
detail=exc.detail,
|
||||
) from exc
|
||||
|
||||
return {
|
||||
"request_id": context.request_id,
|
||||
"data": {
|
||||
"script_id": script.script_id,
|
||||
"script_type": script.script_type,
|
||||
"content": content_data.get("content"),
|
||||
"format": content_data.get("format"),
|
||||
},
|
||||
"meta": {},
|
||||
}
|
||||
|
||||
|
||||
@router.get("/api/v1/scripts/{script_id}")
|
||||
async def get_script(
|
||||
script_id: str,
|
||||
@@ -1360,54 +1411,3 @@ async def version_download_url(
|
||||
request,
|
||||
)
|
||||
return {"request_id": context.request_id, "data": data["data"], "meta": {}}
|
||||
|
||||
|
||||
@router.get("/api/v1/scripts/{script_id}/content")
|
||||
async def get_script_content(
|
||||
script_id: str,
|
||||
request: Request,
|
||||
context: RequestContext = Depends(request_context),
|
||||
session: AsyncSession = Depends(database_session),
|
||||
) -> dict[str, Any]:
|
||||
"""获取脚本内容(用于只读预览)。
|
||||
|
||||
适用于被锁定的脚本,非所有者只能查看内容,不能编辑。
|
||||
该接口不检查锁状态,调用方需自行判断权限。
|
||||
"""
|
||||
script = await session.scalar(
|
||||
select(Scripts)
|
||||
.where(
|
||||
Scripts.script_id == script_id,
|
||||
Scripts.workspace_id == context.workspace.workspace_id,
|
||||
Scripts.status == "active",
|
||||
)
|
||||
)
|
||||
if script is None:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, "script not found")
|
||||
|
||||
# 获取 Jupyter 路径
|
||||
jupyter_name = _jupyter_path(script.script_type, script.script_id)
|
||||
|
||||
# 从 Jupyter 读取内容
|
||||
runtime_client = request.app.state.runtime_client
|
||||
try:
|
||||
content_data = await runtime_client.get_file(
|
||||
context.workspace.workspace_id,
|
||||
name=jupyter_name,
|
||||
)
|
||||
except RuntimeClientError as exc:
|
||||
raise HTTPException(
|
||||
status_code=exc.status_code,
|
||||
detail=exc.detail,
|
||||
) from exc
|
||||
|
||||
return {
|
||||
"request_id": context.request_id,
|
||||
"data": {
|
||||
"script_id": script.script_id,
|
||||
"script_type": script.script_type,
|
||||
"content": content_data.get("content"),
|
||||
"format": content_data.get("format"),
|
||||
},
|
||||
"meta": {},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user