简体   繁体   English

如何从 python 中的 api 规范 yaml 文件中检索数据?

[英]how to retrieve data from api spec yaml file in python?

I have api spec file which explicitly defined api version.我有 api 规范文件,它明确定义了 api 版本。 I want to access api version data from yaml file and return it to one of my endpoints.我想从 yaml 文件中访问 api 版本数据并将其返回到我的端点之一。 To do so, I tried to read my yaml file but cannot access and fetch api version data correctly.为此,我尝试读取我的 yaml 文件,但无法正确访问和获取 api 版本数据。 Perhaps I might defect in my code.也许我可能会在我的代码中出现缺陷。 Can anyone point me out how to make this right?谁能指出我如何做到这一点? any possible thought?任何可能的想法? how can I fetch and retrieve api version programmatically from yaml file in python?如何以编程方式从 python 中的 yaml 文件中获取和检索 api 版本?

api spec yaml definition api 规范 yaml 定义

here is how openapi spec fiel defined:这是 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

my attempt :我的尝试

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'])

update: error更新:错误

I have this error down below after testing above code:在测试上面的代码后,我在下面遇到了这个错误:

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

this is not working for me.这对我不起作用。 any way I can access my api spec yaml file correctly and fetch api version in python function? any way I can access my api spec yaml file correctly and fetch api version in python function? any idea?任何想法?

I think you can do like this which would give you version from your yaml file:我认为您可以这样做,这将为您提供 yaml 文件中的版本:

import yaml

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

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

First of all, your yaml has invalid structure.首先,您的 yaml 结构无效。 openapi: can either have a value here 3.0.0 , or have inner levels indented below it ( info: in your case). openapi:可以在此处具有值3.0.0 ,也可以在其下方缩进内部级别( info:在您的情况下)。 Not both.不是都。 A simple json equivalent of your yaml is {openapi:3.0.0 {info:...}} which is invalid.一个简单的 json 等效于您的 yaml 是{openapi:3.0.0 {info:...}}这是无效的。 So remove the indentation from info: and the subsequent rows below it所以从info:和它下面的后续行中删除缩进

Here's the corrected yaml这是更正后的 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

Once you have that, see that the version you are looking for is at info -> version一旦你有了它,看到你正在寻找的版本是在 info -> version

So you simply need,所以你只需要,

import yaml

spec_yaml= 'apispec.yaml'
with open(spec_yaml, 'r') as f:
    data = yaml.load(f)
print(data['info']['version'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM