Files

28 lines
920 B
Python

import json
from fastapi import APIRouter, HTTPException
from fastapi.responses import StreamingResponse
from server.schemas import SessionBody, device_payload
from server.services.catalog import catalog
router = APIRouter(prefix="/api")
@router.post("/catalog")
async def load_catalog(body: SessionBody):
token = body.token.strip()
if not token:
raise HTTPException(status_code=400, detail="请先登录")
raw_device = device_payload(body.device)
async def generate():
try:
async for event in catalog.stream(token, raw_device, body.refresh):
yield json.dumps(event, ensure_ascii=False) + "\n"
except Exception as exc:
message = str(exc) or "获取清单失败"
yield json.dumps({"type": "error", "message": message}, ensure_ascii=False) + "\n"
return StreamingResponse(generate(), media_type="application/x-ndjson")