forked from cr0hn/aiohttp-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
112 lines (93 loc) · 3.88 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import asyncio
from os.path import abspath, dirname, join
from types import FunctionType
from aiohttp import web
from .helpers import (generate_doc_from_each_end_point,
load_doc_from_yaml_file, swagger_path)
try:
import ujson as json
except ImportError:
import json
async def _swagger_home(request):
"""
Return the index.html main file
"""
return web.Response(
text=request.app["SWAGGER_TEMPLATE_CONTENT"],
content_type="text/html"
)
async def _swagger_def(request):
"""
Returns the Swagger JSON Definition
"""
return web.json_response(text=request.app["SWAGGER_DEF_CONTENT"])
def setup_swagger(app: web.Application,
*,
swagger_from_file: str = None,
swagger_url: str = "/api/doc",
api_base_url: str = "/",
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,
swagger_def_decor: FunctionType = None,
swagger_info: dict = None,
swagger_template_path: str = None,
definitions: dict = None,
security_definitions: dict = 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"))
else:
STATIC_PATH = abspath(join(dirname(__file__), "swagger_ui"))
# Build Swagget Info
if swagger_info is None:
if swagger_from_file:
swagger_info = load_doc_from_yaml_file(swagger_from_file)
else:
swagger_info = generate_doc_from_each_end_point(
app, ui_version=ui_version,
api_base_url=api_base_url, description=description,
api_version=api_version, title=title, contact=contact,
template_path=swagger_template_path,
definitions=definitions,
security_definitions=security_definitions
)
else:
swagger_info = json.dumps(swagger_info)
_swagger_home_func = _swagger_home
_swagger_def_func = _swagger_def
if swagger_home_decor is not None:
_swagger_home_func = swagger_home_decor(_swagger_home)
if swagger_def_decor is not None:
_swagger_def_func = swagger_def_decor(_swagger_def)
# Add API routes
app.router.add_route('GET', _swagger_url, _swagger_home_func)
if _base_swagger_url:
app.router.add_route('GET', "{}/".format(_base_swagger_url),
_swagger_home_func)
app.router.add_route('GET', _swagger_def_url, _swagger_def_func)
# Set statics
statics_path = '{}/swagger_static'.format(_base_swagger_url)
app.router.add_static(statics_path, STATIC_PATH)
# --------------------------------------------------------------------------
# Build templates
# --------------------------------------------------------------------------
app["SWAGGER_DEF_CONTENT"] = swagger_info
with open(join(STATIC_PATH, "index.html"), "r") as f:
app["SWAGGER_TEMPLATE_CONTENT"] = (
f.read()
.replace("##SWAGGER_CONFIG##", '{}{}'.
format(api_base_url.rstrip('/'), _swagger_def_url))
.replace("##STATIC_PATH##", '{}{}'.
format(api_base_url.rstrip('/'), statics_path))
.replace("##SWAGGER_VALIDATOR_URL##", swagger_validator_url)
)
__all__ = ("setup_swagger", "swagger_path")