add HTML response for api endpoint (#98) (#101)

This commit is contained in:
Tom Kralidis
2019-04-29 07:32:12 -04:00
committed by GitHub
parent 946ab0da3d
commit c4597f563b
4 changed files with 41 additions and 3 deletions
+11 -1
View File
@@ -131,7 +131,7 @@ class API(object):
return headers_, 200, json.dumps(fcm)
def api(self, headers, args, openapi):
def api(self, headers, args, request_path, openapi):
"""
Provide OpenAPI document
@@ -143,6 +143,16 @@ class API(object):
"""
headers_ = HEADERS.copy()
format_ = check_format(args, headers)
if format_ == 'html':
data = {
'openapi-document-path': request_path
}
headers_['Content-Type'] = 'text/html'
content = _render_j2_template(self.config, 'api.html', data)
return headers_, 200, content
headers_['Content-Type'] = 'application/openapi+json;version=3.0'
return headers_, 200, json.dumps(openapi)
+1 -1
View File
@@ -68,7 +68,7 @@ def api():
openapi = yaml.load(ff)
headers, status_code, content = api_.api(request.headers, request.args,
openapi)
request.path, openapi)
response = make_response(content, status_code)
if headers:
+22
View File
@@ -0,0 +1,22 @@
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3.17.0/swagger-ui.css">
<script src="https://unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
<script>
function render() {
var ui = SwaggerUIBundle({
url: '{{ data['openapi-document-path'] }}',
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
]
});
}
</script>
</head>
<body onload="render()">
<div id="swagger-ui"></div>
</body>
</html>
+7 -1
View File
@@ -73,12 +73,18 @@ def test_api(config, api_, openapi, headers):
assert api_.config == config
assert isinstance(api_.config, dict)
headers_, code, response = api_.api(headers, {}, openapi)
headers['Content-Type'] = 'application/json'
headers_, code, response = api_.api(headers, {}, '/api', openapi)
assert headers_['Content-Type'] == 'application/openapi+json;version=3.0'
root = json.loads(response)
assert isinstance(root, dict)
a = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
headers['Accept'] = a
headers_, code, response = api_.api(headers, {}, '/api', openapi)
assert headers_['Content-Type'] == 'text/html'
def test_api_exception(config, api_, headers):
headers_, code, response = api_.root(headers, {'f': 'foo'})