From 06a9aeeba5281dbb459e18824f829b8148de6526 Mon Sep 17 00:00:00 2001 From: jtc42 Date: Thu, 18 Apr 2019 12:41:32 +0100 Subject: [PATCH] Added route to list all routes --- openflexure_microscope/api/app.py | 30 +++++++++++++++---------- openflexure_microscope/api/utilities.py | 21 ++++++++++++++--- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/openflexure_microscope/api/app.py b/openflexure_microscope/api/app.py index e7bddaa3..9d8574c5 100644 --- a/openflexure_microscope/api/app.py +++ b/openflexure_microscope/api/app.py @@ -4,7 +4,7 @@ TODO: Implement API route to cleanly shut down server """ from flask import ( - Flask, render_template) + Flask, render_template, jsonify) from flask.logging import default_handler from serial import SerialException @@ -12,6 +12,7 @@ from serial import SerialException from flask_cors import CORS from openflexure_microscope.api.exceptions import JSONExceptionHandler +from openflexure_microscope.api.utilities import list_routes from openflexure_microscope import Microscope from openflexure_microscope.camera.pi import StreamingCamera @@ -89,15 +90,6 @@ def attach_microscope(): ##### WEBAPP ROUTES ###### -@app.route('/') -def index(): - """ - API demo app - """ - return render_template( - 'index_v1.html' - ) - ##### API ROUTES ###### from openflexure_microscope.api.v1 import blueprints @@ -121,6 +113,22 @@ app.register_blueprint(plugin_blueprint, url_prefix=uri('/plugin', 'v1')) task_blueprint = blueprints.task.construct_blueprint(api_microscope) app.register_blueprint(task_blueprint, url_prefix=uri('/task', 'v1')) +@app.route('/') +def index(): + """ + API demo app + """ + return render_template( + 'index_v1.html' + ) + +@app.route('/routes') +def routes(): + """ + List of all connected API routes + """ + return jsonify(list_routes(app)) + # Automatically clean up microscope at exit def cleanup(): global api_microscope @@ -138,9 +146,7 @@ def cleanup(): api_microscope.close() logging.debug("App teardown complete.") - atexit.register(cleanup) - if __name__ == "__main__": app.run(host='0.0.0.0', port="5000", threaded=True, debug=True, use_reloader=False) \ No newline at end of file diff --git a/openflexure_microscope/api/utilities.py b/openflexure_microscope/api/utilities.py index 3a96ede9..27dbeb7b 100644 --- a/openflexure_microscope/api/utilities.py +++ b/openflexure_microscope/api/utilities.py @@ -1,7 +1,7 @@ import pprint import logging from werkzeug.exceptions import BadRequest - +from flask import url_for class JsonPayload: def __init__(self, request): @@ -69,5 +69,20 @@ def get_bool(get_arg): def list_routes(app): - """Print available functions.""" - pprint.pprint(list(map(lambda x: repr(x), app.url_map.iter_rules()))) + output = {} + for rule in app.url_map.iter_rules(): + + options = {} + for arg in rule.arguments: + options[arg] = "[{0}]".format(arg) + + endpoint = rule.endpoint + methods = list(rule.methods) + url = url_for(rule.endpoint, **options) + line = { + 'endpoint': endpoint, + 'methods': methods + } + output[url] = line + + return output \ No newline at end of file