jdeip/app/routers/proxy.py

46 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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()