1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Complete autoswagger!

This commit is contained in:
Steffo 2020-03-09 02:10:41 +01:00
parent 7bb3db0145
commit 668df043f4
3 changed files with 18 additions and 4 deletions

View file

@ -6,5 +6,7 @@ from royalnet.version import semantic
class ApiDocsStar(ApiStar): class ApiDocsStar(ApiStar):
path = "/api/docs" path = "/api/docs"
summary = "Get the swagger.json file used to generate this documentation."
async def api(self, data: ApiData) -> ru.JSON: async def api(self, data: ApiData) -> ru.JSON:
return return

View file

@ -11,14 +11,21 @@ class DocsStar(PageStar):
path = "/docs" path = "/docs"
async def page(self, request: Request) -> Response: async def page(self, request: Request) -> Response:
paths = {}
for star in self.constellation.stars:
if not isinstance(star, ApiStar):
continue
paths[star.path] = star.swagger()
spec = json.dumps({ spec = json.dumps({
"swagger": "2.0", "swagger": "2.0",
"info": { "info": {
"description": "Autogenerated Royalnet API documentation", "description": "Autogenerated Royalnet API documentation",
"title": "Royalnet", "title": "Royalnet",
"version": f"{semantic}", "version": f"{semantic}",
"paths": [star.swagger() for star in self.constellation.stars if isinstance(star, ApiStar)] },
} "paths": paths
}) })
return HTMLResponse( return HTMLResponse(
@ -26,17 +33,21 @@ class DocsStar(PageStar):
<html lang="en"> <html lang="en">
<head> <head>
<title>Royalnet Docs</title> <title>Royalnet Docs</title>
<link rel="stylesheet" type="text/css" href="https://unpkg.com/swagger-ui-dist@3.12.1/swagger-ui.css">
<script src="https://unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"></script> <script src="https://unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
<script src="https://unpkg.com/swagger-ui-dist@3.12.1/swagger-ui-standalone-preset.js"></script>
</head> </head>
<body> <body>
<div id="docs"/> <div id="docs"/>
<script> <script>
const ui = SwaggerUIBundle({{ const ui = SwaggerUIBundle({{
spec: {spec} spec: JSON.parse('{spec}'),
dom_id: '#docs', dom_id: '#docs',
presets: [ presets: [
SwaggerUIBundle.presets.apis, SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
], ],
layout: "StandaloneLayout"
}}) }})
</script> </script>
</body> </body>

View file

@ -53,6 +53,7 @@ class ApiStar(PageStar, ABC):
result = {} result = {}
for method in cls.methods: for method in cls.methods:
result[method.lower()] = { result[method.lower()] = {
"operationId": cls.__name__,
"summary": cls.summary, "summary": cls.summary,
"description": cls.description, "description": cls.description,
"produces": ["application/json"], "produces": ["application/json"],
@ -64,7 +65,7 @@ class ApiStar(PageStar, ABC):
"500": {"description": "Serverside unhandled exception"}, "500": {"description": "Serverside unhandled exception"},
"501": {"description": "Not yet implemented"} "501": {"description": "Not yet implemented"}
}, },
"paameters": [{ "parameters": [{
"name": parameter, "name": parameter,
"in": "query", "in": "query",
"description": cls.parameters[parameter], "description": cls.parameters[parameter],