Hi everyone, I am new to python. I come from a .NET Core background and have been wanting to implement swagger pages on the python application that I have started working on for a new company.
It is a huge api and it’s written using only Flask and Flask Cors
The api has the one main init.py file and is written as follows
def api_prefix(app):
return f'/api/{app.config.get("API_VERSION", "v1")}'
def create_app() -> Flask:
app = Flask(__name__, instance_relative_config=True)
load_app_config(app)
CORS(app, resources={'*': {'origins': app.config['CORS_ORIGINS']}})
contentful_client.init_app(app)
@app.route('/healthpoke')
def healthpoke():
return 'OUCH!', 200
def app_exception_handler(error):
return handle_app_exception(error, request.path)
def internal_server_error_handler(error):
return handle_internal_server_error(error, request.path)
app.register_error_handler(AppException, app_exception_handler)
app.register_error_handler(500, internal_server_error_handler)
apis = [
test_api,
test2_api,
test3_api
]
for api in apis:
app.register_blueprint(api, url_prefix=api_prefix(app))
# register root/standalone blueprints (self versioned)
app.register_blueprint(demo_api)
app.register_blueprint(demo2_api)
app.register_blueprint(demo3_api)
return app
app = create_app()
And for those apis, for example, we have it structured like this
For app.register_blueprint(demo_api), it is in its own folder → app.demo.routes.init.py
and in that file it is like this
from flask import Blueprint
bulk_actions_api = Blueprint('demo', __name__, url_prefix='/bulk_actions')
from . import demo_routes
from . import demoSomethingElse_routes
And then in demo_routes.py, it goes like this
@demo_api.route('/v1/<int:demo_id>', methods=['GET'])
@auth_required
def demo_check(demo_id):
page = int(request.args.get('page', 1))
success, payload = check_demo(demo_id, page) # this comes from the business logic
if success is not True:
if isinstance(payload, str):
raise AppException.bad_request(message=payload)
else:
raise AppException.bad_request(payload=payload)
return jsonify(payload)
Is it possible to add swagger pages to our application using resplus or will that require an entire redesign of our system?
It looks like I will have to start creating a lot of these as classes, etc…