如何在 python 中从 api spec yaml 文件中检索数据?

问题描述 投票:0回答:2

我有一个 api spec 文件,其中明确定义了 api 版本。我想从yaml文件中访问api版本数据,并将其返回到我的一个端点。为此,我尝试读取我的yaml文件,但不能正确地访问和获取api版本数据。也许我的代码有缺陷。有谁能指点我如何使这个问题得到解决吗?

api规范yaml定义

这里是openapi规范的定义。

openapi: 3.0.0
    info:
      title: test REST API
      description: Test REST API
      license:
        name: Apache 2.0
        url: https://www.apache.org/licenses/LICENSE-2.0.html
      version: 1.0.0
    servers:
    - url: /api/v1/
      description: test

    paths:
      /about:
        get:
          summary: api System Version
          description: Obtain current version of test api
          operationId: about_get
          responses:
            "200":
              description: About information
              content:
                application/json:
                  schema:
                    $ref: '#/components/schemas/version'
            "401":
              description: Authorization information is missing or invalid.
          x-openapi-router-controller: test_server.controllers.default_controller
      /rsession:

    components:
      schemas:
        version:
          required:
          - mayor
          - minor
          - name
          - patch
          type: object
          properties:
            name:
              type: string
            mayor:
              type: number
            minor:
              type: number
            patch:
              type: number
          description: api Version
          example:
            name: api
            mayor: 1
            minor: 0
            patch: 0

我的尝试:

import yaml

spec_yaml= 'apispec.yaml'
with open(spec_yaml, 'r') as f:
    data = yaml.load(f)

result = {}
for elem in data['info']:
    name = elem.pop('version')
    result[name] = elem

data['apiversion'] = result

print(data['version'])

更新:错误

在测试了上面的代码后,我出现了下面这个错误,这对我来说是行不通的,我有什么办法可以正确地访问我的api spec yaml文件,并在python函数中获取api版本?

AttributeError: 'str' object has no attribute 'pop'

有什么办法可以让我正确地访问我的 api spec yaml 文件,并在 python 函数中获取 api 版本? 有什么办法?

python yaml
2个回答
1
投票

我想你可以这样做,这样就可以从你的yaml文件中获得版本数据。

import yaml

spec_yaml= 'apispec.yaml'
with open(spec_yaml, 'r') as f:
    data = yaml.load(f)

print(data['info']['version'])

1
投票

首先,你的yaml文件结构无效。openapi: 可以在这里有一个值 3.0.0或其下方有内层缩进(info: 在你的情况下)。) 不是两个都有。你的yaml的一个简单的json对应的是 {openapi:3.0.0 {info:...}} 这是无效的。因此,请删除缩进处的 info: 后面的行

以下是修改后的yaml

openapi: 3.0.0
info:
  title: test REST API
  description: Test REST API
  license:
    name: Apache 2.0
    url: https://www.apache.org/licenses/LICENSE-2.0.html
  version: 1.0.0
servers:
- url: /api/v1/
  description: test

paths:
  /about:
    get:
      summary: api System Version
      description: Obtain current version of test api
      operationId: about_get
      responses:
        "200":
          description: About information
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/version'
        "401":
          description: Authorization information is missing or invalid.
      x-openapi-router-controller: test_server.controllers.default_controller
  /rsession:

components:
  schemas:
    version:
      required:
      - mayor
      - minor
      - name
      - patch
      type: object
      properties:
        name:
          type: string
        mayor:
          type: number
        minor:
          type: number
        patch:
          type: number
      description: api Version
      example:
        name: api
        mayor: 1
        minor: 0
        patch: 0

一旦你有了这些,看看你要找的版本是在info -> 版本。

所以你只需要。

import yaml

spec_yaml= 'apispec.yaml'
with open(spec_yaml, 'r') as f:
    data = yaml.load(f)
print(data['info']['version'])
© www.soinside.com 2019 - 2024. All rights reserved.