Skip to content

Commit

Permalink
v0.0.17
Browse files Browse the repository at this point in the history
v0.0.17 - apidocs html render
  • Loading branch information
wvolkov authored Apr 16, 2021
2 parents 43a1b6d + c687606 commit e98d24a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,4 @@ if __name__ == '__main__':
uvicorn.run(app, port=8080)
```

Open [http://localhost:8080/apidocs.json](http://localhost:8080/apidocs.json) to view generated openapi schema.
Open [http://localhost:8080/apidocs](http://localhost:8080/apidocs) to view generated openapi schema.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get_packages(package):
'apispec<4',
'python-multipart'
],
version='0.0.16',
version='0.0.17',
url='https://github.com/slv0/start_resty',
license='BSD',
description='The web framework',
Expand Down
20 changes: 14 additions & 6 deletions star_resty/apidocs/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from apispec.ext.marshmallow import MarshmallowPlugin
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.responses import JSONResponse, HTMLResponse

from .route import setup_routes
from .utils import resolve_schema_name
from .utils import resolve_schema_name, apispec_json_to_html

logger = logging.getLogger(__name__)

Expand All @@ -21,6 +21,7 @@ def setup_spec(app: Starlette, title: str,
schemes=None,
base_path='/',
route: str = '/apidocs.json',
route_html: str = '/apidocs',
add_head_methods: bool = False,
options: Optional[Mapping] = None,
**kwargs):
Expand All @@ -39,15 +40,22 @@ def setup_spec(app: Starlette, title: str,

@app.route(route, include_in_schema=False)
def generate_api_docs(_: Request):
s = generate_spec(app, get_open_api_version(openapi_version), add_head_methods)
return JSONResponse(s)

@app.route(route_html, include_in_schema=False)
def generate_html_api_docs(_: Request):
s = generate_spec(app, get_open_api_version(openapi_version), add_head_methods)
return HTMLResponse(apispec_json_to_html(s))

def generate_spec(app: Starlette, open_api_version: int, add_head_methods: bool):
nonlocal api_spec
nonlocal spec
if api_spec is None:
logger.info('initialize open api schema')
setup_routes(app.routes, spec, version=get_open_api_version(openapi_version)
, add_head_methods=add_head_methods)
setup_routes(app.routes, spec, version=open_api_version, add_head_methods=add_head_methods)
api_spec = spec.to_dict()

return JSONResponse(api_spec)
return api_spec


def get_open_api_version(version: str) -> int:
Expand Down
66 changes: 66 additions & 0 deletions star_resty/apidocs/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!--<script src="https://gist.github.com/oseiskar/dbd51a3727fc96dcf5ed189fca491fb3.js"></script>-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Source+Code+Pro:300,600|Titillium+Web:400,600,700" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.24.2/swagger-ui.css" >
<link rel="shortcut icon" href="#">
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*,
*:before,
*:after
{
box-sizing: inherit;
}

body {
margin:0;
background: #fafafa;
}
</style>
</head>
<body>

<div id="swagger-ui"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.24.2/swagger-ui-bundle.js"> </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.24.2/swagger-ui-standalone-preset.js"> </script>
<script>
window.onload = function() {

var spec = %s;

// Build a system
const ui = SwaggerUIBundle({
spec: spec,
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})

window.ui = ui
var elements = document.getElementsByClassName("download-url-wrapper");
while(elements.length > 0){
elements[0].parentNode.removeChild(elements[0]);
}

}
</script>
</body>

</html>
11 changes: 10 additions & 1 deletion star_resty/apidocs/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import inspect
import os
import re
import json
from typing import Any

__all__ = ('resolve_schema_name', 'convert_path')
__all__ = ('resolve_schema_name', 'convert_path', 'apispec_json_to_html')


def resolve_schema_name(schema: Any) -> str:
Expand All @@ -17,3 +19,10 @@ def resolve_schema_name(schema: Any) -> str:

def convert_path(path: str) -> str:
return re.sub(r'{([^:]+).*}', r'{\1}', path)


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

0 comments on commit e98d24a

Please sign in to comment.