用swagger生成api的接口文档(yaml版)

swagger的文档可以用json和yaml写,这里我用的yaml写的,会以文档+效果图表现出来:

swagger: "2.0"  
info:
  description: "this is a api for authenticating users and binding cloud accounts."
  version: "1.0.0"
  title: "Auth api"
 
host: "52.82.26.240:5000"

swagger: “2.0” 指定swagger的版本号
info: 关于该api的信息,版本和标题
host: 表示你的ip地址或是域名

paths:
  /authenticate:
    post:
      tags:
      - "auth"
      summary: "Get a accessToken"
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
      - name: "body"
        description: "account of fastone"
        required: true
        in: "body"
        schema:
          $ref: "#/definitions/Auth"
      responses:
        400:
          description: "Invalid input"
        403:
          description: "Invalid credential"
        200:
          description: "Get a accessToken"

paths: 路由地址
post: 请求方法
tags: 命名空间
summary: 描述信息
consumes:消费格式‘application/json’和‘application/xml’
produces:生产格式‘application/json’和‘application/xml’
parameters:参数
name: 参数名字
description:描述
required:是否必要
in:属于哪种参数 body, header, formData, query, path
body只能有一个,body里的参数需要以model的形式通过schema放在里面
schema:
$ref: “#/definitions/Auth”
$ref: 把model 在definitions里的Auth当作参数放入body中。
model Auth的写法,会放到yaml的最后

definitions:
  Auth:
    type: "string"
    properties:
      usernameOrEmail:
        type: "string"
      password: 
        type: "string"

responses: 状态码

/accounts/{usernameOrEmail}:
    get:
      tags:
      - "accounts"
      summary: "Finds all information by usernameOrEmail"
      description: ""
      produces:
      - "application/json"
      parameters:
      - name: "accessToken"
        in: "header"
        required: true
        type: "string"
      - name: "usernameOrEmail"
        in: "path"
        description: "account of user that needs to be finded"
        required: true
        type: "string"
      responses:
        200:
          description: "successful operation"
          schema:
            $ref: "#/definitions/User"
        400:
          description: "Invalid ID supplied"
        404:
          description: "Invalid input"
        405:
          description: "Validation exception"

这里说下parameters,header是请求头,path是请求参数,可以有多个。

最后把文件结构给出:

python flask 自动生成api文档点击这里
以上!

swagger editor 链接:swagger editor

  • 11
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值