From 849fed8e567c1ccf6de14cb909fd94409f70b552 Mon Sep 17 00:00:00 2001 From: Ales Teska Date: Thu, 2 Nov 2023 12:13:00 +0100 Subject: [PATCH 1/2] Improve on the Swagger doc --- asab/api/doc.py | 14 +++++++---- asab/api/doc_templates.py | 49 +++++++++++++++++---------------------- asab/web/__init__.py | 9 ++++++- examples/webserver.py | 4 +++- 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/asab/api/doc.py b/asab/api/doc.py index 4b5f59a7e..839aecd20 100644 --- a/asab/api/doc.py +++ b/asab/api/doc.py @@ -47,7 +47,7 @@ def __init__(self, api_service, app, web_container, config_section_name="asab:do self.ServerUrls: str = asab.Config.get(config_section_name, "server_urls", fallback="/").strip().split("\n") - def build_swagger_documentation(self) -> dict: + def build_swagger_documentation(self, host) -> dict: """ Take a docstring of the class and a docstring of methods and merge them into Swagger data. """ @@ -65,7 +65,8 @@ def build_swagger_documentation(self) -> dict: "version": "1.0.0", }, "servers": [ - {"url": url} for url in self.ServerUrls + # {"url": url} for url in self.ServerUrls + { "url": "http://{}".format(host)} ], # Base path relative to openapi endpoint @@ -261,11 +262,13 @@ async def doc(self, request): swagger_js_url: str = "https://unpkg.com/swagger-ui-dist@4/swagger-ui-bundle.js" swagger_css_url: str = "https://unpkg.com/swagger-ui-dist@4/swagger-ui.css" + base_url = request.headers.get('Host') + doc_page = SWAGGER_DOC_PAGE.format( title=self.App.__class__.__name__, swagger_css_url=swagger_css_url, swagger_js_url=swagger_js_url, - openapi_url="./asab/v1/openapi", + openapi_url="http://{}/asab/v1/openapi".format(base_url), ) return aiohttp.web.Response(text=doc_page, content_type="text/html") @@ -293,8 +296,11 @@ async def openapi(self, request): url: https://swagger.io/specification/ """ + + host = request.headers.get('Host') + return aiohttp.web.Response( - text=(yaml.dump(self.build_swagger_documentation(), sort_keys=False)), + text=(yaml.dump(self.build_swagger_documentation(host=host), sort_keys=False)), content_type="text/yaml", ) diff --git a/asab/api/doc_templates.py b/asab/api/doc_templates.py index d5e823a5d..0762a526d 100644 --- a/asab/api/doc_templates.py +++ b/asab/api/doc_templates.py @@ -79,31 +79,24 @@ """ SWAGGER_DOC_PAGE = """ - - - - - - {title} API Documentation - - - -
- - - -""" + + + API Reference {openapi_url} + + + + + + + + + +""" diff --git a/asab/web/__init__.py b/asab/web/__init__.py index ebffc81ab..28e20e489 100644 --- a/asab/web/__init__.py +++ b/asab/web/__init__.py @@ -19,7 +19,7 @@ def __init__(self, app): self.service = WebService(app, "asab.WebService") -def create_web_server(app, section: str = "web", config: typing.Optional[dict] = None) -> aiohttp.web.UrlDispatcher: +def create_web_server(app, section: str = "web", config: typing.Optional[dict] = None, api = False) -> aiohttp.web.UrlDispatcher: """ Build the web server with the specified configuration. @@ -49,6 +49,13 @@ async def hello(self, request): app.add_module(Module) websvc = app.get_service("asab.WebService") container = WebContainer(websvc, section, config=config) + + if api: + # The DiscoverySession is functional only with ApiService initialized. + from ..api import ApiService + apisvc = ApiService(app) + apisvc.initialize_web(container) + return container.WebApp.router diff --git a/examples/webserver.py b/examples/webserver.py index cddf3c969..92d3a1d37 100755 --- a/examples/webserver.py +++ b/examples/webserver.py @@ -1,4 +1,6 @@ #!/usr/bin/env python3 + +import asab.api import asab.web.rest @@ -14,7 +16,7 @@ def __init__(self): super().__init__() # Create the Web server - web = asab.web.create_web_server(self) + web = asab.web.create_web_server(self, api = True) # Add a route to the handler method web.add_get('/hello', self.hello) From a13359168aeb60b4b00117c18feb6041d948e91c Mon Sep 17 00:00:00 2001 From: Ales Teska Date: Tue, 5 Dec 2023 13:01:33 +0100 Subject: [PATCH 2/2] Flake8 --- asab/api/doc.py | 3 +-- asab/api/doc_templates.py | 34 ++++++++++++++++------------------ asab/web/__init__.py | 2 +- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/asab/api/doc.py b/asab/api/doc.py index 839aecd20..eb5d61611 100644 --- a/asab/api/doc.py +++ b/asab/api/doc.py @@ -65,8 +65,7 @@ def build_swagger_documentation(self, host) -> dict: "version": "1.0.0", }, "servers": [ - # {"url": url} for url in self.ServerUrls - { "url": "http://{}".format(host)} + {"url": "http://{}".format(host)} ], # Base path relative to openapi endpoint diff --git a/asab/api/doc_templates.py b/asab/api/doc_templates.py index 0762a526d..acc4c7aa4 100644 --- a/asab/api/doc_templates.py +++ b/asab/api/doc_templates.py @@ -80,23 +80,21 @@ SWAGGER_DOC_PAGE = """ - - API Reference {openapi_url} - - - - - - - - + + API Reference {openapi_url} + + + + + + + + """ diff --git a/asab/web/__init__.py b/asab/web/__init__.py index 28e20e489..bf78e911a 100644 --- a/asab/web/__init__.py +++ b/asab/web/__init__.py @@ -19,7 +19,7 @@ def __init__(self, app): self.service = WebService(app, "asab.WebService") -def create_web_server(app, section: str = "web", config: typing.Optional[dict] = None, api = False) -> aiohttp.web.UrlDispatcher: +def create_web_server(app, section: str = "web", config: typing.Optional[dict] = None, api: bool = False) -> aiohttp.web.UrlDispatcher: """ Build the web server with the specified configuration.