
理解API代码:高效REST API集成的综合指南
FastAPI 是一个快速、轻量级的 Web 框架,用于使用 Python 3.6 及更高版本构建现代应用程序编程接口。在本教程中,我们将介绍使用 FastAPI 构建应用程序的基础知识,您将了解为什么它被提名为 2021 年最佳开源框架之一。
一旦你准备好开发自己的 FastAPI 应用程序,你就不必走远就能找到一个托管它们的地方。Kinsta 的应用程序托管和数据库托管服务提供在 Python 上强大的平台即服务。
让我们先了解基础知识。
以下是 FastAPI 框架为项目带来的一些优势。
type
要按照本教程开始使用 FastAPI,你需要先做几件事。
确保您拥有程序员的文本编辑器/IDE,例如 Visual Studio Code。其他选项包括 Sublime Text 和 Espresso。
通常的做法是让 Python 应用程序及其实例在虚拟环境中运行。虚拟环境允许同时运行不同的软件包集和配置,并避免由于软件包版本不兼容而引起的冲突。
要创建虚拟环境,请打开终端并运行以下命令:
$ python3 -m venv env
您还需要激活虚拟环境。执行此操作的命令会因您使用的操作系统和 shell 而异。以下是许多环境的一些 CLI 激活示例:
# On Unix or MacOS (bash shell):
/path/to/venv/bin/activate
# On Unix or MacOS (csh shell):
/path/to/venv/bin/activate.csh
# On Unix or MacOS (fish shell):
/path/to/venv/bin/activate.fish
# On Windows (command prompt):
\path\to\venv\Scripts\activate.bat
# On Windows (PowerShell):
\path\to\venv\Scripts\Activate.ps1
(某些 Python 感知 IDE 也可以配置为激活当前虚拟环境。
现在,安装 FastAPI:
$ pip3 install fastapi
FastAPI 是一个用于构建 API 的框架,但要测试你的 API,你需要一个本地 Web 服务器。Uvicorn 是一个闪电般快速的异步服务器网关接口 (ASGI) Web 服务器,适用于 Python,非常适合开发。要安装 Uvicorn,请运行以下命令:
$ pip3 install "uvicorn[standard]"
成功安装后,在项目的工作目录中创建一个名为 main.py 的文件。此文件将是您的应用程序入口点。
您将通过快速设置示例终端节点来测试您的 FastAPI 安装。在 main.py 文件中,粘贴以下代码,然后保存文件:
# main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"greeting":"Hello world"}
上面的代码段创建了一个基本的 FastAPI 端点。以下是每行的作用摘要:
from fastapi import FastAPI
:API 的功能由 FastAPI Python 类提供。app = FastAPI()
:这将创建一个 FastAPI 实例。@app.get("/")
:这是一个 python 装饰器,它向 FastAPI 指定它下面的函数负责请求处理。@app.get("/")
:这是一个指定路由的装饰器。这将在站点的路由上创建一个方法。然后,包装函数返回结果。GET
@app.post()
@app.put()
@app.delete()
@app.options()
@app.head()
@app.patch()
@app.trace()
在 files 目录中,在终端中运行以下命令以启动 API 服务器:
$ uvicorn main:app --reload
在此命令中, 是模块的名称。该对象是应用程序的一个实例,并被导入到 ASGI 服务器中。该标志告诉服务器在您进行任何更改时自动重新加载。main
app
--reload
您应该在终端中看到如下内容:
$ uvicorn main:app --reload
INFO: Will watch for changes in these directories: ['D:\\WEB DEV\\Eunit\\Tests\\fast-api']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [26888] using WatchFiles
INFO: Started server process [14956]
INFO: Waiting for application startup.
INFO: Application startup complete.
在浏览器中,导航到以确认您的 API 是否正常工作。您应该会在页面上看到 “Hello”: “World” 作为 JSON 对象。这说明了使用 FastAPI 创建 API 是多么容易。您所要做的就是定义一个路由并返回您的 Python 字典,如上面代码段的第 6 行所示。http://localhost:8000
如果您使用 Python,则习惯于使用基本数据类型(如 、 、 和 )对变量进行注释。但是,从 Python 版本 3.9 开始,引入了高级数据结构。这允许您使用数据结构,例如 、 和 。借助 FastAPI 的类型提示,您可以使用 pydantic 模型构建数据架构,然后使用 pydantic 模型进行类型提示并从提供的数据验证中受益。intstrfloatbooldictionariestupleslists
在下面的示例中,使用一个简单的餐费计算器演示了 Python 中类型提示的使用:calculate_meal_fee
def calculate_meal_fee(beef_price: int, meal_price: int) -> int:
total_price: int = beef_price + meal_price
return total_price
print("Calculated meal fee", calculate_meal_fee(75, 19))
请注意,类型提示不会更改代码的运行方式。
FastAPI 使用 Swagger UI 提供自动交互式 API 文档。要访问它,请导航到 ,您将看到一个包含所有终端节点、方法和架构的屏幕。http://localhost:8000/docs
这个基于浏览器的自动 API 文档由 FastAPI 提供,您无需执行任何其他操作即可利用它。
另一个基于浏览器的 API 文档,也是由 FastAPI 提供的,是 Redoc。要访问 Redoc,请导航到 ,其中将显示端点、方法及其各自响应的列表。http://localhost:8000/redoc
装饰器允许您指定路由的方法,例如 or ,并支持 、 、 、 和 ,以及不太常见的选项 、 、 和 。@app@app.get@app.postGETPOSTPUTDELETEHEADPATCHTRACE
在本教程中,您将逐步使用 FastAPI 构建 CRUD 应用程序。该应用程序将能够:
要执行这些 CRUD 操作,您将创建公开 API 端点的方法。结果将是一个可以存储用户列表的内存数据库。
您将使用 pydantic 库通过 Python 类型注释执行数据验证和设置管理。在本教程中,您将把数据的形状声明为具有属性的类。
本教程将使用 in-memory 数据库。这是为了让你快速开始使用 FastAPI 来构建你的 API。但是,对于生产,您可以使用您选择的任何数据库,例如 PostgreSQL、MySQL、SQLite 甚至 Oracle。
首先,您将创建用户模型。用户模型将具有以下属性:
id
:通用唯一标识符 (UUID)first_name
:用户的名字last_name
:用户的姓氏gender
:用户的性别roles
,这是一个包含 和 角色的列表admin
user
首先在工作目录中创建一个名为 models.py 的新文件,然后将以下代码粘贴到 models.py 以创建模型:
# models.py
from typing import List, Optional
from uuid import UUID, uuid4
from pydantic import BaseModel
from enum import Enum
from pydantic import BaseModel
class Gender(str, Enum):
male = "male"
female = "female"
class Role(str, Enum):
admin = "admin"
user = "user"
class User(BaseModel):
id: Optional[UUID] = uuid4()
first_name: str
last_name: str
gender: Gender
roles: List[Role]
在上面的代码中:
User
BaseModel
pydantic
下一步是创建数据库。将 main.py 文件的内容替换为以下代码:
# main.py
from typing import List
from uuid import uuid4
from fastapi import FastAPI
from models import Gender, Role, User
app = FastAPI()
db: List[User] = [
User(
id=uuid4(),
first_name="John",
last_name="Doe",
gender=Gender.male,
roles=[Role.user],
),
User(
id=uuid4(),
first_name="Jane",
last_name="Doe",
gender=Gender.female,
roles=[Role.user],
),
User(
id=uuid4(),
first_name="James",
last_name="Gabriel",
gender=Gender.male,
roles=[Role.user],
),
User(
id=uuid4(),
first_name="Eunit",
last_name="Eunit",
gender=Gender.male,
roles=[Role.admin, Role.user],
),
]
在 main.py:
db
List
User
first_name
last_name
gender
roles
Eunit
admin
user
user
您已成功设置内存数据库并在其中填充了用户,因此下一步是设置一个终端节点,该终端节点将返回所有用户的列表。这就是 FastAPI 的用武之地。
在 main.py 文件中,将以下代码粘贴到 endpoint 的正下方:Hello World
# main.py
@app.get("/api/v1/users")
async def get_users():
return db
此代码定义端点 ,并创建一个异步函数 ,该函数返回数据库 的所有内容 。/api/v1/users
get_users
db
保存您的文件,然后您可以测试您的用户终端节点。在终端中运行以下命令以启动 API 服务器。
$ uvicorn main:app --reload
在浏览器中,导航到 。这应该会返回所有用户的列表,如下所示:http://localhost:8000/api/v1/users
在此阶段,您的 main.py 文件将如下所示:
# main.py
from typing import List
from uuid import uuid4
from fastapi import FastAPI
from models import Gender, Role, User
app = FastAPI()
db: List[User] = [
User(
id=uuid4(),
first_name="John",
last_name="Doe",
gender=Gender.male,
roles=[Role.user],
),
User(
id=uuid4(),
first_name="Jane",
last_name="Doe",
gender=Gender.female,
roles=[Role.user],
),
User(
id=uuid4(),
first_name="James",
last_name="Gabriel",
gender=Gender.male,
roles=[Role.user],
),
User(
id=uuid4(),
first_name="Eunit",
last_name="Eunit",
gender=Gender.male,
roles=[Role.admin, Role.user],
),
]
@app.get("/")
async def root():
return {"Hello": "World",}
@app.get("/api/v1/users")
async def get_users():
return db
下一步是创建终端节点以在数据库中创建新用户。将以下代码片段粘贴到 main.py 文件中:
# main.py
@app.post("/api/v1/users")
async def create_user(user: User):
db.append(user)
return {"id": user.id}
在此代码段中,您定义了用于提交新用户的终端节点,并使用装饰器创建方法。@app.post
POST
您还创建了函数 ,该函数接受模型,并将新创建的 追加(添加)到数据库 。最后,端点返回新创建的用户的 JSON 对象。create_user
user
User
user
db
id
您必须使用 FastAPI 提供的自动 API 文档来测试您的端点,如上所示。这是因为您无法使用 Web 浏览器发出 POST 请求。导航到使用 SwaggerUI 提供的文档进行测试。http://localhost:8000/docs
由于您正在构建 CRUD 应用程序,因此您的应用程序需要能够删除指定的资源。在本教程中,您将创建一个终端节点来删除用户。
将以下代码粘贴到 main.py 文件中:
# main.py
from uuid import UUID
from fastapi HTTPException
@app.delete("/api/v1/users/{id}")
async def delete_user(id: UUID):
for user in db:
if user.id == id:
db.remove(user)
return
raise HTTPException(
status_code=404, detail=f"Delete user failed, id {id} not found."
)
以下是该代码工作原理的逐行细分:
@app.delete("/api/v1/users/{id}")
:您使用装饰器创建了删除端点。路径仍为 ,但随后它会检索 ,该 是对应于用户 id 的 path 变量。@app.delete()
/api/v1/users/{id}
id
async def delete_user(id: UUID):
:创建函数,该函数从 URL 中检索 。delete_user
id
for user in db:
:这会告知应用程序遍历数据库中的用户,并检查传递的内容是否与数据库中的用户匹配。id
db.remove(user)
:如果匹配到某个用户,则该用户将被删除;否则,将引发状态代码为 404 的 an。idHTTPException
您将创建一个终端节点来更新用户的详细信息。可更新的详细信息包括以下参数:、 和 。first_name
last_name
roles
在 models.py 文件中,将以下代码粘贴到 model 下,即 class 之后:User
User(BaseModel):
# models.py
class UpdateUser(BaseModel):
first_name: Optional[str]
last_name: Optional[str]
roles: Optional[List[Role]]
在此代码段中,类扩展了 .然后,您将可更新的用户参数(如 、 和 )设置为可选参数。UpdateUser
BaseModel
first_name
last_name
roles
现在,您将创建一个终端节点来更新特定用户的详细信息。在 main.py 文件中,将以下代码粘贴到 decorator 之后:@app.delete
# main.py
@app.put("/api/v1/users/{id}")
async def update_user(user_update: UpdateUser, id: UUID):
for user in db:
if user.id == id:
if user_update.first_name is not None:
user.first_name = user_update.first_name
if user_update.last_name is not None:
user.last_name = user_update.last_name
if user_update.roles is not None:
user.roles = user_update.roles
return user.id
raise HTTPException(status_code=404, detail=f"Could not find user with id: {id}")
在上面的代码中,您已完成以下操作:
@app.put("/api/v1/users/{id}")
id
update_user
UpdateUser
id
for
id
is not None
first_name
last_name
roles
HTTPException
Could not find user with id: {id}
要测试此终端节点,请确保您的 Uvicorn 服务器正在运行。如果它未运行,请输入以下命令:
uvicorn main:app --reload
下面是测试的屏幕截图。
在本教程中,你了解了 Python 的 FastAPI 框架,并亲眼看到了如何快速启动和运行 FastAPI 驱动的应用程序。您学习了如何使用框架构建 CRUD API 终端节点 — 创建、读取、更新和删除数据库记录。