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.
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:
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 ?
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.
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.
There are 2 possible solutions using Flask-Restplus:
Api
sYou 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With