Skip to content

Commit

Permalink
Add rapidoc api docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kdsuneraavinash committed Sep 16, 2023
1 parent 9084b5e commit fa9c95f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
2 changes: 1 addition & 1 deletion apps/api_auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
user_model = get_user_model()


@extend_schema(auth=[], responses={status.HTTP_201_CREATED: api_settings.JWT_SERIALIZER})
@extend_schema(auth=[], responses={status.HTTP_201_CREATED: api_settings.JWT_SERIALIZER}, summary="Register a new user")
class RegisterView(CreateAPIView):
serializer_class = RegisterSerializer
permission_classes = [AllowAny]
Expand Down
45 changes: 37 additions & 8 deletions config/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,36 @@
from rest_framework.views import APIView


class SpectacularElementsView(APIView):
class SpectacularCustomView(APIView):
"""
This view returns a standalone HTML page which can be used for manual
exploration of the API schema.
The view is based on the Stoplight Elements project:
https://github.com/stoplightio/elements
"""

renderer_classes = [TemplateHTMLRenderer]
permission_classes = spectacular_settings.SERVE_PERMISSIONS
authentication_classes = AUTHENTICATION_CLASSES
url_name = "schema"
url = None
template_name = "schema/elements_api_doc.html"
title = spectacular_settings.TITLE

@extend_schema(exclude=True)
def get(self, request, *args, **kwargs):
raise NotImplementedError()

def _get_schema_url(self, request):
schema_url = self.url or get_relative_url(reverse(self.url_name, request=request))
return set_query_parameters(url=schema_url, lang=request.GET.get("lang"), version=request.GET.get("version"))


class SpectacularElementsView(SpectacularCustomView):
"""
The view is based on the Stoplight Elements project:
https://github.com/stoplightio/elements
"""

template_name = "schema/elements_api_doc.html"

@extend_schema(exclude=True)
def get(self, request, *args, **kwargs):
return Response(
Expand All @@ -37,6 +50,22 @@ def get(self, request, *args, **kwargs):
template_name=self.template_name,
)

def _get_schema_url(self, request):
schema_url = self.url or get_relative_url(reverse(self.url_name, request=request))
return set_query_parameters(url=schema_url, lang=request.GET.get("lang"), version=request.GET.get("version"))

class SpectacularRapiDocView(SpectacularCustomView):
"""
The view is based on the RapiDoc project:
https://github.com/rapi-doc/RapiDoc
"""

template_name = "schema/rapidoc_api_doc.html"

@extend_schema(exclude=True)
def get(self, request, *args, **kwargs):
return Response(
data={
"title": self.title,
"js_dist": "https://cdn.jsdelivr.net/npm/rapidoc@latest/dist/rapidoc-min.js",
"schema_url": self._get_schema_url(request),
},
template_name=self.template_name,
)
4 changes: 2 additions & 2 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from rest_framework.routers import DefaultRouter

from config.admin import custom_admin_site
from config.schema import SpectacularElementsView
from config.schema import SpectacularRapiDocView

spectacular_api_view = spec_views.SpectacularAPIView.as_view()
spectacular_api_docs_view = SpectacularElementsView.as_view(url_name="schema")
spectacular_api_docs_view = SpectacularRapiDocView.as_view(url_name="schema")

router = DefaultRouter()
# TODO: Register API view sets here
Expand Down
2 changes: 1 addition & 1 deletion templates/schema/elements_api_doc.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<link rel="stylesheet" href="{{ css_dist }}">
</head>
<body>
<elements-api apiDescriptionUrl="{{ schema_url }}" router="hash" />
<elements-api apiDescriptionUrl="{{ schema_url }}" router="hash"></elements-api>
</body>
</html>
27 changes: 27 additions & 0 deletions templates/schema/rapidoc_api_doc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ title|default:"RapiDoc" }}</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="module" src="{{ js_dist }}"></script>
</head>
<body>
<rapi-doc
spec-url="{{ schema_url }}"
persist-auth="true"
fill-request-fields-with-example="false"
theme="light"
show-method-in-nav-bar="as-colored-block"
use-path-in-nav-bar="true"
nav-bg-color="#161F6D"
primary-color="#337DEF"
show-header="false"
allow-spec-file-download="true"
>
<img slot="nav-logo" src="https://ixdlabs.com/_next/static/media/logo-icon-white.e468545b.svg"
style="width:100px; height:100px; margin-left: auto; margin-right: auto"/>
</rapi-doc>

</body>
</html>

0 comments on commit fa9c95f

Please sign in to comment.