Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger with Flask-Restplus, API and multiple Blueprints

I'm building a very complex microservice using Flask and Flask-Restplus.
It will have many endpoints, thus I'm organizing each endpoint into a separate Blueprint.

  • Currently, I'm struggling with the usage of Flask-Restplus and API using multiple Blueprints in combination with swagger
  • I want to be able to get all the endpoints of my blueprints into the built-in swagger of API, but this doesn't seem to work.
  • I can access my endpoints via postman, but the swagger-UI doesn't show anything. :(

The following example code and directory structure should give you a hint towards my idea:

.
├── endpoints
│   ├── endpointa.py
│   ├── endpointb.py
│   ├── endpointc.py
│   └── __init__.py
├── __init__.py
└── run.py

My main init.py looks like this:

from flask import Flask, Blueprint, logging, jsonify, request, Response
from flask_restplus import Resource, Api   


# create app and api
app = Flask(__name__)
api_prefix  = '/api/v1/'

# register Blueprints
from endpoints.endpointa import endpointa_api
app.register_blueprint(endpointa_api, url_prefix=api_prefix)

from endpoints.endpointb import endpointb_api
app.register_blueprint(endpointb_api, url_prefix=api_prefix)

from endpoints.endpointc import endpointc_api
app.register_blueprint(endpointc_api, url_prefix=api_prefix)


api = Api(app,
          version='1',
          title='Test Service REST-API',
          description='A REST-API for the Test Service, implemented in python')


if __name__ == '__main__':
    app.run(debug=True, host="0.0.0.0", port=5060)

endpointa.py with the corresponding Blueprint:

from os import environ
import json, ast, syslog
import requests
import gc
from flask import Flask, Blueprint, logging, jsonify, request, Response
from flask_restplus import Resource, Api

endpointa_api = Blueprint('endpointa_api', __name__)

@endpointa_api.route('testa', methods=['GET'])
def testa():
    ...


@endpointa_api.route('testa/<string:testa_id>', methods=['GET', 'POST'])
def testa_id():
    ...

Again:

I can access my endpoints via postman, but the swagger-UI doesn't show anything:

enter image description here

Normally I would add endpoints to API using something like

api.add_resource(TestClass, api_prefix + 'test')

but this doesn't seem to be possible with multiple Blueprints.

Can anyone show me how to add/register these Blueprints (endpointa_api, endpointb_api and endpointc_api) with Api ?

like image 685
dacoda Avatar asked Feb 22 '19 13:02

dacoda


People also ask

What is namespace in flask RESTPlus?

namespace is from the Flask-RESTPlus and Blueprint is from flask to organize your app. the namespaces modules (specific to Flask-RESTPlus) are reusable namespaces designed like you would do with Flask's Blueprint.

What is the use of blueprint in flask?

Each Flask Blueprint is an object that works very similarly to a Flask application. They both can have resources, such as static files, templates, and views that are associated with routes. However, a Flask Blueprint is not actually an application. It needs to be registered in an application before you can run it.


1 Answers

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 Api

from .namespace1 import api as ns1
from .namespace2 import api as ns2
# ...
from .namespaceX import api as nsX

api = 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 Blueprint
from flask_restplus import Api

blueprint = 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 Flask
from apis import blueprint as api

app = Flask(__name__)
app.register_blueprint(api, url_prefix='/api/1')
app.run(debug=True)
like image 104
Robbe Avatar answered Sep 25 '22 20:09

Robbe