日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)FastAPI教程:FastAPI教程路徑操作配置

您可以將幾個(gè)參數(shù)傳遞給路徑操作裝飾器來配置它。

警告

請(qǐng)注意,這些參數(shù)直接傳遞給路徑操作裝飾器,而不是您的路徑操作函數(shù)。

響應(yīng)狀態(tài)碼

您可以定義status_code要在您的路徑操作的響應(yīng)中使用的 (HTTP) 。

您可以直接傳遞int代碼,例如404.

但是,如果您不記得每個(gè)數(shù)字代碼的用途,您可以使用 中的快捷常量status:

from typing import Optional, Set

from fastapi import FastAPI, status
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
    tags: Set[str] = []


@app.post("/items/", response_model=Item, status_code=status.HTTP_201_CREATED)
async def create_item(item: Item):
    return item

該狀態(tài)代碼將在響應(yīng)中使用,并將添加到 OpenAPI 架構(gòu)中。

技術(shù)細(xì)節(jié)

您也可以使用from starlette import status.

FastAPI提供相同starlette.status的fastapi.status,就像為你的方便,開發(fā)人員。但它直接來自Starlette。

標(biāo)簽

您可以向路徑操作添加標(biāo)簽,tags使用listof str(通常只有一個(gè)str)傳遞參數(shù):

from typing import Optional, Set

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
    tags: Set[str] = []


@app.post("/items/", response_model=Item, tags=["items"])
async def create_item(item: Item):
    return item


@app.get("/items/", tags=["items"])
async def read_items():
    return [{"name": "Foo", "price": 42}]


@app.get("/users/", tags=["users"])
async def read_users():
    return [{"username": "johndoe"}]

它們將被添加到 OpenAPI 模式并由自動(dòng)文檔接口使用:

總結(jié)和描述

您可以添加一個(gè)summary和description:

from typing import Optional, Set

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
    tags: Set[str] = []


@app.post(
    "/items/",
    response_model=Item,
    summary="Create an item",
    description="Create an item with all the information, name, description, price, tax and a set of unique tags",
)
async def create_item(item: Item):
    return item

來自文檔字符串的描述

由于描述往往很長(zhǎng)并且涵蓋多行,您可以在函數(shù)docstring 中聲明路徑操作描述,F(xiàn)astAPI將從那里讀取它。

您可以在 docstring 中編寫Markdown,它將被正確解釋和顯示(考慮到 docstring 縮進(jìn))。

from typing import Optional, Set

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
    tags: Set[str] = []


@app.post("/items/", response_model=Item, summary="Create an item")
async def create_item(item: Item):
    """
    Create an item with all the information:

    - **name**: each item must have a name
    - **description**: a long description
    - **price**: required
    - **tax**: if the item doesn't have tax, you can omit this
    - **tags**: a set of unique tag strings for this item
    """
    return item

它將在交互式文檔中使用:

響應(yīng)說明

您可以使用參數(shù)指定響應(yīng)描述response_description:

from typing import Optional, Set

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
    tags: Set[str] = []


@app.post(
    "/items/",
    response_model=Item,
    summary="Create an item",
    response_description="The created item",
)
async def create_item(item: Item):
    """
    Create an item with all the information:

    - **name**: each item must have a name
    - **description**: a long description
    - **price**: required
    - **tax**: if the item doesn't have tax, you can omit this
    - **tags**: a set of unique tag strings for this item
    """
    return item

信息

請(qǐng)注意,response_description特指響應(yīng),description泛指路徑操作。

查看

OpenAPI 指定每個(gè)路徑操作都需要響應(yīng)描述。

因此,如果您不提供,F(xiàn)astAPI將自動(dòng)生成“成功響應(yīng)”之一。

棄用路徑操作

如果您需要將路徑操作標(biāo)記為deprecated,但不刪除它,請(qǐng)傳遞參數(shù)deprecated:

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/", tags=["items"])
async def read_items():
    return [{"name": "Foo", "price": 42}]


@app.get("/users/", tags=["users"])
async def read_users():
    return [{"username": "johndoe"}]


@app.get("/elements/", tags=["items"], deprecated=True)
async def read_elements():
    return [{"item_id": "Foo"}]

它將在交互式文檔中明確標(biāo)記為已棄用:

檢查已棄用和未棄用的路徑操作的樣子:

回顧

通過將參數(shù)傳遞給路徑操作裝飾器,您可以輕松地為路徑操作配置和添加元數(shù)據(jù)。


網(wǎng)頁(yè)名稱:創(chuàng)新互聯(lián)FastAPI教程:FastAPI教程路徑操作配置
分享地址:http://www.5511xx.com/article/ccieijp.html