一个网站多大空间,写软文的平台有哪些,湖南网站推广建设公司,wordpress+媒体路径简介
FastAPI是流行的Python web框架#xff0c;适用于开发高吞吐量API和微服务#xff08;直接支持异步编程#xff09;
FastAPI的优势之一#xff1a;通过提供高级抽象和自动数据模型转换#xff0c;简化请求数据的处理#xff08;用户不需要手动处理原始请求数据适用于开发高吞吐量API和微服务直接支持异步编程
FastAPI的优势之一通过提供高级抽象和自动数据模型转换简化请求数据的处理用户不需要手动处理原始请求数据并能根据路由和 Pydantic 模型自动生成 OpenAPI 接口文档。
Swagger UIReDoc
demo
import uuid
import uvicorn
from typing import Any, Union, Optional
from typing_extensions import Literal
from fastapi import Body, FastAPI
from pydantic import (BaseModel,Field,UUID4
)app FastAPI()class UserIn(BaseModel):channel: Literal[0, 1] Field(0, title渠道)username: str Field(..., title用户名)password: str Field(..., title用户密码, description长度6-8位)email: str Field(..., title用户邮箱地址)full_name: str Field(None, title用户全名)request_id: Optional[UUID4]class UserOut(BaseModel):username: str Field(..., title用户名)email: str Field(..., title用户邮箱地址)full_name: str Field(None, title用户全名)request_id: Optional[UUID4]# FastAPI will take care of filtering out all the data that is not declared in the output model (using Pydantic).
# 因此FastAPI将负责过滤掉输出模型中未声明的所有数据使用Pydantic。app.post(/user/, response_modelUserOut)
async def create_user(user: UserIn Body(examples{example1: {summary: A short summary or description of the example,value: {# example data herechannel: 0,username: Foo,password: 33759,email: chencare163.com,full_name: xiaotao}}})
) - UserOut:user.request_id uuid.uuid4()print(user.request_id)return userif __name__ __main__:uvicorn.run(appapp, access_logTrue, port9988)
运行后会提示Uvicorn running on http://127.0.0.1:9988 (Press CTRLC to quit)
在浏览器输入http://127.0.0.1:9988/redoc ReDoc,http://127.0.0.1:9988/docsSwagger UI 即可查看
ReDoc 页面如下
ReDoc vs. Swagger UI
ReDoc更美观Swagger UI更注重交互用户直接从界面中发送请求查看响应这对于测试和调试 API 非常有用。