fastapi编写接口并批量运行
为什么要写这fastapi接口以及拿它做什么呢?
fastapi可以快速构建你自己的api,前端后端联调时,后端接口还有做好,那么这个fastapi可以快速生成mock一些数据。
结合uvicorn这个python库使用起来很方便
为接口自动化研究做demo接口,专门拿来作为接口postman调试。
**目录结构如下:
其中user.py的内容如下
#没有导入FastAPI,,而是导入APIRouter,这个是方便可以运行多个fastapi的app应用
from fastapi import APIRouter
import uvicorn
# typing和pydantic是用于请求参数的控制的
from typing import Optional
from pydantic import BaseModel
router = APIRouter(tags=["fastApi_01"], responses={404: {"description": "Not found"}})
# 定义输入的请求参数
class Car(BaseModel):
name: str # 必选 名字
weight: Optional[str] = None # 可选参数
price: float # 必选参数
length: Optional[str] = None # 可选参数
# 定义接口的输入请求参数
class deskTop(BaseModel):
name: str #颜色
color: str #颜色
length: str #长度
tiji: Optional[int] = None #体积可选
@router.post("/car")
def create_item(car : Car):
return {"code": 200, "msg": "新增成功", "data": car}
@router.post("/desktop")
def create_item2(desktop : deskTop):
return {"code": 200, "msg": "新增成功", "data": desktop}
@router.get("/")
def index():
return {"city": "chengdu"}
@router.get("/list")
def list():
return ["one", "two", "three"]
@router.get("/user/list")
def getList_user():
#返回用户列表
return {"code": 1, "msg": "修改成功", "data": [
{"id": 1, "username": "wangjie", "age": 21},
{"id": 2, "username": "wanghao", "age": 12},
{"id": 3, "username": "liubo", "age": 31},
{"id": 4, "username": "zhangxinhua", "age": 76},
{"id": 5, "username": "liuneng", "age": 65}
]}
@router.post("/user/{id}")
def user(id):
return {"code": 1, "msg": "新增成功", "id": id}
@router.put("/user")
def update(id,name):
if id==1:
return {"code": 1, "msg": "修改成功", "id": id, "name": name}
else:
return {"code": 0, "mag": "未找到结果"}
@router.delete("/user")
def delete_user(id):
return {"code": 1, "msg": "删除成功", "data": {"id": id}}
# if __name__ == '__main__':
# uvicorn.run(app)
# 在终端terminal中使用uvicorn重启
# 终端输入:uvicorn --reload 文件名:app的名称 比如:uvicorn main:app --reload
fastApi_02.py文件模块的内容如下:
from fastapi import APIRouter
import uvicorn
# typing和pydantic是用于请求参数的控制的
from typing import Optional
from pydantic import BaseModel
router = APIRouter(tags=["fastApi_02"], responses={404: {"description": "Not found"}})
class Station:
id: int # 基站为int 的id
name: str #基站名称
addr: str #定位地址
status: bool #是否在线 1-true, 不在线为0-false
@router.get("/station/list")
def getStation_list():
return {"code": 1, "msg": "查询列表成功", "data": [
{"id": 1001, "name": "基站1", "addr": "成都双流区二楼1号南", "status": True},
{"id": 1002, "name": "基站2", "addr": "成都双流区二楼2号北", "status": False},
{"id": 1003, "name": "基站3", "addr": "成都双流区二楼3号东", "status": True},
{"id": 1004, "name": "基站4", "addr": "成都双流区二楼4号西", "status": False},
{"id": 1005, "name": "基站5", "addr": "成都双流区二楼5号东", "status": True}
]}
@router.post("/station")
def add_station():
return {"code": 1, "msg": "新增基站成功", "data": {"name": "基站1", "type": "A"}}
@router.put("/station")
def update_station():
return {"code": 1, "msg": "更新基站成功", "data": {
"id": 1002,
"name": "基站2",
"addr": "四川省体育馆",
"status": True
}}
@router.delete("/station")
def del_station():
return {"code": 1, "msg": "删除基站成功", "data": {}}
# if __name__ == '__main__':
# uvicorn.run(app)
最终呢,只要运行main.py即可访问fastApi_01和fastApi-02的相关接口了。
main/main.py:
import os
from app.routers import fastApi_01, fastApi_02
from fastapi import FastAPI
# from mongoengine import *
# 这里不需要使用mangodb数据库
# connect("test_fastapi", host='localhost', port=27017)
app = FastAPI()
app.include_router(fastApi_01.router)
app.include_router(fastApi_02.router)
在cmd项目根目录下或是terminal终端(如是虚拟环境,切换到虚拟环境运行),运行代码:
#指定host和port运行,main.main是模块main下边的main.py文件,app是对应的FastAPI的对象实例,reload是每次更新代码后自动重新启动。
uvicorn main.main:app --reload --host=192.168.x.x --port=52123