Commit a7a3b178 authored by Goran Pavlovic's avatar Goran Pavlovic
Browse files

Merge branch 'Alexei-Kornienko-alex/swagger-ui3'

parents a749d4b5 42698900
include LICENSE CHANGELOG README.md requirements.txt requirements-performance.txt
recursive-include aiohttp_swagger/templates *
recursive-include aiohttp_swagger/swagger_ui *
recursive-include aiohttp_swagger/swagger_ui3 *
recursive-exclude * __pycache__
recursive-exclude * *.pyc
......
......@@ -30,9 +30,6 @@ async def _swagger_def(request):
return web.json_response(text=request.app["SWAGGER_DEF_CONTENT"])
STATIC_PATH = abspath(join(dirname(__file__), "swagger_ui"))
def setup_swagger(app: web.Application,
*,
swagger_from_file: str = None,
......@@ -41,6 +38,7 @@ def setup_swagger(app: web.Application,
swagger_validator_url: str = "",
description: str = "Swagger API definition",
api_version: str = "1.0.0",
ui_version: int = None,
title: str = "Swagger API",
contact: str = "",
swagger_home_decor: FunctionType = None,
......@@ -55,6 +53,11 @@ def setup_swagger(app: web.Application,
_base_swagger_url = _swagger_url.rstrip('/')
_swagger_def_url = '{}/swagger.json'.format(_base_swagger_url)
if ui_version == 3:
STATIC_PATH = abspath(join(dirname(__file__), "swagger_ui3"))
else:
STATIC_PATH = abspath(join(dirname(__file__), "swagger_ui"))
# Build Swagget Info
if swagger_info is None:
if swagger_from_file:
......
<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="##STATIC_PATH##/swagger-ui.css" >
<link rel="icon" type="image/png" href="##STATIC_PATH##/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="##STATIC_PATH##/favicon-16x16.png" sizes="16x16" />
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*,
*:before,
*:after
{
box-sizing: inherit;
}
body
{
margin:0;
background: #fafafa;
}
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="##STATIC_PATH##/swagger-ui-bundle.js"> </script>
<script src="##STATIC_PATH##/swagger-ui-standalone-preset.js"> </script>
<script>
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "##SWAGGER_CONFIG##",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
// End Swagger UI call region
window.ui = ui
}
</script>
</body>
</html>
<!doctype html>
<html lang="en-US">
<body onload="run()">
</body>
</html>
<script>
'use strict';
function run () {
var oauth2 = window.opener.swaggerUIRedirectOauth2;
var sentState = oauth2.state;
var redirectUrl = oauth2.redirectUrl;
var isValid, qp, arr;
if (/code|token|error/.test(window.location.hash)) {
qp = window.location.hash.substring(1);
} else {
qp = location.search.substring(1);
}
arr = qp.split("&")
arr.forEach(function (v,i,_arr) { _arr[i] = '"' + v.replace('=', '":"') + '"';})
qp = qp ? JSON.parse('{' + arr.join() + '}',
function (key, value) {
return key === "" ? value : decodeURIComponent(value)
}
) : {}
isValid = qp.state === sentState
if ((
oauth2.auth.schema.get("flow") === "accessCode"||
oauth2.auth.schema.get("flow") === "authorizationCode"
) && !oauth2.auth.code) {
if (!isValid) {
oauth2.errCb({
authId: oauth2.auth.name,
source: "auth",
level: "warning",
message: "Authorization may be unsafe, passed state was changed in server Passed state wasn't returned from auth server"
});
}
if (qp.code) {
delete oauth2.state;
oauth2.auth.code = qp.code;
oauth2.callback({auth: oauth2.auth, redirectUrl: redirectUrl});
} else {
let oauthErrorMsg
if (qp.error) {
oauthErrorMsg = "["+qp.error+"]: " +
(qp.error_description ? qp.error_description+ ". " : "no accessCode received from the server. ") +
(qp.error_uri ? "More info: "+qp.error_uri : "");
}
oauth2.errCb({
authId: oauth2.auth.name,
source: "auth",
level: "error",
message: oauthErrorMsg || "[Authorization failed]: no accessCode received from the server"
});
}
} else {
oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid, redirectUrl: redirectUrl});
}
window.close();
}
</script>
This diff is collapsed.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -42,7 +42,9 @@ Where to access to API Doc
By default, API will be generated at URL: :samp:`yourdomain.com/api/doc`.
You can modify the URI adding the parameter :samp:`swagger_url` in :samp:`setup_swagger`:
You can modify the URI adding the parameter :samp:`swagger_url` in :samp:`setup_swagger`.
You can specify UI version (Version 2 and 3 are supported) by adding the parameter :samp:`ui_version` in :samp:`ui_version`.
.. code-block:: python
......@@ -69,6 +71,6 @@ You can modify the URI adding the parameter :samp:`swagger_url` in :samp:`setup_
app = web.Application()
app.router.add_route('GET', "/ping", ping)
setup_swagger(app, swagger_url="/api/v1/doc") # <-- NEW Doc URI
setup_swagger(app, swagger_url="/api/v1/doc", ui_version=2) # <-- NEW Doc URI
web.run_app(app, host="127.0.0.1")
\ No newline at end of file
......@@ -8,6 +8,7 @@ from aiohttp import web
from aiohttp_swagger import *
async def ping(request):
"""
---
......@@ -104,6 +105,45 @@ async def test_ping(test_client, loop):
assert 'pong' in text
async def test_swagger_ui(test_client, loop):
TESTS_PATH = abspath(join(dirname(__file__)))
app = web.Application(loop=loop)
setup_swagger(app,
swagger_from_file=TESTS_PATH + "/data/example_swagger.yaml")
client = await test_client(app)
resp1 = await client.get('/api/doc')
assert resp1.status == 200
retrieved = await resp1.text()
loaded = open(join(TESTS_PATH, "..", "aiohttp_swagger/swagger_ui/index.html")).read()
loaded = loaded.replace("##STATIC_PATH##", "/api/doc/swagger_static")
loaded = loaded.replace("##SWAGGER_CONFIG##", "/api/doc/swagger.json")
loaded = loaded.replace("##SWAGGER_VALIDATOR_URL##", '')
assert retrieved == loaded
async def test_swagger_ui3(test_client, loop):
TESTS_PATH = abspath(join(dirname(__file__)))
app = web.Application(loop=loop)
setup_swagger(app,
swagger_from_file=TESTS_PATH + "/data/example_swagger.yaml",
ui_version=3
)
client = await test_client(app)
resp1 = await client.get('/api/doc')
assert resp1.status == 200
retrieved = await resp1.text()
loaded = open(join(TESTS_PATH, "..", "aiohttp_swagger/swagger_ui3/index.html")).read()
loaded = loaded.replace("##STATIC_PATH##", "/api/doc/swagger_static")
loaded = loaded.replace("##SWAGGER_CONFIG##", "/api/doc/swagger.json")
loaded = loaded.replace("##SWAGGER_VALIDATOR_URL##", '')
assert retrieved == loaded
async def test_swagger_file_url(test_client, loop):
TESTS_PATH = abspath(join(dirname(__file__)))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment