From adee1465122865a44a89c9024fe47e118f692efa Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Fri, 7 Jun 2024 13:10:45 +0200 Subject: [PATCH] Add swaagger UI --- IM/REST.py | 13 +++++++++++++ pyproject.toml | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/IM/REST.py b/IM/REST.py index 572f3c55..ff57d8ca 100644 --- a/IM/REST.py +++ b/IM/REST.py @@ -22,6 +22,7 @@ import os import yaml +from flask_swagger_ui import get_swaggerui_blueprint from cheroot.wsgi import Server as WSGIServer, PathInfoDispatcher from cheroot.ssl.builtin import BuiltinSSLAdapter from werkzeug.middleware.proxy_fix import ProxyFix @@ -62,6 +63,13 @@ app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_port=1, x_prefix=1) flask_server = None +# Add the swagger UI +swaggerui_blueprint = get_swaggerui_blueprint( + '/api/docs', # Swagger UI static files will be mapped to '{SWAGGER_URL}/dist/' + '/swagger.json' +) +app.register_blueprint(swaggerui_blueprint) + def run_in_thread(host, port): flask_thr = threading.Thread(target=run, args=(host, port)) @@ -943,6 +951,11 @@ def RESTGetVersion(): @app.route('/') def RESTIndex(): + return flask.redirect('/api/docs') + + +@app.route('/swagger.json') +def RESTSwagger(): rest_path = os.path.dirname(os.path.abspath(__file__)) abs_file_path = os.path.join(rest_path, 'swagger_api.yaml') api_docs = yaml.safe_load(open(abs_file_path, 'r')) diff --git a/pyproject.toml b/pyproject.toml index fd6487a1..064e3e0d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,7 +64,8 @@ dependencies = [ "requests-cache >= 1.0.0", "packaging", "werkzeug", - "xmltodict" + "xmltodict", + "flask-swagger-ui" ] license = {text = "GPL version 3, http://www.gnu.org/licenses/gpl-3.0.txt"} dynamic = ["version", "readme"]