/
blueprint.py
167 lines (131 loc) · 5.58 KB
/
blueprint.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import inspect
from functools import partial
from os.path import abspath, dirname, realpath
from sanic.blueprints import Blueprint
from sanic.config import Config
from sanic.response import html, json
from sanic_ext.extensions.openapi.builders import (
OperationStore,
SpecificationBuilder,
)
from ...utils.route import (
clean_route_name,
get_all_routes,
get_blueprinted_routes,
)
def blueprint_factory(config: Config):
bp = Blueprint("openapi", url_prefix=config.OAS_URL_PREFIX)
dir_path = dirname(realpath(__file__))
dir_path = abspath(dir_path + "/ui")
for ui in ("redoc", "swagger"):
if getattr(config, f"OAS_UI_{ui}".upper()):
path = getattr(config, f"OAS_PATH_TO_{ui}_HTML".upper())
uri = getattr(config, f"OAS_URI_TO_{ui}".upper())
version = getattr(config, f"OAS_UI_{ui}_VERSION".upper(), "")
html_path = path if path else f"{dir_path}/{ui}.html"
with open(html_path, "r") as f:
page = f.read()
def index(request, page):
return html(
page.replace("__VERSION__", version).replace(
"__URL_PREFIX__", getattr(config, "OAS_URL_PREFIX")
)
)
bp.add_route(partial(index, page=page), uri, name=ui)
if config.OAS_UI_DEFAULT and config.OAS_UI_DEFAULT == ui:
bp.add_route(partial(index, page=page), "", name="index")
@bp.get(config.OAS_URI_TO_JSON)
def spec(request):
return json(SpecificationBuilder().build().serialize())
if config.OAS_UI_SWAGGER:
@bp.get(config.OAS_URI_TO_CONFIG)
def openapi_config(request):
return json(request.app.config.SWAGGER_UI_CONFIGURATION)
@bp.before_server_start
def build_spec(app, loop):
specification = SpecificationBuilder()
# --------------------------------------------------------------- #
# Blueprint Tags
# --------------------------------------------------------------- #
for blueprint_name, handler in get_blueprinted_routes(app):
operation = OperationStore()[handler]
if not operation.tags:
operation.tag(blueprint_name)
# --------------------------------------------------------------- #
# Operations
# --------------------------------------------------------------- #
for (
uri,
route_name,
route_parameters,
method_handlers,
) in get_all_routes(app, bp.url_prefix):
# --------------------------------------------------------------- #
# Methods
# --------------------------------------------------------------- #
uri = uri if uri == "/" else uri.rstrip("/")
for method, _handler in method_handlers:
if (
(method == "OPTIONS" and app.config.OAS_IGNORE_OPTIONS)
or (method == "HEAD" and app.config.OAS_IGNORE_HEAD)
or method == "TRACE"
):
continue
if hasattr(_handler, "view_class"):
_handler = getattr(_handler.view_class, method.lower())
operation = OperationStore()[_handler]
if operation._exclude or "openapi" in operation.tags:
continue
docstring = inspect.getdoc(_handler)
if (
docstring
and app.config.OAS_AUTODOC
and operation._allow_autodoc
):
operation.autodoc(docstring)
operation._default[
"operationId"
] = f"{method.lower()}_route_name"
operation._default["summary"] = clean_route_name(route_name)
# TODO: solve for this
# for _parameter in route_parameters:
# if any(
# (
# param.fields["name"] == _parameter.name
# for param in operation.parameters
# )
# ):
# continue
# operation.parameter(
# _parameter.name, _parameter.cast, "path"
# )
specification.operation(uri, method, operation)
add_static_info_to_spec_from_config(app, specification)
return bp
def add_static_info_to_spec_from_config(app, specification):
"""
Reads app.config and sets attributes to specification according to the
desired values.
Modifies specification in-place and returns None
"""
specification._do_describe(
getattr(app.config, "API_TITLE", "API"),
getattr(app.config, "API_VERSION", "1.0.0"),
getattr(app.config, "API_DESCRIPTION", None),
getattr(app.config, "API_TERMS_OF_SERVICE", None),
)
specification._do_license(
getattr(app.config, "API_LICENSE_NAME", None),
getattr(app.config, "API_LICENSE_URL", None),
)
specification._do_contact(
getattr(app.config, "API_CONTACT_NAME", None),
getattr(app.config, "API_CONTACT_URL", None),
getattr(app.config, "API_CONTACT_EMAIL", None),
)
for scheme in getattr(app.config, "API_SCHEMES", ["http"]):
host = getattr(app.config, "API_HOST", None)
basePath = getattr(app.config, "API_BASEPATH", "")
if host is None or basePath is None:
continue
specification.url(f"{scheme}://{host}/{basePath}")