Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add section to docs showing how to use connexion as a Blueprint #512

Closed
jasonrhaas opened this issue Sep 14, 2017 · 7 comments
Closed

Add section to docs showing how to use connexion as a Blueprint #512

jasonrhaas opened this issue Sep 14, 2017 · 7 comments

Comments

@jasonrhaas
Copy link

jasonrhaas commented Sep 14, 2017

Description

It would be nice to add a section in the docs that describes how to use connexion using the api.blueprint like in #188. I think a lot of people use Flask Blueprint to manage their different endpoints, so this I think would increase adoption of the connexion framework.

Also #393 is another reference, with an example gist.

@jasonrhaas jasonrhaas changed the title Add section to docs that shows how to use connexion as a Blueprint Add section to docs showing how to use connexion as a Blueprint Sep 14, 2017
@jasonrhaas
Copy link
Author

Also, I am happy to submit a PR for this if the maintainers agree with this addition.

@hjacobs
Copy link
Contributor

hjacobs commented Sep 15, 2017

@jasonrhaas adding a section to the docs sounds awesome! 😄

@mvalkon
Copy link
Collaborator

mvalkon commented Sep 15, 2017

@jasonrhaas I don't see any reason why we would be against a PR of this nature so feel free to contribute, it's more than welcome. Just add a section of it's own so it's clear that it's Flask specific as we're trying to move Connexion to a more framework agnostic model (#380). Thanks!

@zerox1212
Copy link

It would be great to have an example that uses blueprints. For example a regular flask web app endpoint (/) along side an API endpoint (/api). This seems like a common requirement for a typical web + mobiles + api use case.

@sxslex
Copy link

sxslex commented Mar 3, 2019

"""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')
...

@varqasim
Copy link
Contributor

varqasim commented Jan 3, 2020

Correct me If I am wrong but by using connexion.FlaskApp.add_api already does this? It actually registers a blueprint. For example:

app.add_api('users.yaml', arguments={
                'api_local': 'local_value'}, validate_responses=True)

the add_api method from connexion.FlaskApp

def add_api(self, specification, **kwargs):
        api = super(FlaskApp, self).add_api(specification, **kwargs)
        self.app.register_blueprint(api.blueprint)
        return api

@ghost
Copy link

ghost commented Apr 7, 2021

I needed to use the after_request decorator on the connexion api blueprint. Thus, I had to access the connexion blueprint before the blueprint was registered in the app. Here's how I've done it, thanks to the comment above :

connexion_app = connexion.FlaskApp(__name__, specification_dir="./api/openapi/")
connexion_api = super(connexion.FlaskApp, connexion_app).add_api("openapi.yaml")

@connexion_api.blueprint.after_request
def after_request(response):
    # your logic
    return response

# Register the api blueprint
connexion_app.app.register_blueprint(connexion_api.blueprint)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants