API接口文档及验证详细教程

  Flask 是一款流行的 Python 实现的 Web 开发微框架;
  Swagger 是一款 Restful 接口的文档在线自动生成 + 功能测试功能软件;
  通过 swagger 能够清晰、便捷地调试符合 Restful 规范的 API
  在 flask 框架中使用的 swagger 即为 flasgger flasgger flask 支持的 swagger UI ,便于调试使用 flask 框架搭建的 web api 接口;
--安装
   pip install -U setuptools
   pip install flasgger

--导入模块

 --执行

 --使用文档字符串作为规范

@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)

       


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值