noirbizarre / flask-restplus

Fully featured framework for fast, easy and documented API development with Flask
http://flask-restplus.readthedocs.org
Other
2.74k stars 506 forks source link

How to use swagger with a old flask project. #808

Open engFelipeMonteiro opened 3 years ago

engFelipeMonteiro commented 3 years ago

I have system that is build with two API one is mine and another is third-part maintaned. How can I add swagger without modify the source of the third-part software.

I build a POC in the source below: PoC

simple_page.py

# Pure flask page
from flask import Blueprint, render_template, abort
from jinja2 import TemplateNotFound

simple_page = Blueprint('simple_page', __name__,
                        template_folder='templates')

@simple_page.route('/', defaults={'page': 'index'})
@simple_page.route('/<page>')
def show(page):
    try:
        return render_template('%s.html' % page)
    except TemplateNotFound:
        abort(418)

other_page.py

#restplus based artecture
from flask import Blueprint, render_template, abort, Flask
from jinja2 import TemplateNotFound
app = Flask(__name__)

other_page = Blueprint('other_page', __name__)

from flask_restplus import Api, Resource, fields

api = Api(other_page, version='1.0', title='Todo API',
    description='A simple TODO API', doc='/doc/', template_folder='templates',
)

@api.route('/')
class TodoSimple(Resource):
    def get(self):
        try:
            return render_template('other_page.html')
        except TemplateNotFound:
            abort(418)

simple.py

from flask import Flask
from simple_page import simple_page
from other_page import other_page
from flask_restplus import Api

app = Flask(__name__)
api = Api(simple_page, doc='/doc/')

app.register_blueprint(simple_page)
#app.register_blueprint(simple_page, url_prefix='/simple_page')
app.register_blueprint(other_page, url_prefix='/other_page')
app.run()

with this I get the doc page but a message "No operations defined in spec!". Screen Shot 2021-07-02 at 6 35 50 PM

And the rest_plus page I get the documentation normal.