0

I use the generated openapi python flask server (generated by openapi-generator-cli-4.2.3.jar) in a larger python project. One of the openapi controllers needs access to my database. Another controller needs a list of objects. Both information are already in use by the python project.

My Idea is to pass all needed information as variables into the main method (in __main__.py). But how can I pass the variables to the controller or to a function in it? Are there other (better) ways?

openapi main method:

def main(): # add: db_connection:dict, my_object:list
    app = connexion.App(__name__, specification_dir='./openapi/')
    app.app.json_encoder = encoder.JSONEncoder
    app.add_api('openapi.yaml',
                arguments={'title': 'My API'},
                pythonic_params=True)
    app.run(port=8080)

Thank you for your help!

I found the following question on github, but no practical solution: https://github.com/zalando/connexion/issues/516

1 Answers1

0

You should instanciate your variables outside de main function:

db = SQLAlchemy()
ma = Marshmallow()
logger = factory_logger()

def init_api():
    app = connexion.App(__name__, specification_dir="./swagger/")
    app.add_api("swagger.yaml", arguments={"title": "files"})
    app.app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
    db.init_app(app.app)
    ma.init_app(app.app)
    app.app.before_request(before_request)
    return app

And import this variables where you and to use :

from my_server import db

or

from my_server import logger
Kevin Martins
  • 590
  • 7
  • 20