From 668df043f4882f1d1e6bd46e1b9a758d7f4bc3da Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 9 Mar 2020 02:10:41 +0100 Subject: [PATCH] Complete autoswagger! --- royalnet/backpack/stars/api_docs.py | 2 ++ royalnet/backpack/stars/docs.py | 17 ++++++++++++++--- royalnet/constellation/api/apistar.py | 3 ++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/royalnet/backpack/stars/api_docs.py b/royalnet/backpack/stars/api_docs.py index 128afb26..e306b234 100644 --- a/royalnet/backpack/stars/api_docs.py +++ b/royalnet/backpack/stars/api_docs.py @@ -6,5 +6,7 @@ from royalnet.version import semantic class ApiDocsStar(ApiStar): path = "/api/docs" + summary = "Get the swagger.json file used to generate this documentation." + async def api(self, data: ApiData) -> ru.JSON: return diff --git a/royalnet/backpack/stars/docs.py b/royalnet/backpack/stars/docs.py index 885d2616..97494f2d 100644 --- a/royalnet/backpack/stars/docs.py +++ b/royalnet/backpack/stars/docs.py @@ -11,14 +11,21 @@ class DocsStar(PageStar): path = "/docs" 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({ "swagger": "2.0", "info": { "description": "Autogenerated Royalnet API documentation", "title": "Royalnet", "version": f"{semantic}", - "paths": [star.swagger() for star in self.constellation.stars if isinstance(star, ApiStar)] - } + }, + "paths": paths }) return HTMLResponse( @@ -26,17 +33,21 @@ class DocsStar(PageStar): Royalnet Docs + +
diff --git a/royalnet/constellation/api/apistar.py b/royalnet/constellation/api/apistar.py index 32b4e250..c5c6007d 100644 --- a/royalnet/constellation/api/apistar.py +++ b/royalnet/constellation/api/apistar.py @@ -53,6 +53,7 @@ class ApiStar(PageStar, ABC): result = {} for method in cls.methods: result[method.lower()] = { + "operationId": cls.__name__, "summary": cls.summary, "description": cls.description, "produces": ["application/json"], @@ -64,7 +65,7 @@ class ApiStar(PageStar, ABC): "500": {"description": "Serverside unhandled exception"}, "501": {"description": "Not yet implemented"} }, - "paameters": [{ + "parameters": [{ "name": parameter, "in": "query", "description": cls.parameters[parameter],