Connexion: Is a standalone Framework in top of Flask o can be implemented as a Flask Blueprint?

Created on 20 Mar 2016  路  4Comments  路  Source: zalando/connexion

Just my question.

Is there a sample with connexion implemented as a Blueprint for Flask.
Could be interesting to use existing server apps to server api too with connexion.

Thanks

Most helpful comment

I'm managing all my different endpoints (using different flask extensions) with blueprints so this is super helpful.

As far as this statement goes:

you can also use the App object from connexion as you would use a Flask App object

Make sure you use connexion.App().app for this. connexion.App() is not an instance of flask.Flask, but connexion.App().app is.

Sidenote: Would be nice to see something in the docs about this as well.

All 4 comments

You can instantiate an Api object and use the Api.blueprint to add the API blueprint to Flask. At least for simple applications you can also use the App object from connexion as you would use a Flask App object.

Thanks @jmcs Joao . Don`t know too much about Api.object. I'll try to doit.
Close the issue.
Thanks again

I'm managing all my different endpoints (using different flask extensions) with blueprints so this is super helpful.

As far as this statement goes:

you can also use the App object from connexion as you would use a Flask App object

Make sure you use connexion.App().app for this. connexion.App() is not an instance of flask.Flask, but connexion.App().app is.

Sidenote: Would be nice to see something in the docs about this as well.

"""Implement load api from swagger using template import yam."""
import yaml
import connexion
from jinja2 import FileSystemLoader
from jinja2.environment import Environment


def connexion_register_blueprint(app, swagger_file, **kwargs):
    con = connexion.FlaskApp("api", app.instance_path)
    env = Environment(loader=FileSystemLoader(
        app.config.get('SWAGGER_ROOT_TEMPLATE') or
        app.config['PROJECT_ROOT']
    ))
    swagger_string = env.get_template(swagger_file).render(**kwargs)
    specification = yaml.safe_load(swagger_string)
    api = super(connexion.FlaskApp, con).add_api(specification, **kwargs)
    app.register_blueprint(api.blueprint)
    return api

...
connexion_register_blueprint(app, 'api/v1/swagger/main.yaml')
...
Was this page helpful?
0 / 5 - 0 ratings