Skip to content

Commit c687606

Browse files
author
Vladimir Volkov
committed
fix: minor changes setup, utils
1 parent adba9a2 commit c687606

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

star_resty/apidocs/setup.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ def setup_spec(app: Starlette, title: str,
2020
openapi_version='2.0',
2121
schemes=None,
2222
base_path='/',
23-
route: str = '/apidocs',
23+
route: str = '/apidocs.json',
24+
route_html: str = '/apidocs',
2425
add_head_methods: bool = False,
25-
render_to_html: bool = True,
2626
options: Optional[Mapping] = None,
2727
**kwargs):
2828
if options is None:
@@ -40,17 +40,22 @@ def setup_spec(app: Starlette, title: str,
4040

4141
@app.route(route, include_in_schema=False)
4242
def generate_api_docs(_: Request):
43+
s = generate_spec(app, get_open_api_version(openapi_version), add_head_methods)
44+
return JSONResponse(s)
45+
46+
@app.route(route_html, include_in_schema=False)
47+
def generate_html_api_docs(_: Request):
48+
s = generate_spec(app, get_open_api_version(openapi_version), add_head_methods)
49+
return HTMLResponse(apispec_json_to_html(s))
50+
51+
def generate_spec(app: Starlette, open_api_version: int, add_head_methods: bool):
4352
nonlocal api_spec
4453
nonlocal spec
4554
if api_spec is None:
4655
logger.info('initialize open api schema')
47-
setup_routes(app.routes, spec, version=get_open_api_version(openapi_version)
48-
, add_head_methods=add_head_methods)
56+
setup_routes(app.routes, spec, version=open_api_version, add_head_methods=add_head_methods)
4957
api_spec = spec.to_dict()
50-
if render_to_html:
51-
return HTMLResponse(apispec_json_to_html(api_spec))
52-
else:
53-
return JSONResponse(api_spec)
58+
return api_spec
5459

5560

5661
def get_open_api_version(version: str) -> int:

star_resty/apidocs/template.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<title>Swagger UI</title>
77
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Source+Code+Pro:300,600|Titillium+Web:400,600,700" rel="stylesheet">
88
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.24.2/swagger-ui.css" >
9+
<link rel="shortcut icon" href="#">
910
<style>
1011
html
1112
{

star_resty/apidocs/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ def convert_path(path: str) -> str:
2323

2424
def apispec_json_to_html(apispec_json: dict) -> str:
2525
template_path = os.path.join(os.path.dirname(__file__), 'template.html')
26-
template = open(template_path, 'r').read()
26+
with open(template_path, 'r') as file:
27+
template = file.read()
2728
return template % json.dumps(apispec_json)

0 commit comments

Comments
 (0)