flask11:flasgger

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/

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值