赞
踩
from enum import Enum from fastapi import APIRouter, Path app01 = APIRouter() """ Path Parameters and Number Validations路径参数和数字验证 """ @app01.get("/parameters") def path_params01(): return {"message": "This is a messaget"} @app01.get("/{parameters}") # 函数的顺序就是路由的顺序(api的顺序) def path_params01(parameters: str): return {"message": parameters} class CityName(str, Enum): Beijing = "Beijing china" Shanghai = "Shanghai China" # 路径参数---》枚举类型 @app01.get("/enum/{city}") # 枚举类型参数 async def latest(city: CityName): if city == CityName.Shanghai: return {"city_name": city, "confirmed": 1492, "death": 7} if city == CityName.Beijing: return {"city_name": city, "confirmed": 971, "death": 9} return {"city_name": city, "latest": "unknown"} # 文件路径 @app01.get("/files/{file_path:path}") #通过path parameters传递文件路径 ---->注明file_path:path 是为了区分前面的路由的路径 def filepath(file_path: str) : return f"The file path is {file_path}" @app01.get("/path_/{num}" ) def path_params_validate( num: int = Path(..., title="Your number", description="路径参数校验大于1小于10",ge=1,le=10)): # 路径参数校验 return num
查看交互文档docs
查看各个接口(节选)
第三个,枚举类型
通过path parameters传递文件路径 参数
参数验证(调用fastapi的Path进行路径参数校验)
""" 查询参数和字符串验证 """ @app01.get("/query") def page_limit(page: int = 1,limit: Optional[int] = None): #给了默认值就是选填的参数 if limit: return {"page":page, "limit": limit} return {"page": page} @app01.get("/query/bool/conversion") def type_conversion(param: bool = False): # bool类型转换: yes on 1 True true会转换成t return param @app01.get("/query/validations") def query_params_validate( value: str = Query(..., min_length=8,max_length=16,regex="^a"), values: List[str] = Query(default=["v1","v2"],alias="alias_name")):#多个查询参数的列表。参数别名 return value,values
查看接口
数据验证(/query/validations最后一个接口)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。