您的位置:首页 > 其它

FastAPI学习-3(路由参数详解)

2020-06-28 05:24 531 查看

1.路径参数
声明路径参数时,可以使用Python格式字符串使用的相同语法声明路径“参数”或“变量”

  • 无参数类型
from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}") async def read_item(item_id):
return {"item_id": item_id}

代码中没有规定参数item_id的数据类型

  • 有数据类型
from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}")
async def read_item(item_id: int):
return {"item_id": item_id}

代码中规定了item_id的参数类型为int,如果传入的参数类型不符合规定的类型,则会返回错误,如下:

{
"detail": [
{
"loc": [
"path",
"item_id"
],
"msg": "value is not a valid integer",
"type": "type_error.integer"
}
]
}

该错误会清楚的指明验证未通过的点

注意:
创建路由的时候,你需要对路由的操作顺序进行调整,例如,您用/ users / me这个路由去获取当前的所有用户信息,然后通过/ users / {user_id}这个路由来获取某个用户id的信息,但是在FastAPI中的路由操作是按顺序来查找的,如果/ users / {user_id} 这个路由写在了/ users / me的前面,当您请求/ users / me时候,默认的会去匹配/ users / {user_id}的执行方法,此时的user_id的值是me

from fastapi import FastAPI

app = FastAPI()

@app.get("/users/me") async def read_user_me():
return {"user_id": "the current user"}

@app.get("/users/{user_id}") async def read_user(user_id: str):
return {"user_id": user_id}
  • 枚举类型
    如果路径参数希望传递的参数在某些数据里面去选择,则可以使用Enum库,通过继承 str , API 文档将可能知道接收的值必须是 string 类型才会被正确渲染。并创建具有固定值的类属性,这些固定值将是可用的有效值:
from enum import Enum
from fastapi import FastAPI

class ModelName(str, Enum):
alexnet = "alexnet" resnet = "resnet" lenet = "lenet"

app = FastAPI()

@app.get("/model/{model_name}")
async def get_model(model_name: ModelName):  # 规定了model_name 的数据类型在ModelName中选择
if model_name == ModelName.alexnet:  # model_name 指向ModelName的枚举成员
return {"model_name": model_name, "message": "Deep Learning FTW!"}
if model_name.value == "lenet":  # model_name.value是获取枚举成员的值
return {"model_name": model_name, "message": "LeCNN all the images"}
return {"model_name": model_name, "message": "Have some residuals"}  # model_name,枚举类型的成员可以作为返回值,返回时,自动对应该枚举类型成员的值
  • 文件路径类型
    假设现在你有一个路径操作:/files/{file_path},但是你需要 file_path 本身包含一个 路径, 比如 home/johndoe/myfile.txt,因此, 文件路径可能是: /files/home/johndoe/myfile.txt,但是OpenAPI 不支持包含文件路径的路径参数,这个路由就会难以被匹配,不过可以使用Starlette的内置工具:
from fastapi import FastAPI

app = FastAPI()

@app.get("/files/{file_path:path}")  # path 定义了参数file_path是个路径,可以将参数以‘/’开头,url就是/files//home/johndoe/myfile.txt
async def read_user_me(file_path: str):
return {"file_path": file_path}

2.查询参数
当你声明不属于路径参数的其他函数参数时,他们就自动解释为查询参数,也就是Query参数

from fastapi import FastAPI

app = FastAPI()

@app.get("/items/")
async def read_item(skip: int = 0, limit: int = 10):
return {"skip": skip+limit}

如在get请求中,在url中?后面的所有用&分割的键值对参数都是查询参数,例:

http://127.0.0.1:8888/user?username=zhangsan&pwd=zweasd

此时的查询参数就是username和pwd,对应的值是zhangsan, zweasd,所有适应于路径参数的所有过程都适用于查询参数

设置查询参数的默认值:
比如在注册的时候,有些参数是必填的,有些参数可以不填,但是要给定一个默认值,这时候,这些不是必填的参数可以不用出现在请求路由的参数列表里面,可以如下设置默认参数:

from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}")
async def read_item(item_id: str, name: str = 'zhangsan', age:int = 18):
return {"item_id": item_id, 'name': name, 'age': age}

"""
item_id: 路径参数,类型为str,必选
name: 查询参数,类型为str,可选,默认值为zhangsan
age: 查询参数,类型为int,可以,默认值为18
"""

查询参数数据类型转换:

from fastapi import FastAPI

app = FastAPI()

@app.get("/items")
async def read_item(name: str = None, is_delete: bool = False):
item = {}
if name:
item.update({"name": q})
if not is_delete:
item.update(
{"description": "{} 存在".format(name)}
)
return item
"""
此时请求 http://127.0.0.1:8000/items?name=zhangsan&short=0
http://127.0.0.1:8000/items?name=zhangsan&short=true
http://127.0.0.1:8000/items?name=zhangsan&short=adad
请求参数short都会进行数据转换,转换为True或者False
"""

可选的类型声明:
如果声明查询参数的时候,spik: int = None, 因为None不是int类型,这样会报如下错误:

Incompatible types in assignment (expression has type "None", variable has type "int")

可以用Optional 告诉 mypy,这个值可能是 None:

from typing import Optional

from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}") # 路径参数不可以用Optional
async def read_user_item(item_id: str, limit: Optional[int] = None):
item = {"item_id": item_id, "limit": limit}
return item

3.boby参数
不能通过get请求发送boby参数,常见发送boby参数的请求方法有,post, put, delete, patch
请求体参数也可以和查询传参一样,定义在声明的函数中,当请求体(boby)参数过多时,一般会引用pydantic中BaseModel去定义boby参数,代码如下:

from fastapi import FastAPI
from pydantic import BaseModel

class Item(BaseModel):
name: str
description: str = None
price: float
tax: float = None

app = FastAPI()

@app.post("/items/")
async def create_item(item: Item):
return item
"""
可以用None默认赋值,去定义参数是否是必填
"""
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: