I recently started writing REST API, I wanted to have a proper structure of project and an approach which is more readable and make more sense interms of organising in a logical order. I took advantage of namespace and I have put endpoints of each name space into separate python package.

| |______init__.py
| |____base
| | |______init__.py
| | |____resources.py
| |____hello
| | |______init__.py
| | |____resources.py
| |____routes.py


from flask import Flask 

def register_blueprints(app):
    # Since the application instance is now created, register each Blueprint
    # with the Flask application instance (app)
    from project.api import api_blueprint
    from project.api import base_api_blueprint

def create_app(config_filename=None):
    app = Flask(__name__, instance_relative_config=True)
    return app


from flask import Blueprint

api_blueprint = Blueprint('api', __name__, template_folder='templates')
base_api_blueprint = Blueprint('base_api', __name__)

from . import routes
from .hello import resources
from .base import resources


from flask_restplus import Resource

from . import api_blueprint, base_api_blueprint

base_api = Api(base_api_blueprint, catch_all_404s=True, prefix='/api', version='1.0', title='Sample API',
               description='A sample API',


base_api_ns = base_api.namespace('todos', description='base api namespace.')
hello_api_ns = base_api.namespace('hello', description='Hello World api.')


from flask_restplus import Resource

from ..routes import base_api_ns

@base_api_ns.route('/base', endpoint='base-endpoint')
class BaseResource(Resource):
    def get(self):
        return {"from": "base"}


from flask_restplus import Resource

from ..routes import hello_api_ns

@hello_api_ns.route('/world', endpoint='world-endpoint')
class BaseResource(Resource):
    def get(self):
        return {"hello": "world"}

the rest api web app, has api that deals with two different type of tasks base and hello so I have segregated to a separate package and used different name space for each.

having a second API object of api_blueprint would rather make more sense but that seems not work maybe due to a bug in flask restplus. the swagger finally renders up like this: enter image description here Any input in implementation or improvement will be highly appreciated.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.