Skip to content

Commit

Permalink
feat: add /openapi (OAS schema) and schema docs
Browse files Browse the repository at this point in the history
This will make the REST API self describing
  • Loading branch information
ngurenyaga committed Nov 9, 2021
1 parent 686bbf0 commit 138d6c0
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
5 changes: 4 additions & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@
"DATETIME_FORMAT": "iso-8601",
"DATE_FORMAT": "iso-8601",
"TIME_FORMAT": "iso-8601",
"UNICODE_JSON": "true",
}

# OAuth
Expand All @@ -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
Expand Down Expand Up @@ -403,3 +405,4 @@
)

GRAPHENE = {"SCHEMA": "mycarehub.schema.schema.schema"}
API_VERSION = "0.0.1"
36 changes: 33 additions & 3 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)),
Expand All @@ -61,14 +62,43 @@
urlpatterns += staticfiles_urlpatterns()

# API URLS
urlpatterns += [
api_patterns = [
# API base url
path("api/", include("config.api_router")),
# DRF auth token
path("auth-token/", obtain_auth_token),
# 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
Expand Down
24 changes: 24 additions & 0 deletions mycarehub/templates/redoc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>

<head>
<title>ReDoc</title>
<!-- needed for adaptive design -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
<!-- ReDoc doesn't change outer page styles -->
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>

<body>
<redoc spec-url='{% url schema_url %}'></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"> </script>
</body>

</html>
31 changes: 31 additions & 0 deletions mycarehub/templates/swagger-ui.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>

<head>
<title>Swagger</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="//unpkg.com/swagger-ui-dist@3/swagger-ui.css" />
</head>

<body>
<div id="swagger-ui"></div>
<script src="//unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
<script>
const ui = SwaggerUIBundle({
url: "{% url schema_url %}",
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
],
layout: "BaseLayout",
requestInterceptor: (request) => {
request.headers['X-CSRFToken'] = "{{ csrf_token }}"
return request;
}
})
</script>
</body>

</html>

0 comments on commit 138d6c0

Please sign in to comment.