Skip to content

Commit

Permalink
Add automatic API documentation (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinasr authored Jun 23, 2022
1 parent 4b3061d commit e9b6a7e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
35 changes: 35 additions & 0 deletions frontend/public/api_documentation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<base href="/relmonservice/">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="static/favicon.png">
<title>RelmonService API</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900">
<style>
body {
font-family: "Roboto", sans-serif;
color: rgba(0, 0, 0, 0.87);
font-size: 14px;
}
a {
color: rgb(25, 118, 210);
}
pre {
margin: 0;
}
</style>
</head>
<body>
<ul>
{% for (endpoint, endpoint_dict) in docs.items() %}
<li style="margin: 12px;">{{endpoint_dict.doc}}
(<a href="https://github.com/cms-PdmV/RelMonService2/blob/master/{{endpoint_dict.file}}#L{{endpoint_dict.line}}">source</a>)
<pre>[{{','.join(endpoint_dict.methods)}}] <b>{{endpoint}}</b></pre>
</li>
{% endfor %}
</ul>
</body>
</html>
3 changes: 3 additions & 0 deletions local/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -249,6 +250,29 @@ def user_info():
return output_text(user_info_dict())


@app.route('/api', defaults={'_path': ''})
@app.route('/api/<path:_path>')
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
Expand Down

0 comments on commit e9b6a7e

Please sign in to comment.