文档的详细编写可以提高开发效率,swagger的合理应用更是如此,之前项目中的swagger只是一个"静态的文档",故此抽空优化了下,并将登录认证加到项目的swagger中.使得接口的安全也有所保障.
一. 在入口文件配置以下信息
def register_swagger(app):
"""
注册swagger
"""
from flasgger import Swagger
template = {
"securityDefinitions": {'basicAuth': {'type': 'basic'}}
}
Swagger(app, template=template)
二. 在views中写入
""" 多文件上传
---
tags:
- 用户模块
...省略
# 登录认证配置
security:
- basicAuth: []
"""
将username里替换成我们生成的token,password随便填写,我写"1"
认证成功
正规一点的写法
@api.route(''):
def shop_add():
"""
发布商品信息
---
tags:
- 商品
parameters:
- in: "body"
name: "body"
description: "需要填写的参数,请认真对应"
required: true
schema:
$ref: "#/definitions/AddShop" # 引用 'definitions'
definitions:
AddShop: # 命名和返回的名称要一致
type: "object"
properties:
category_id:
type: "string"
goods_sn:
type: "string"
name:
type: "广告地址"
format: "formData"
shop_id:
type: "string"
json: # 返回名称和命名要一致,除了json也可以用 'xml'
name: "AddShop"
responses:
200:
description: 返回信息
examples:
success: {"auth": 777,"error_code": 0,"success_token": "......."}
405:
description: "Invalid input"
security: # 认证方式为 basic Auth
- basicAuth: []
"""
**ps:**
security: # 认证方式为N种,例如
- bearAuth: []
"securityDefinitions": {'bearAuth': {'type': 'bear'}} # 入口文件里也要修改.
pass
Tags: None
Archives QR Code