from fastapi import APIRouter
routes = APIRouter()
samples = [f"R{i:02}" for i in range(32)]
@routes.get("/samples")
def samples_page(skip: int = 0, limit: int = 10):
return {"skip": skip, "limit": limit, "items": samples[skip:skip+limit]}
@routes.get("/lookup/{record}")
def lookup(record: str, q: str | None = None):
return {"record": record, "q": q}
@routes.get("/query-required")
def required_nullable(q: str | None):
return {"q": q}
@routes.get("/read/{record}")
def read(record: str, short: bool = False):
return {"record": record, "short": short} | ({} if short else {"description": "观测记录说明"})
@routes.get("/selection/{record}")
def select(record: str, source: str, skip: int = 0, limit: int | None = None):
return {"record": record, "source": source, "skip": skip, "limit": limit}
@routes.get("/stations/{station_id}/logs/{record}")
def log(record: str, station_id: int, q: str | None = None, short: bool = False):
return {"record": record, "station_id": station_id, "q": q, "short": short} | (
{} if short else {"description": "站点日志说明"}
)
本例需在 CPython 3.12、FastAPI 0.162.2、Uvicorn 0.54.0、pytest 9.2.0 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:CPython 3.12、FastAPI 0.162.2、Uvicorn 0.54.0、pytest 9.2.0
入口文件:projects/fastapi-url-parameters-lab/query_lab.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
query_lab.py,或下载右侧课程工程包。 - 确认本地已准备 CPython 3.12、FastAPI 0.162.2、Uvicorn 0.54.0、pytest 9.2.0。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联2章) · 含当前章节或实例 · mbskycom202611a511.zip
课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a182.zip