在同一def中使用Swagg的HTTP方法的语法

2024-04-27 02:52:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新来的。在python代码中,我有一个名为“work”的API,它支持POST、PUT和DELETE HTTP方法。在

现在我想为同样的情况创建Swagger文档。我使用以下代码:

@app.route('/work', methods=['POST', 'PUT', 'DELETE'])
def work():
"""
    Micro Service Based API for work operations
    This API is for work to task matching operations
    ---
    paths:
      /cv:
        put:
          parameters:
            - name: body
              in: body
              required: true
              schema:
                id: data
                properties:
                    _id:
                        type: string
              description: Id
          responses:
                    200:
                        description: Please wait the calculation, you'll receive an email with results
        delete:
          parameters:
            - name: body
              in: body
              required: true
              schema:
                id: data
                properties:
                    _id:
                        type: string
              description: Id
          responses:
                    200:
                        description: Please wait the calculation, you'll receive an email with results
        post:
          responses:
                    200:
                        description: done
"""

然而,这似乎行不通。在

我试着浏览下面的文档链接,但没什么帮助 https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#pathsObject

你能帮帮我吗?在

每个httpmethods请求的参数是不同的,我还希望在httpui中为每个方法指定不同的描述。在

编辑

将此添加到索引.yml文件。在

^{pr2}$

对python代码进行了上述更改。在

@app.route('/work', methods=['POST', 'PUT', 'DELETE'])
@swag_from('index.yml')
def work():

但是http://127.0.0.1:5000/apidocs/#!/default/根本没有显示任何内容。在


Tags: 方法代码文档apiidappputbody
1条回答
网友
1楼 · 发布于 2024-04-27 02:52:22

如果您使用的是flasger(http://github.com/rochacbruno/flasgger) 遗憾的是,它还不支持在同一个docstring中定义不同的HTTP方法, 这是issue opened。在

但是,有一个变通方法可以让它发挥作用。在

1)将您的YAML放入单独的文件中
2) 从Swagger模板文件加载

YAML文件,另存为测试.yaml公司名称:

definitions:
  Data:
    type: object
    properties:
      _id:
        type: string

paths:
  /cv:
    put:
      parameters:
        - name: body
          in: body
          required: true
          schema:
            $ref: '#/definitions/Data'
      responses:
        200:
          description: |
            Please wait the calculation, you'll receive an email with results
    delete:
      parameters:
        - name: body
          in: body
          required: true
          schema:
            $ref: '#/definitions/Data'
      responses:
        200:
          description: |
            Please wait the calculation, you'll receive an email with results
    post:
      responses:
        200:
          description: done

然后呢测试.py在

^{pr2}$

你得到了

flasgger

相关问题 更多 >