如何用python从api spec yaml文件中获取数据?

1 人关注

我有一个api规格文件,明确定义了api版本。我想从yaml文件中访问api版本数据并将其返回到我的一个端点中。为了做到这一点,我试图读取我的yaml文件,但不能正确访问和获取api版本数据。也许我的代码中存在缺陷。有谁能告诉我怎样才能做到这一点?有什么可能的想法吗?我怎样才能用python从yaml文件中以编程方式获取和检索api版本?

api spec 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:
          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'])

更新:错误

在测试了上述代码后,我有如下错误。

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

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

python
yaml
jyson
jyson
发布于 2020-05-19
2 个回答
Jerry07
Jerry07
发布于 2020-05-19
已采纳
0 人赞同

我想你可以这样做,这将从你的yaml文件中得到版本。

import yaml
spec_yaml= 'apispec.yaml'
with open(spec_yaml, 'r') as f:
    data = yaml.load(f)
print(data['info']['version'])
    
dumbPy
dumbPy
发布于 2020-05-19
0 人赞同

首先,你的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:
      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

Once you have that, see that the version you are looking for is at info -> version

So you simply need,

import yaml