From 4471a404c70116b371bf509f5963d44a49a37a2d Mon Sep 17 00:00:00 2001 From: Yaphet Kebede Date: Fri, 3 Jan 2020 13:10:31 -0500 Subject: [PATCH 1/2] Adding starlette framework support for UI generation --- swagger_ui/core.py | 28 ++++++++++++++++++++++++++++ swagger_ui/old.py | 6 ++++++ 2 files changed, 34 insertions(+) diff --git a/swagger_ui/core.py b/swagger_ui/core.py index e459c9d..90ca92d 100644 --- a/swagger_ui/core.py +++ b/swagger_ui/core.py @@ -238,6 +238,27 @@ def on_get(self, req, resp): self._app.add_route(self._uri('/swagger.json'), SwaggerConfigHandler(self)) self._app.add_static_route(prefix=self._uri('/'), directory='{}/'.format(self.static_dir), downloadable=True) + def _starlette_handler(self): + from starlette.responses import HTMLResponse, JSONResponse + from starlette.staticfiles import StaticFiles + + async def swagger_doc_handler(request): + return HTMLResponse(content=self.doc_html, media_type='text/html') + + async def swagger_editor_handler(request): + return JSONResponse(content=self.editor_html, media_type='text/html') + + async def swagger_config_handler(request): + return JSONResponse(self.get_config(request.host)) + self._app.router.add_route(self._uri('/apidocs',), swagger_doc_handler, ['get'], 'swagger-ui') + + if self._editor: + self._app.router.add_route(self._uri('/editor'), swagger_editor_handler,['get'], 'swagger-editor') + + self._app.router.add_route(self._uri('/swagger.json'), swagger_config_handler, ['get'], 'swagger-config') + self._app.router.mount(self._uri('/'), app=StaticFiles(directory='{}/'.format(self.static_dir)), name='swagger-static-files') + + def _auto_match_handler(self): try: import tornado.web @@ -282,4 +303,11 @@ def _auto_match_handler(self): except ImportError: pass + try: + import starlette.application + if isinstance(self._app, starlette.application.Starlette): + return self._starlette_handler() + except ImportError: + pass + raise Exception('No match application isinstance type!') diff --git a/swagger_ui/old.py b/swagger_ui/old.py index c3e5c9d..94cc859 100644 --- a/swagger_ui/old.py +++ b/swagger_ui/old.py @@ -35,3 +35,9 @@ class FalconInterface(Interface): def __init__(self, *args, **kwargs): kwargs['app_type'] = 'falcon' super(FalconInterface, self).__init__(*args, **kwargs) + + +class StarletteInterface(Interface): + def __init__(self, *args, **kwargs): + kwargs['app_type'] = 'starlette' + super(StarletteInterface, self).__init__(*args, **kwargs) \ No newline at end of file From 11ea7ff939e5fece24ddfbe70cd776d43ab1d4bc Mon Sep 17 00:00:00 2001 From: Yaphet Kebede Date: Fri, 3 Jan 2020 13:13:43 -0500 Subject: [PATCH 2/2] Adding starlette framework support for UI generation --- swagger_ui/core.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/swagger_ui/core.py b/swagger_ui/core.py index 90ca92d..262842b 100644 --- a/swagger_ui/core.py +++ b/swagger_ui/core.py @@ -304,10 +304,10 @@ def _auto_match_handler(self): pass try: - import starlette.application - if isinstance(self._app, starlette.application.Starlette): + import starlette.applications + if isinstance(self._app, starlette.applications.Starlette): return self._starlette_handler() except ImportError: pass - + raise Exception('No match application isinstance type!')