FastAPI
チュートリアル
簡単な実装
code:main.py
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Message(BaseModel):
message: str
@app.get("/hello", response_model=Message)
async def root(name: str, age: int):
return {"message": f"Hello {name}({age})"}
起動
code:shell
(venv)$ uvicorn main:app --reload
INFO: Started server process 4080 INFO: Waiting for application startup.
INFO: Application startup complete.
code:openapi.json
{
"openapi": "3.0.2",
"info": {
"title": "FastAPI",
"version": "0.1.0"
},
"paths": {
"/hello": {
"get": {
"summary": "Root",
"operationId": "root_hello_get",
"parameters": [
{
"required": true,
"schema": {
"title": "Name",
"type": "string"
},
"name": "name",
"in": "query"
},
{
"required": true,
"schema": {
"title": "Age",
"type": "integer"
},
"name": "age",
"in": "query"
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Message"
}
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {
"$ref": "#/components/schemas/ValidationError"
}
}
}
},
"Message": {
"title": "Message",
"required": [
"message"
],
"type": "object",
"properties": {
"message": {
"title": "Message",
"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"
}
}
}
}
}
}