@@ -20,9 +20,9 @@ def setup_spec(app: Starlette, title: str,
20
20
openapi_version = '2.0' ,
21
21
schemes = None ,
22
22
base_path = '/' ,
23
- route : str = '/apidocs' ,
23
+ route : str = '/apidocs.json' ,
24
+ route_html : str = '/apidocs' ,
24
25
add_head_methods : bool = False ,
25
- render_to_html : bool = True ,
26
26
options : Optional [Mapping ] = None ,
27
27
** kwargs ):
28
28
if options is None :
@@ -40,17 +40,22 @@ def setup_spec(app: Starlette, title: str,
40
40
41
41
@app .route (route , include_in_schema = False )
42
42
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 ):
43
52
nonlocal api_spec
44
53
nonlocal spec
45
54
if api_spec is None :
46
55
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 )
49
57
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
54
59
55
60
56
61
def get_open_api_version (version : str ) -> int :
0 commit comments