Skip to content

Commit 217030c

Browse files
committed
Added openapi specs and swagger/redoc ui to api
1 parent 2de326d commit 217030c

File tree

5 files changed

+432
-25
lines changed

5 files changed

+432
-25
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,6 @@ sudo systemctl enable iot.mqttsubscriber.service
162162
### Make a SD Card Backup
163163

164164
Read more here: https://howchoo.com/pi/create-a-backup-image-of-your-raspberry-pi-sd-card-in-mac-osx. this pretty useful as cards can become corrupt if power is cut or the device is not powered down correctly.
165+
166+
## API Documentation
167+
The API documentation is available at `/api/schema/ui/` (Swagger) and `/api/schema/docs/` (Redoc) when the server is running.

iotserver/settings.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
# 3rd party apps
3333
'corsheaders',
3434
'django_filters',
35+
'drf_spectacular',
3536
'mapwidgets',
3637
'rest_framework',
3738
'rest_framework_gis',
@@ -111,20 +112,6 @@
111112
},
112113
]
113114

114-
REST_FRAMEWORK = {
115-
'DEFAULT_AUTHENTICATION_CLASSES': [
116-
'rest_framework.authentication.SessionAuthentication',
117-
'rest_framework.authentication.TokenAuthentication',
118-
],
119-
'DEFAULT_PERMISSION_CLASSES': ['rest_framework.permissions.DjangoModelPermissions'],
120-
'DEFAULT_FILTER_BACKENDS': [
121-
'django_filters.rest_framework.DjangoFilterBackend',
122-
'rest_framework.filters.OrderingFilter',
123-
],
124-
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
125-
'PAGE_SIZE': 50,
126-
}
127-
128115
# Internationalization
129116
# https://docs.djangoproject.com/en/3.1/topics/i18n/
130117
LANGUAGE_CODE = 'en-us'
@@ -147,6 +134,31 @@
147134
GDAL_LIBRARY_PATH = os.environ.get('IOTSERVER_GDAL_LIBRARY_PATH', None)
148135
GEOS_LIBRARY_PATH = os.environ.get('IOTSERVER_GEOS_LIBRARY_PATH', None)
149136

137+
# Django Rest Framework config
138+
REST_FRAMEWORK = {
139+
'DEFAULT_AUTHENTICATION_CLASSES': (
140+
'rest_framework.authentication.SessionAuthentication',
141+
'rest_framework.authentication.TokenAuthentication',
142+
),
143+
'DEFAULT_RENDERER_CLASSES': ('rest_framework.renderers.JSONRenderer',),
144+
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.DjangoModelPermissions',),
145+
'DEFAULT_FILTER_BACKENDS': (
146+
'django_filters.rest_framework.DjangoFilterBackend',
147+
'rest_framework.filters.OrderingFilter',
148+
),
149+
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
150+
'PAGE_SIZE': 50,
151+
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
152+
}
153+
154+
# Spectacular settings - OpenAPI 3
155+
SPECTACULAR_SETTINGS = {
156+
'TITLE': 'IoTServer API',
157+
'DESCRIPTION': 'Simple IoT Server, Configuration Tool & Dashboard',
158+
'VERSION': '0.9.0',
159+
'SERVE_INCLUDE_SCHEMA': False,
160+
}
161+
150162
# MQTT settings
151163
MQTT = {
152164
'host': os.environ.get('IOTSERVER_MQTT_HOST', 'localhost'),

iotserver/urls.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
from django.conf.urls.static import static
33
from django.contrib import admin
44
from django.urls import include, path
5+
from drf_spectacular.views import (
6+
SpectacularAPIView,
7+
SpectacularRedocView,
8+
SpectacularSwaggerView
9+
)
510
from rest_framework import routers
611

712
from iotserver.apps.device.api import viewsets as device_viewsets
@@ -33,13 +38,28 @@
3338
path('admin/', admin.site.urls),
3439
]
3540

36-
3741
# DRF url patterns
3842
urlpatterns += [
3943
path('api/auth/token/', user_viewsets.ObtainAuthTokenUser.as_view()),
4044
path('api/auth/', include('rest_framework.urls')),
4145
path('api/', include(router.urls)),
4246
]
4347

48+
# DRF spectacular schema
49+
urlpatterns += [
50+
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
51+
path('api/schema/ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='ui'),
52+
path('api/schema/docs/', SpectacularRedocView.as_view(url_name='schema'), name='docs'),
53+
]
54+
55+
# from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
56+
# urlpatterns = [
57+
# # YOUR PATTERNS
58+
# path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
59+
# # Optional UI:
60+
# path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
61+
# path('api/schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),
62+
# ]
63+
4464
# Static files
4565
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

0 commit comments

Comments
 (0)