Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom theme support and an example #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,8 @@ ENV/

# Pycharm
.idea/

# Pipenv files
Pipfile
Pipfile.lock
pyproject.toml
13 changes: 9 additions & 4 deletions aiohttp_swagger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,22 @@ def setup_swagger(app: web.Application,
swagger_info: dict = None,
swagger_template_path: str = None,
definitions: dict = None,
security_definitions: dict = None):
security_definitions: dict = None,
custom_theme_dir = None
):
_swagger_url = ("/{}".format(swagger_url)
if not swagger_url.startswith("/")
else swagger_url)
_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"))
if custom_theme_dir is None:
if ui_version == 3:
STATIC_PATH = abspath(join(dirname(__file__), "swagger_ui3"))
else:
STATIC_PATH = abspath(join(dirname(__file__), "swagger_ui"))
else:
STATIC_PATH = abspath(join(dirname(__file__), "swagger_ui"))
STATIC_PATH = abspath(custom_theme_dir)

# Build Swagget Info
if swagger_info is None:
Expand Down
38 changes: 38 additions & 0 deletions examples/example_06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from os.path import join, dirname

from aiohttp import web

if __name__ == '__main__':
from aiohttp_swagger import *

async def ping(request):
"""
This is my usually Sphinx doc

>>> import json
>>> ping(None)

:param request: Context injected by aiohttp framework
:type request: RequestHandler

---
description: This end-point allow to test that service is up.
tags:
- Health check
produces:
- text/plain
responses:
"200":
description: successful operation. Return "pong" text
"405":
description: invalid HTTP Method
"""
return web.Response(text="pong")

app = web.Application()

app.router.add_route('GET', "/ping", ping)

setup_swagger(app, custom_theme_dir="swagger_custom")

web.run_app(app, host="127.0.0.1")
Binary file added examples/swagger_custom/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/swagger_custom/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions examples/swagger_custom/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!-- 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
],
presets_config: {
SwaggerUIStandalonePreset: {
TopbarPlugin: false
}
}
})
// End Swagger UI call region

window.ui = ui
}
</script>
</body>
</html>
68 changes: 68 additions & 0 deletions examples/swagger_custom/oauth2-redirect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!doctype html>
<html lang="en-US">
<title>Swagger UI: OAuth2 Redirect</title>
<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>
134 changes: 134 additions & 0 deletions examples/swagger_custom/swagger-ui-bundle.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/swagger_custom/swagger-ui-bundle.js.map

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions examples/swagger_custom/swagger-ui-standalone-preset.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions examples/swagger_custom/swagger-ui.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/swagger_custom/swagger-ui.css.map

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions examples/swagger_custom/swagger-ui.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/swagger_custom/swagger-ui.js.map

Large diffs are not rendered by default.