pip install flasgger
from flasgger import Swagger
from flask import make_response
from app import create_app
from app.libs.error_code import ServerError
from app.libs.error import APIException, HTTPException
app = create_app()
Swagger(app)
@api.route('', methods=['POST'])
def add():
"""
方法名称:增加用户
方法描述:调用此API增加用户
---
tags:
- 接口名称:用户管理API
parameters:
- name: body
in: body
required: true
schema:
id: user
required:
- account
- name
- password
properties:
account:
type: string
description: 帐号
default: 'XXXX'
name:
type: string
description: 姓名
default: 'XXXX'
phone:
type: string
description: 电话
default: 'XXXX'
email:
type: string
description: 电子邮件
default: 'XXXX'
qq:
type: string
description: qq
default: 'XXXX'
wechat:
type: string
description: wechat
default: 'XXXX'
password:
type: string
description: password
default: 'XXXX'
responses:
500:
description: 状态描述:Error The language is not awesome!
200:
description: 状态描述:返回成功json格式
schema:
id: Success
properties:
code:
type: string
description: 状态码
default: 200
msg:
type: string
description: 信息
default: 'OK'
error_code:
type: string
description: 错误码
default: 0
"""
form = UserForm()
if form.validate_for_api():
User.add(form)
return Success()
@api.route('/<int:id>', methods=['GET'])
def get_by_id(id):
"""
方法名称:通过ID查询用户
方法描述:通过输入ID调用此API查询用户
---
tags:
- 接口名称:用户管理API
parameters:
- name: id
in: path
type: string
required: true
description: 需要查询的用户ID
responses:
500:
description: 状态描述:Error The language is not awesome!
200:
description: 状态描述:返回成功json格式
schema:
id: Success
properties:
code:
type: string
description: 状态码
default: 200
msg:
type: string
description: 信息
default: 'OK'
error_code:
type: string
description: 错误码
default: 0
"""
user = User.get_by_id(id)
return jsonify(user)
测试
http://127.0.0.1:5000/apidocs/