我有一个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版本? 有什么想法吗?