๐ ๐จ ๐¶
Warning
๐ ๐ ๐ง โ.
๐ฅ ๐ โถ๏ธ โฎ๏ธ FastAPI, ๐ ๐ช ๐ซ ๐ช ๐.
๐ ๐ช ๐ฃ ๐ ๐จ, โฎ๏ธ ๐ ๐ ๐, ๐ ๐, ๐, โ๏ธ.
๐ ๐ ๐จ ๐ ๐ ๐ ๐, ๐ซ ๐ ๐ ๐ ๏ธ ๐ฉบ.
โ๏ธ ๐ ๐ ๐จ ๐ โ๏ธ โ ๐ญ ๐ ๐จ Response
๐ JSONResponse
๐, โฎ๏ธ ๐ ๐ ๐ & ๐.
๐ ๐จ โฎ๏ธ model
¶
๐ ๐ช ๐ถโโ๏ธ ๐ โก ๐ ๏ธ ๐จโ๐จ ๐ข responses
.
โซ๏ธ ๐จ dict
, ๐ ๐ ๐ ๐ ๐จ, ๐ 200
, & ๐ฒ ๐ dict
โ โฎ๏ธ โน ๐ ๐ซ.
๐ ๐ ๐จ dict
โ ๐ช โ๏ธ ๐ model
, โ Pydantic ๐ท, ๐ response_model
.
FastAPI ๐ โ ๐ ๐ท, ๐ ๐ฎ ๐ป ๐ & ๐ โซ๏ธ โ ๐ฅ ๐.
๐ผ, ๐ฃ โ1๏ธโฃ ๐จ โฎ๏ธ ๐ ๐ 404
& Pydantic ๐ท Message
, ๐ ๐ช โ:
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from pydantic import BaseModel
class Item(BaseModel):
id: str
value: str
class Message(BaseModel):
message: str
app = FastAPI()
@app.get("/items/{item_id}", response_model=Item, responses={404: {"model": Message}})
async def read_item(item_id: str):
if item_id == "foo":
return {"id": "foo", "value": "there goes my hero"}
return JSONResponse(status_code=404, content={"message": "Item not found"})
Note
โ๏ธ ๐คฏ ๐ ๐ โ๏ธ ๐จ JSONResponse
๐.
Info
model
๐ ๐ซ ๐ ๐.
FastAPI ๐ โ Pydantic ๐ท โช๏ธโก๏ธ ๐ค, ๐ JSON Schema
, & ๐ฎ โซ๏ธ โ ๐ฅ.
โ ๐ฅ:
- ๐
content
, ๐ โ๏ธ ๐ฒ โ1๏ธโฃ ๐ป ๐ (dict
) ๐ ๐:- ๐ โฎ๏ธ ๐ป ๐, โ
application/json
, ๐ ๐ ๐ฒ โ1๏ธโฃ ๐ป ๐, ๐ ๐:- ๐
schema
, ๐ โ๏ธ ๐ฒ ๐ป ๐ โช๏ธโก๏ธ ๐ท, ๐ฅ โ ๐ฅ.- FastAPI ๐ฎ ๐ ๐ฅ ๐ ๐ป ๐ โ1๏ธโฃ ๐ฅ ๐ ๐ โฉ๏ธ โ โซ๏ธ ๐. ๐ ๐, ๐ ๐ธ & ๐ฉโ๐ป ๐ช โ๏ธ ๐ ๐ป ๐ ๐, ๐ ๐ป ๐ โก ๐งฐ, โ๏ธ.
- ๐
- ๐ โฎ๏ธ ๐ป ๐, โ
๐ ๐จ ๐ ๐ โก ๐ ๏ธ ๐:
{
"responses": {
"404": {
"description": "Additional Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Message"
}
}
}
},
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Item"
}
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
๐ ๐ โ1๏ธโฃ ๐ฅ ๐ ๐ ๐:
{
"components": {
"schemas": {
"Message": {
"title": "Message",
"required": [
"message"
],
"type": "object",
"properties": {
"message": {
"title": "Message",
"type": "string"
}
}
},
"Item": {
"title": "Item",
"required": [
"id",
"value"
],
"type": "object",
"properties": {
"id": {
"title": "Id",
"type": "string"
},
"value": {
"title": "Value",
"type": "string"
}
}
},
"ValidationError": {
"title": "ValidationError",
"required": [
"loc",
"msg",
"type"
],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {
"type": "string"
}
},
"msg": {
"title": "Message",
"type": "string"
},
"type": {
"title": "Error Type",
"type": "string"
}
}
},
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {
"$ref": "#/components/schemas/ValidationError"
}
}
}
}
}
}
}
๐ ๐ ๐ ๐ ๐จ¶
๐ ๐ช โ๏ธ ๐ ๐ responses
๐ข ๐ฎ ๐ ๐ ๐ ๐ ๐ ๐จ.
๐ผ, ๐ ๐ช ๐ฎ ๐ ๐ป ๐ image/png
, ๐ฃ ๐ ๐ โก ๐ ๏ธ ๐ช ๐จ ๐ป ๐ (โฎ๏ธ ๐ป ๐ application/json
) โ๏ธ ๐ฉ๐ด ๐ผ:
from typing import Union
from fastapi import FastAPI
from fastapi.responses import FileResponse
from pydantic import BaseModel
class Item(BaseModel):
id: str
value: str
app = FastAPI()
@app.get(
"/items/{item_id}",
response_model=Item,
responses={
200: {
"content": {"image/png": {}},
"description": "Return the JSON item or an image.",
}
},
)
async def read_item(item_id: str, img: Union[bool, None] = None):
if img:
return FileResponse("image.png", media_type="image/png")
else:
return {"id": "foo", "value": "there goes my hero"}
Note
๐ ๐ ๐ โ๏ธ ๐จ ๐ผ โ๏ธ FileResponse
๐.
Info
๐ฅ ๐ โ ๐ ๐ป ๐ ๐ฏ ๐ responses
๐ข, FastAPI ๐ ๐ค ๐จ โ๏ธ ๐ ๐ป ๐ ๐ ๐จ ๐ (๐ข application/json
).
โ๏ธ ๐ฅ ๐ โ๏ธ โ ๐ ๐จ ๐ โฎ๏ธ None
๐ฎ ๐ป ๐, FastAPI ๐ โ๏ธ application/json
๐ ๐ ๐จ ๐ โ๏ธ ๐จโ๐ผ ๐ท.
๐ โน¶
๐ ๐ช ๐ ๐จ โน โช๏ธโก๏ธ ๐ ๐ฅ, ๐ response_model
, status_code
, & responses
๐ข.
๐ ๐ช ๐ฃ response_model
, โ๏ธ ๐ข ๐ ๐ 200
(โ๏ธ ๐ 1๏ธโฃ ๐ฅ ๐ ๐ช), & โคด๏ธ ๐ฃ ๐ โน ๐ ๐ ๐จ responses
, ๐ ๐ ๐.
FastAPI ๐ ๐ง ๐ โน โช๏ธโก๏ธ responses
, & ๐ โซ๏ธ โฎ๏ธ ๐ป ๐ โช๏ธโก๏ธ ๐ ๐ท.
๐ผ, ๐ ๐ช ๐ฃ ๐จ โฎ๏ธ ๐ ๐ 404
๐ โ๏ธ Pydantic ๐ท & โ๏ธ ๐ description
.
& ๐จ โฎ๏ธ ๐ ๐ 200
๐ โ๏ธ ๐ response_model
, โ๏ธ ๐ ๐ example
:
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from pydantic import BaseModel
class Item(BaseModel):
id: str
value: str
class Message(BaseModel):
message: str
app = FastAPI()
@app.get(
"/items/{item_id}",
response_model=Item,
responses={
404: {"model": Message, "description": "The item was not found"},
200: {
"description": "Item requested by ID",
"content": {
"application/json": {
"example": {"id": "bar", "value": "The bar tenders"}
}
},
},
},
)
async def read_item(item_id: str):
if item_id == "foo":
return {"id": "foo", "value": "there goes my hero"}
else:
return JSONResponse(status_code=404, content={"message": "Item not found"})
โซ๏ธ ๐ ๐ ๐ & ๐ ๐ ๐, & ๐ฆ ๐ ๏ธ ๐ฉบ:
๐ ๐ข ๐จ & ๐ ๐¶
๐ ๐ช ๐ โ๏ธ ๐ ๐จ ๐ โ ๐ โก ๐ ๏ธ, โ๏ธ ๐ ๐ ๐ ๐ซ โฎ๏ธ ๐ ๐จ ๐ ๐ โก ๐ ๏ธ.
๐ ๐ผ, ๐ ๐ช โ๏ธ ๐ โ "๐" dict
โฎ๏ธ **dict_to_unpack
:
old_dict = {
"old key": "old value",
"second old key": "second old value",
}
new_dict = {**old_dict, "new key": "new value"}
๐ฅ, new_dict
๐ ๐ ๐ ๐-๐ฒ ๐ซ โช๏ธโก๏ธ old_dict
โ ๐ ๐-๐ฒ ๐ซ:
{
"old key": "old value",
"second old key": "second old value",
"new key": "new value",
}
๐ ๐ช โ๏ธ ๐ โ ๐ค-โ๏ธ ๐ข ๐จ ๐ โก ๐ ๏ธ & ๐ ๐ซ โฎ๏ธ ๐ ๐ ๐.
๐ผ:
from typing import Union
from fastapi import FastAPI
from fastapi.responses import FileResponse
from pydantic import BaseModel
class Item(BaseModel):
id: str
value: str
responses = {
404: {"description": "Item not found"},
302: {"description": "The item was moved"},
403: {"description": "Not enough privileges"},
}
app = FastAPI()
@app.get(
"/items/{item_id}",
response_model=Item,
responses={**responses, 200: {"content": {"image/png": {}}}},
)
async def read_item(item_id: str, img: Union[bool, None] = None):
if img:
return FileResponse("image.png", media_type="image/png")
else:
return {"id": "foo", "value": "there goes my hero"}
๐ โน ๐ ๐ ๐จ¶
๐ โซ๏ธโ โซ๏ธโ ๐ ๐ช ๐ ๐จ, ๐ ๐ช โ ๐ ๐ ๐ ๐ง:
- ๐ ๐จ ๐, โซ๏ธ ๐
Response Object
. - ๐ ๐จ ๐, ๐ ๐ช ๐ ๐ณ โช๏ธโก๏ธ ๐ ๐ ๐ ๐จ ๐ ๐
responses
๐ข. โdescription
,headers
,content
(๐ ๐ ๐ ๐ ๐ฃ ๐ ๐ ๐ & ๐ป ๐), &links
.