Swagger with Flask-Restplus, API and multiple Blueprints Swagger with Flask-Restplus, API and multiple Blueprints flask flask

Swagger with Flask-Restplus, API and multiple Blueprints


There are 2 possible solutions using Flask-Restplus:

  • Use Flask-RestPlus namespaces
  • Turn your blueprints into Flask-RestPlus Apis

You can read about both in the documentation:https://flask-restplus.readthedocs.io/en/stable/scaling.html

Namespaces

Flask-RESTPlus provides a way to use almost the same pattern as Flask’s blueprint. The main idea is to split your app into reusable namespaces.

from flask_restplus import Apifrom .namespace1 import api as ns1from .namespace2 import api as ns2# ...from .namespaceX import api as nsXapi = Api(    title='My Title',    version='1.0',    description='A description',    # All API metadatas)api.add_namespace(ns1)api.add_namespace(ns2)# ...api.add_namespace(nsX)

Blueprint Apis

Here’s an example of how to link an Api up to a Blueprint.

from flask import Blueprintfrom flask_restplus import Apiblueprint = Blueprint('api', __name__)api = Api(blueprint)# ...

Using a blueprint will allow you to mount your API on any url prefix and/or subdomain in you application:

from flask import Flaskfrom apis import blueprint as apiapp = Flask(__name__)app.register_blueprint(api, url_prefix='/api/1')app.run(debug=True)