From 138d6c09bcb0f8525a404de1e54af30ae6f1790d Mon Sep 17 00:00:00 2001 From: Ngure Nyaga Date: Tue, 9 Nov 2021 07:07:52 +0300 Subject: [PATCH] feat: add /openapi (OAS schema) and schema docs This will make the REST API self describing --- config/settings/base.py | 5 +++- config/urls.py | 36 ++++++++++++++++++++++++++--- mycarehub/templates/redoc.html | 24 +++++++++++++++++++ mycarehub/templates/swagger-ui.html | 31 +++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 mycarehub/templates/redoc.html create mode 100644 mycarehub/templates/swagger-ui.html diff --git a/config/settings/base.py b/config/settings/base.py index 4c09c22..f781550 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -330,6 +330,7 @@ "DATETIME_FORMAT": "iso-8601", "DATE_FORMAT": "iso-8601", "TIME_FORMAT": "iso-8601", + "UNICODE_JSON": "true", } # OAuth @@ -354,7 +355,8 @@ CORS_URLS_REGEX = r"^/api/.*$" # wagtail CMS -WAGTAIL_SITE_NAME = "myCareHub" +SITE_NAME = "myCareHub" +WAGTAIL_SITE_NAME = SITE_NAME WAGTAIL_APPEND_SLASH = True WAGTAILSEARCH_HITS_MAX_AGE = 30 WAGTAIL_I18N_ENABLED = True @@ -403,3 +405,4 @@ ) GRAPHENE = {"SCHEMA": "mycarehub.schema.schema.schema"} +API_VERSION = "0.0.1" diff --git a/config/urls.py b/config/urls.py index c3dfd2b..94f6373 100644 --- a/config/urls.py +++ b/config/urls.py @@ -6,9 +6,10 @@ from django.urls import include, path, re_path from django.views import defaults as default_views from django.views.decorators.csrf import csrf_exempt -from django.views.generic import RedirectView +from django.views.generic import RedirectView, TemplateView from graphene_django.views import GraphQLView from rest_framework.authtoken.views import obtain_auth_token +from rest_framework.schemas import get_schema_view from wagtail.admin import urls as wagtailadmin_urls from wagtail.core import urls as wagtail_urls from wagtail.documents import urls as wagtaildocs_urls @@ -45,7 +46,7 @@ # re_path(r"authoring", include("mycarehub.content.urls")), # For anything not caught by a more specific rule above, hand over to # Wagtail's serving mechanism - re_path(r"content", include(wagtail_urls)), + re_path(r"content", include(wagtail_urls), name="wagtail"), path("graphql", csrf_exempt(GraphQLView.as_view(graphiql=True)), name="graphql"), # content management path("admin/", include(wagtailadmin_urls)), @@ -61,7 +62,7 @@ urlpatterns += staticfiles_urlpatterns() # API URLS -urlpatterns += [ +api_patterns = [ # API base url path("api/", include("config.api_router")), # DRF auth token @@ -69,6 +70,35 @@ # OAuth 2 path("o/", include("oauth2_provider.urls", namespace="oauth2_provider")), ] +urlpatterns += api_patterns + +# Open API Schema and automatic API documentation +urlpatterns += [ + path( + "openapi", + get_schema_view( + title=settings.SITE_NAME, + description=f"{settings.SITE_NAME} REST API", + version=settings.API_VERSION, + patterns=api_patterns, + ), + name="openapi-schema", + ), + path( + "swagger/", + TemplateView.as_view( + template_name="swagger-ui.html", extra_context={"schema_url": "openapi-schema"} + ), + name="swagger-ui", + ), + path( + "redoc/", + TemplateView.as_view( + template_name="redoc.html", extra_context={"schema_url": "openapi-schema"} + ), + name="redoc", + ), +] if settings.DEBUG: # This allows the error pages to be debugged during development, just visit diff --git a/mycarehub/templates/redoc.html b/mycarehub/templates/redoc.html new file mode 100644 index 0000000..4250cce --- /dev/null +++ b/mycarehub/templates/redoc.html @@ -0,0 +1,24 @@ + + + + + ReDoc + + + + + + + + + + + + + + diff --git a/mycarehub/templates/swagger-ui.html b/mycarehub/templates/swagger-ui.html new file mode 100644 index 0000000..727b8fb --- /dev/null +++ b/mycarehub/templates/swagger-ui.html @@ -0,0 +1,31 @@ + + + + + Swagger + + + + + + +
+ + + + +