from typing import Optional from fastapi import APIRouter, Body, Query, Form from pydantic import BaseModel from ..rotation_service import rotate as rotate_impl, status as status_impl router = APIRouter() class RotateRequest(BaseModel): cityhash: Optional[str] = None num: Optional[int] = None @router.post("/rotate") def rotate( # 查询参数 cityhash_q: Optional[str] = Query(None, alias="cityhash"), num_q: Optional[int] = Query(None, alias="num"), # 表单参数(multipart/form-data 或 application/x-www-form-urlencoded) cityhash_f: Optional[str] = Form(None), num_f: Optional[int] = Form(None), # JSON 请求体(application/json) req: Optional[RotateRequest] = Body(None), ): # 优先级:Query > Form > JSON effective_cityhash = ( cityhash_q if cityhash_q is not None else (cityhash_f if cityhash_f is not None else (req.cityhash if req else None)) ) effective_num = ( num_q if num_q is not None else (num_f if num_f is not None else (req.num if req else None)) ) result = rotate_impl(cityhash=effective_cityhash, num=effective_num) return result @router.get("/status") def get_status(): return status_impl()