Browse Source

[api] Add swagger UI and schema for public APIs (#3776)

Co-authored-by: tabraiz <tabraiz@cloudera.com>
Tabraiz 1 year ago
parent
commit
034e6fd3fe

+ 1 - 0
desktop/core/base_requirements.txt

@@ -74,6 +74,7 @@ git+https://github.com/gethue/django-babel.git
 django-utils-six==2.0
 six==1.16.0
 psutil==5.8.0
+drf-spectacular[sidecar]==0.27.2
 -e file://${ROOT}/desktop/core/ext-py3/boto-2.49.0
 -e file://${ROOT}/desktop/core/ext-py3/django-axes-5.13.0
 -e file://${ROOT}/desktop/core/ext-py3/djangosaml2-0.18.0

+ 13 - 1
desktop/core/src/desktop/settings.py

@@ -208,6 +208,8 @@ INSTALLED_APPS = [
     # 'django_celery_results',
     'rest_framework',
     'rest_framework.authtoken',
+    'drf_spectacular',
+    'drf_spectacular_sidecar'
 ]
 
 WEBPACK_LOADER = {
@@ -325,8 +327,18 @@ REST_FRAMEWORK = {
     'DEFAULT_PERMISSION_CLASSES': [
       'rest_framework.permissions.IsAuthenticated',
     ],
-    'DEFAULT_AUTHENTICATION_CLASSES': desktop.conf.AUTH.API_AUTH.get()
+    'DEFAULT_AUTHENTICATION_CLASSES': desktop.conf.AUTH.API_AUTH.get(),
+    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
 }
+
+SPECTACULAR_SETTINGS = {
+    'TITLE': 'Hue Public API',
+    'VERSION': '1.0.0',
+    'SWAGGER_UI_DIST': 'SIDECAR',
+    'SWAGGER_UI_FAVICON_HREF': 'SIDECAR',
+}
+
+
 if desktop.conf.is_custom_jwt_auth_enabled() and \
   'desktop.auth.api_authentications.JwtAuthentication' not in REST_FRAMEWORK['DEFAULT_AUTHENTICATION_CLASSES']:
   REST_FRAMEWORK['DEFAULT_AUTHENTICATION_CLASSES'].insert(0, 'desktop.auth.api_authentications.JwtAuthentication')

+ 8 - 16
desktop/core/src/desktop/urls.py

@@ -15,25 +15,18 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from __future__ import absolute_import
-
 import re
-import sys
-import logging
 
 from django.conf import settings
+from django.urls import include, re_path
 from django.views.static import serve
+from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
 from rest_framework_simplejwt.views import (
   TokenObtainPairView,
   TokenRefreshView,
   TokenVerifyView,
 )
 
-# FIXME: This could be replaced with hooking into the `AppConfig.ready()`
-# signal in Django 1.7:
-#
-# https://docs.djangoproject.com/en/1.7/ref/applications/#django.apps.AppConfig.ready
-#
 import desktop.lib.metrics.file_reporter
 from desktop import api as desktop_api, api2 as desktop_api2, api_public_urls_v1, appmanager, views as desktop_views
 from desktop.auth import views as desktop_auth_views
@@ -46,10 +39,6 @@ from useradmin import views as useradmin_views
 
 desktop.lib.metrics.file_reporter.start_file_reporter()
 
-if sys.version_info[0] > 2:
-  from django.urls import include, re_path
-else:
-  from django.conf.urls import include, url as re_path
 
 # Django expects handler404 and handler500 to be defined.
 # django.conf.urls provides them. But we want to override them.
@@ -202,9 +191,12 @@ dynamic_patterns += [
 ]
 
 dynamic_patterns += [
-  re_path('^api/v1/token/auth/?$', TokenObtainPairView.as_view(), name='token_obtain'),
-  re_path('^api/v1/token/verify/?$', TokenVerifyView.as_view(), name='token_verify'),
-  re_path('^api/v1/token/refresh/?$', TokenRefreshView.as_view(), name='token_refresh'),
+  re_path(r'^api/schema/?$', SpectacularAPIView.as_view(), name='schema'),
+  re_path(r'^api/swagger/?$', SpectacularSwaggerView.as_view(), name='swagger-ui'),
+
+  re_path(r'^api/v1/token/auth/?$', TokenObtainPairView.as_view(), name='token_obtain'),
+  re_path(r'^api/v1/token/verify/?$', TokenVerifyView.as_view(), name='token_verify'),
+  re_path(r'^api/v1/token/refresh/?$', TokenRefreshView.as_view(), name='token_refresh'),
 
   re_path(r'^api/v1/', include(('desktop.api_public_urls_v1', 'api'), 'api')),
 ]