FastApi的搭建与测试

简介: FastApi的搭建与测试

一、fastapi的安装

1-1、使用pip安装

安装fastapi的语句

pip install fastapi -i https://mirrors.aliyun.com/pypi/simple

因为fastapi启动依赖于uvicorn,所以我们还需要安装uvicorn。

pip install uvicorn -i https://mirrors.aliyun.com/pypi/simple

下面我们来验证一下安装是否成功。

验证

我们创建一个文件,名称叫做main.py。

from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
    return {"message": "Hello World"}

然后我们执行一个指令:

uvicorn main:app --reload
uvicorn main:app 命令含义如下:
main:main.py 文件(一个 Python「模块」)。
app:在 main.py 文件中通过 app = FastAPI() 创建的对象。
--reload:让服务器在更新代码后重新启动。仅在开发时使用该选项。
如果服务器返回了一串结果是:
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [8438] using statreload
INFO:     Started server process [8440]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

打开浏览器访问 http://127.0.0.1:8000

你将看到如下的 JSON 响应:

{"message": "Hello World"}

交互式 API 文档

跳转到 http://127.0.0.1:8000/docs

你将会看到自动生成的交互式 API 文档。

可选参数

通过同样的方式,你可以将它们的默认值设置为 None 来声明可选查询参数:

from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(item_id: str, q: Union[str, None] = None):
    if q:
        return {"item_id": item_id, "q": q}
    return {"item_id": item_id}

在这个例子中,函数参数 q 将是可选的,并且默认值为 None。

查询参数类型转换

你还可以声明 bool 类型,它们将被自动转换:

from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(item_id: str, q: Union[str, None] = None, short: bool = False):
    item = {"item_id": item_id}
    if q:
        item.update({"q": q})
    if not short:
        item.update(
            {"description": "This is an amazing item that has a long description"}
        )
    return item

这个例子中,如果你访问:

http://127.0.0.1:8000/items/foo?short=1

http://127.0.0.1:8000/items/foo?short=True

http://127.0.0.1:8000/items/foo?short=true

http://127.0.0.1:8000/items/foo?short=on

http://127.0.0.1:8000/items/foo?short=yes

或任何其他的变体形式(大写,首字母大写等等),你的函数接收的 short 参数都会是布尔值 True。对于值为 False 的情况也是一样的。

额外的校验

我们打算添加约束条件:即使 q 是可选的,但只要提供了该参数,则该参数值不能超过50个字符的长度。

导入 Query

为此,首先从 fastapi 导入 Query:

from typing import Union
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
相关文章
|
11月前
|
JSON 数据安全/隐私保护 数据格式
python fastapi 入门教程,每个案例都使用postman进行测试写的接口
python fastapi 入门教程,每个案例都使用postman进行测试写的接口
|
测试技术 API 数据库
FastAPI(61)- 异步测试
FastAPI(61)- 异步测试
563 0
|
测试技术 Python
FastAPI 学习之路(三十九)对开发接口进行测试
FastAPI 学习之路(三十九)对开发接口进行测试
FastAPI 学习之路(三十九)对开发接口进行测试
|
JSON 算法 安全
fastapi 安全性 / APIRouter / BackgroundTasks / 元数据 / 测试调试
fastapi 安全性 / APIRouter / BackgroundTasks / 元数据 / 测试调试
253 0
fastapi 安全性 / APIRouter / BackgroundTasks / 元数据 / 测试调试
|
NoSQL 测试技术 Redis
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(下)
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(下)
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(下)
|
测试技术 数据安全/隐私保护
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(上)
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(上)
|
存储 测试技术 数据安全/隐私保护
FastAPI(八十三)实战开发《在线课程学习系统》--注册接口单元测试
FastAPI(八十三)实战开发《在线课程学习系统》--注册接口单元测试
FastAPI(八十三)实战开发《在线课程学习系统》--注册接口单元测试
FastAPI(八十二)实战开发《在线课程学习系统》接口开发-- 课程上架下架
FastAPI(八十二)实战开发《在线课程学习系统》接口开发-- 课程上架下架
|
NoSQL Redis 数据库
FastAPI(八十一)实战开发《在线课程学习系统》接口开发-- 推荐课程列表与课程点赞
FastAPI(八十一)实战开发《在线课程学习系统》接口开发-- 推荐课程列表与课程点赞
FastAPI(八十)实战开发《在线课程学习系统》接口开发-- 课程列表
FastAPI(八十)实战开发《在线课程学习系统》接口开发-- 课程列表