-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit_swagger.py
37 lines (32 loc) · 1.33 KB
/
init_swagger.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
import json
from apispec import APISpec
from apispec.exceptions import APISpecError
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.tornado import TornadoPlugin
def generate_swagger_file(handlers, file_location):
"""Automatically generates Swagger spec file based on RequestHandler
docstrings and saves it to the specified file_location.
"""
# Starting to generate Swagger spec file. All the relevant
# information can be found from here https://apispec.readthedocs.io/
spec = APISpec(
title="Car Brand API",
version="1.0.0",
openapi_version="3.0.2",
info=dict(description="Documentation for the Car Brand API"),
plugins=[TornadoPlugin(), MarshmallowPlugin()],
servers=[
{"url": "http://localhost:8888/", "description": "Local environment",},
],
)
# Looping through all the handlers and trying to register them.
# Handlers without docstring will raise errors. That's why we
# are catching them silently.
for handler in handlers:
try:
spec.path(urlspec=handler)
except APISpecError:
pass
# Write the Swagger file into specified location.
with open(file_location, "w", encoding="utf-8") as file:
json.dump(spec.to_dict(), file, ensure_ascii=False, indent=4)