如何使用python读取openapi json schema并提取API信息

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

我在本地 PC 中保存了一个 openapi json 架构文件。使用 python 脚本,我想读取该 json 文件并提取其中定义的所有 API(包括 API 定义、请求标头、请求正文等...)并在循环中执行所有这些 API。 我可以处理 API 执行部分,但我想知道是否有任何标准 python 库可以读取 openapi json 规范以提取 API 信息。 如果有一个标准的 python 库,请给我一个示例代码,如何从这个 openapi json 中提取 API 详细信息。

作为示例,请查看下面的 openapi json 架构。

{
    "openapi": "3.0.0",
    "info": {
        "title": "Cat Facts API",
        "version": "1.0"
    },
    "paths": {
        "/breeds": {
            "get": {
                "tags": [
                    "Breeds"
                ],
                "summary": "Get a list of breeds",
                "description": "Returns a a list of breeds",
                "operationId": "getBreeds",
                "parameters": [
                    {
                        "name": "limit",
                        "in": "query",
                        "description": "limit the amount of results returned",
                        "required": false,
                        "schema": {
                            "type": "integer",
                            "format": "int64"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "successful operation",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "array",
                                    "items": {
                                        "$ref": "#/components/schemas/Breed"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        "/fact": {
            "get": {
                "tags": [
                    "Facts"
                ],
                "summary": "Get Random Fact",
                "description": "Returns a random fact",
                "operationId": "getRandomFact",
                "parameters": [
                    {
                        "name": "max_length",
                        "in": "query",
                        "description": "maximum length of returned fact",
                        "required": false,
                        "schema": {
                            "type": "integer",
                            "format": "int64"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "successful operation",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/CatFact"
                                }
                            }
                        }
                    },
                    "404": {
                        "description": "Fact not found"
                    }
                }
            }
        },
        "/facts": {
            "get": {
                "tags": [
                    "Facts"
                ],
                "summary": "Get a list of facts",
                "description": "Returns a a list of facts",
                "operationId": "getFacts",
                "parameters": [
                    {
                        "name": "max_length",
                        "in": "query",
                        "description": "maximum length of returned fact",
                        "required": false,
                        "schema": {
                            "type": "integer",
                            "format": "int64"
                        }
                    },
                    {
                        "name": "limit",
                        "in": "query",
                        "description": "limit the amount of results returned",
                        "required": false,
                        "schema": {
                            "type": "integer",
                            "format": "int64"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "successful operation",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "array",
                                    "items": {
                                        "$ref": "#/components/schemas/CatFact"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "Breed": {
                "title": "Breed model",
                "description": "Breed",
                "properties": {
                    "breed": {
                        "title": "Breed",
                        "description": "Breed",
                        "type": "string",
                        "format": "string"
                    },
                    "country": {
                        "title": "Country",
                        "description": "Country",
                        "type": "string",
                        "format": "string"
                    },
                    "origin": {
                        "title": "Origin",
                        "description": "Origin",
                        "type": "string",
                        "format": "string"
                    },
                    "coat": {
                        "title": "Coat",
                        "description": "Coat",
                        "type": "string",
                        "format": "string"
                    },
                    "pattern": {
                        "title": "Pattern",
                        "description": "Pattern",
                        "type": "string",
                        "format": "string"
                    }
                },
                "type": "object"
            },
            "CatFact": {
                "title": "CatFact model",
                "description": "CatFact",
                "properties": {
                    "fact": {
                        "title": "Fact",
                        "description": "Fact",
                        "type": "string",
                        "format": "string"
                    },
                    "length": {
                        "title": "Length",
                        "description": "Length",
                        "type": "integer",
                        "format": "int32"
                    }
                },
                "type": "object"
            }
        }
    }
}
python openapi openapi-generator swagger-codegen
1个回答
0
投票

没有标准库,但您可以使用 openapai3-parser 来解析您的 openapi 规范文档并提取所有必要的信息。查看 GitHub 上的文档了解如何使用它。您可能需要首先使用 PyYAML 或此类库将 json 转换为 yaml。

© www.soinside.com 2019 - 2024. All rights reserved.