From 167281ee83b3ac3b3044abf6604178c4ced8a2f4 Mon Sep 17 00:00:00 2001 From: Tom Kralidis Date: Wed, 30 Jun 2021 11:11:57 -0400 Subject: [PATCH] add support for OpenAPI rendering via ReDoc (#414) (#720) * add support for OpenAPI rendering via ReDoc (#414) * add support for OpenAPI rendering via ReDoc (#414) --- docs/source/openapi.rst | 11 +++++++++-- pygeoapi/api.py | 7 ++++++- pygeoapi/templates/landing_page.html | 2 +- pygeoapi/templates/openapi/redoc.html | 19 +++++++++++++++++++ .../{openapi.html => openapi/swagger.html} | 0 tests/test_api.py | 10 ++++++++++ 6 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 pygeoapi/templates/openapi/redoc.html rename pygeoapi/templates/{openapi.html => openapi/swagger.html} (100%) diff --git a/docs/source/openapi.rst b/docs/source/openapi.rst index 00a5483..0c387b8 100644 --- a/docs/source/openapi.rst +++ b/docs/source/openapi.rst @@ -21,8 +21,8 @@ The API is accessible at the ``/openapi`` endpoint, providing a Swagger-based we the pygeoapi demo OpenAPI/Swagger endpoint at https://demo.pygeoapi.io/master/openapi -Using OpenAPI -------------- +Using OpenAPI via Swagger +------------------------- Accessing the Swagger webpage we have the following structure: @@ -62,6 +62,13 @@ This identifier can be used to obtain a specific item from the dataset using the .. image:: /_static/openapi_get_item_id2.png +Using OpenAPI via ReDoc +----------------------- + +pygeoapi also supports OpenAPI document rendering via `ReDoc `_. + +ReDoc rendering is accessible at the same ``/openapi`` endpoint, adding ``ui=redoc`` to the request URL. + Summary ------- diff --git a/pygeoapi/api.py b/pygeoapi/api.py index 4d99282..af77278 100644 --- a/pygeoapi/api.py +++ b/pygeoapi/api.py @@ -629,13 +629,18 @@ class API: return self.get_format_exception(request) headers = request.get_response_headers() + if request.format == F_HTML: + template = 'openapi/swagger.html' + if request._args.get('ui') == 'redoc': + template = 'openapi/redoc.html' + path = '/'.join([self.config['server']['url'].rstrip('/'), 'openapi']) data = { 'openapi-document-path': path } - content = render_j2_template(self.config, 'openapi.html', data, + content = render_j2_template(self.config, template, data, request.locale) return headers, 200, content diff --git a/pygeoapi/templates/landing_page.html b/pygeoapi/templates/landing_page.html index 417a62e..caca4a8 100644 --- a/pygeoapi/templates/landing_page.html +++ b/pygeoapi/templates/landing_page.html @@ -68,7 +68,7 @@

API Definition

- Documentation + Documentation: Swagger UI ReDoc

OpenAPI Document diff --git a/pygeoapi/templates/openapi/redoc.html b/pygeoapi/templates/openapi/redoc.html new file mode 100644 index 0000000..9ab5e41 --- /dev/null +++ b/pygeoapi/templates/openapi/redoc.html @@ -0,0 +1,19 @@ + + + + ReDoc - {{ config['metadata']['identification']['title'] }} + + + + + + + + + + diff --git a/pygeoapi/templates/openapi.html b/pygeoapi/templates/openapi/swagger.html similarity index 100% rename from pygeoapi/templates/openapi.html rename to pygeoapi/templates/openapi/swagger.html diff --git a/tests/test_api.py b/tests/test_api.py index 5938a94..ecde13b 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -208,6 +208,16 @@ def test_api(config, api_, openapi): assert rsp_headers['Content-Type'] == FORMAT_TYPES[F_HTML] == \ FORMAT_TYPES[F_HTML] + assert 'Swagger UI' in response + + a = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' + req = mock_request({'ui': 'redoc'}, HTTP_ACCEPT=a) + rsp_headers, code, response = api_.openapi(req, openapi) + assert rsp_headers['Content-Type'] == FORMAT_TYPES[F_HTML] == \ + FORMAT_TYPES[F_HTML] + + assert 'ReDoc' in response + req = mock_request({'f': 'foo'}) rsp_headers, code, response = api_.openapi(req, openapi) assert rsp_headers['Content-Language'] == 'en-US'