gpt4 book ai didi

python - Swagger-ui 连接未找到 Python 嵌套函数

转载 作者:行者123 更新时间:2023-12-01 01:18:50 24 4
gpt4 key购买 nike

我正在尝试使用 Swagger 记录已经存在的 python API 。我在他们 editor 的帮助下编写了 swagger.yaml,其中记录了每条路线。 。现在我想使用 connexion 部署文档.

(swagger.yaml 文件的简短示例)

swagger: "2.0"
info:
description: "This is a description of the routes of the API"
version: "1.0.0"
title: "API"

basePath: "/api"

paths:
/home:
get:
tags:
- "API"
summary: "Home of the application"
operationId: home
responses:
200:
description: "Success"
schema:
type: object
properties:
user_id:
type: string
username:
type: string
403:
description: "Limit of api connections overrun"

我在服务器启动期间通过 connexion.app 更改了 Flask 应用程序,并且能够指定 .yaml 文件。但是当我尝试启动它时,它立即崩溃:

  File "/usr/local/lib/python2.7/dist-packages/connexion/utils.py", line 74, in get_function_from_name
raise ValueError("Empty function name")
exceptions.ValueError: Empty function name

据我了解,connexion 的手动测试功能将基于每个需要指向处理请求的函数的路由中的对象 operationId 的手动测试功能。问题:API的每个路由都被定义为嵌套函数。

def add_routes(app, oauth):
@app.route('/api/home', methods=['GET'])
@oauth.require_oauth()
def home():
user = request.oauth.user
return jsonify(
user_id=user.user_id,
username=user.username
)

我知道Python中的嵌套函数实际上根本不是函数:不可调用,只是存在于语言中以便我们程序员组织我们的代码。

我认为这将是连接的问题,它只是无法找到这些功能并将它们映射到手动测试功能,但我不知道如何解决这个问题。您是否看到一些允许 connexion 映射函数而无需重构整个 API 以避免嵌套函数的东西?

非常感谢您的帮助。

最佳答案

我的猜测是您尚未定义处理程序函数。您需要提供与规范中的操作Id匹配的模块+函数。

例如:我在文件 app.py 中有一个名为 get_user() 的函数,我需要将 operationId 设置为 app.get_user。

operationId: app.get_user

希望有帮助!

关于python - Swagger-ui 连接未找到 Python 嵌套函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54043220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com