From e9b6a7ec4fee258f47d2f557967afe3eaee5bf21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Justinas=20Rum=C5=A1evi=C4=8Dius?= Date: Fri, 24 Jun 2022 00:31:46 +0200 Subject: [PATCH] Add automatic API documentation (#49) --- frontend/public/api_documentation.html | 35 ++++++++++++++++++++++++++ local/controller.py | 3 +++ main.py | 24 ++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 frontend/public/api_documentation.html diff --git a/frontend/public/api_documentation.html b/frontend/public/api_documentation.html new file mode 100644 index 0000000..ea7a3a7 --- /dev/null +++ b/frontend/public/api_documentation.html @@ -0,0 +1,35 @@ + + + + + + + + + RelmonService API + + + + + + + diff --git a/local/controller.py b/local/controller.py index 74f8833..96382a2 100644 --- a/local/controller.py +++ b/local/controller.py @@ -119,6 +119,9 @@ def tick(self): ', '.join(r.get('id') for r in relmons_to_submit)) for relmon_json in relmons_to_submit: relmon = RelMon(relmon_json) + if 'NOSUBMIT' in relmon.get_name(): + continue + status = relmon.get_status() if status == 'new': # Double check and if it is new, submit it diff --git a/main.py b/main.py index 7b03d31..a592516 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ import configparser import os import time +import inspect from datetime import datetime from flask import Flask, render_template, request, make_response from flask_restful import Api @@ -249,6 +250,29 @@ def user_info(): return output_text(user_info_dict()) +@app.route('/api', defaults={'_path': ''}) +@app.route('/api/') +def api_documentation(_path): + """ + Endpoint for API documentation HTML + """ + docs = {} + base = os.path.dirname(os.path.realpath(__file__)) + for rule in app.url_map.iter_rules(): + endpoint = rule.rule + func = app.view_functions[rule.endpoint] + methods = sorted(list(rule.methods & {'GET', 'PUT', 'POST', 'DELETE'})) + if not methods or 'api' not in endpoint: + continue + + docs[endpoint] = {'doc': func.__doc__.strip(), + 'methods': methods, + 'file': inspect.getfile(func).replace(base, '').strip('/'), + 'line': inspect.getsourcelines(func)[1]} + + return render_template('api_documentation.html', docs=docs) + + def user_info_dict(): """ Get user name, login, email and authorized flag from request headers