2

我目前正在尝试使用 connexion 构建一个 api。但是,我通过 connexion 模块使用相对本地模块导入时遇到了一些问题,该模块修改了底层的烧瓶应用程序。这是我的文件结构的简化概述:

  • hall_of_fame_api
    • 控制器
      • ____初始化____.py
      • 路线.py
    • 模型
      • ____初始化____.py
      • 路线.py
    • ____初始化____.py
    • 配置文件
    • create_db.py
    • 招摇.yml

当我尝试在终端中运行“python config.py”时出现错误。这是config.py:

import os
import connexion
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow

basedir = os.path.abspath(os.path.dirname(__file__))

# Create the Connexion application instance
connex_app = connexion.App(__name__, specification_dir=basedir)

# Get the underlying Flask app instance
app = connex_app.app
connex_app.add_api('swagger.yml')

# Configure the SQLAlchemy part of the app instance
app.config['SQLALCHEMY_ECHO'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://doadmin:password@nba-player-db-do-user-7027314-0.db.ondigitalocean.com:25060/nba_test_1?sslmode=require'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

# Create the SQLAlchemy db instance
db = SQLAlchemy(app)

# Initialize Marshmallow
ma = Marshmallow(app)

这是它给出的错误:

Failed to add operation for GET /api/players
Failed to add operation for GET /api/players
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/connexion/apis/abstract.py", line 209, in add_paths
self.add_operation(path, method)
  File "/usr/local/lib/python3.7/site-packages/connexion/apis/abstract.py", line 173, in add_operation
    pass_context_arg_name=self.pass_context_arg_name
  File "/usr/local/lib/python3.7/site-packages/connexion/operations/__init__.py", line 8, in make_operation
    return spec.operation_cls.from_spec(spec, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/connexion/operations/swagger2.py", line 137, in from_spec
    **kwargs
  File "/usr/local/lib/python3.7/site-packages/connexion/operations/swagger2.py", line 96, in __init__
    pass_context_arg_name=pass_context_arg_name
  File "/usr/local/lib/python3.7/site-packages/connexion/operations/abstract.py", line 96, in __init__
    self._resolution = resolver.resolve(self)
  File "/usr/local/lib/python3.7/site-packages/connexion/resolver.py", line 40, in resolve
    return Resolution(self.resolve_function_from_operation_id(operation_id), operation_id)
  File "/usr/local/lib/python3.7/site-packages/connexion/resolver.py", line 66, in resolve_function_from_operation_id
    raise ResolverError(str(e), sys.exc_info())
connexion.exceptions.ResolverError: <ResolverError: module 'controller.routes' has no attribute 'read_all'>

此错误特别来自第 12 行,其中 connexion 正在尝试添加 swagger.yml 文件,这里也供参考:

swagger: "2.0"
info:
  description: This is the swagger file that goes with our server code
  version: "1.0.0"
  title: Swagger REST Article
consumes:
  - "application/json"
produces:
  - "application/json"

basePath: "/api"

# Paths supported by the server application
paths:
  /players:
    get:
      operationId: "controller.routes.read_all"
      tags:
        - "People"
      summary: "The people data structure supported by the server application"
      description: "Read the list of people"
      responses:
        200:
          description: "Successful read people list operation"
          schema:
            type: "array"
            items:
              properties:
                fname:
                  type: "string"
                lname:
                  type: "string"
                timestamp:
                  type: "string"

现在这是我感到困惑的地方,因为我的 routes.py 文件确实有一个定义为 read_all() 的函数,这是该文件:

from model.models import Regseason, RegSchema, Playoffs, PlayoffSchema

def read_all():

    return Regseason.query.all()

我已经为这个错误绞尽脑汁将近 24 小时,任何指导将不胜感激。提前致谢!

4

1 回答 1

1

请在 operationid 下方添加一个额外的字段 x-openapi-router-controller。Connexion 使用它来映射将请求发送到哪个模块。它与 OperationId 结合在一起以转到正确的模块和功能。

于 2020-06-02T00:23:17.187 回答