--导入模块
--执行
--使用文档字符串作为规范
@app.route('/xxx/score')
def XXX():
""" This is using docstrings for specifications.
---
tags:
- score #接口的名称
parameters:
- name: 接口主体
in: body(传参方式,可参考postman)
schema:
id: score(接口的名称)
required:(需要传的参数)
- id
- name
- coruse
properties:
id:
type: string
description: 学号
name:
type: string
description: 姓名
course:
type: string
description: 课程
responses:
200:
description: XX正确
400:
description: XX错误
--YAML文件
-- 新建一个score.yml文件
In this example the specification is taken from external YAML file
---
tags:
- score
parameters:
- name: 接口主体
in: body
schema:
id: score(接口的名称)
required:
- id
- name
- course
properties:
id:
type: string
description: 学号
name:
type: string
description: 姓名
course:
type: string
description: 课程
responses:
200:
description: XX正确
400:
description: XX错误
-- 引用yml文件
from flasgger import swag_from
@app.route('/xxx/score', methods=[‘POST’])
@swag_from(‘score.yml’)
def score():
...
-------------------------------------------------------------
或者用下面的方式表示:
@app.route('/xxx/score’, methods=[‘POST’])
def score ():
"""
file:score.yml
"""
...
--字典
-- 新建一个specs_dict字典
specs_dict = {
"parameters": [
{
"name": "接口主体",
"in": "body"
},
{
"schema" : {
"id": "score",
"required": {
"id",
"name",
"course"
},
"properties": {
"id":{
"type": "string",
"description": "学号“
},
"name":{
"type": "string",
"description": "姓名“
},
"course":{
"type": "string",
"description": "课程“
}
}
}
}
]
”responses": {
"200": {
"description": "xx正确“
},
”400“: {
”description": "xx错误“
}
}
}
--引用字典
from flasgger import swag_from
@app.route('/xxx/score’, methods=[‘POST’])
@swag_from(specs_dict)
def score():
"""
In this example the specification is taken from specs_dict
"""
...
--访问
进入http://localhost:5000/apidocs/ 可显示API文档
--验证
-- 导入模块
from flasgger.utils import swag_from, validate
-- 装饰器
@app.route('/xxx/score’, methods=[‘POST’])
@swag_from("score.yml", validation=True)