Skip to content

Commit

Permalink
feat(Swagger documentation): Users can access the API using swagger d…
Browse files Browse the repository at this point in the history
…ocumentation

- Install drf-yasg
- Add drf-yasg to the installed apps in settings.py
- Add default url

[Starts #165273468]
  • Loading branch information
engjames committed May 13, 2019
1 parent 8137eb1 commit 38732f5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
13 changes: 13 additions & 0 deletions authors/apps/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from drf_yasg.utils import swagger_auto_schema

import facebook
import twitter
Expand All @@ -36,6 +37,12 @@ class RegistrationAPIView(APIView):
renderer_classes = (UserJSONRenderer,)
serializer_class = RegistrationSerializer

@swagger_auto_schema(
operation_description="Regester a new User.",
operation_id="Sign up as a new user",
request_body=serializer_class,
responses={201: serializer_class(many=False), 400: "BAD REQUEST"},
)
def post(self, request):
user = request.data.get('user', {})
# The create serializer, validate serializer, save serializer pattern
Expand Down Expand Up @@ -72,6 +79,12 @@ class LoginAPIView(APIView):
renderer_classes = (UserJSONRenderer,)
serializer_class = LoginSerializer

@swagger_auto_schema(
operation_description="Login a User.",
operation_id="Login a user",
request_body=serializer_class,
responses={201: serializer_class(many=False), 400: "BAD REQUEST"},
)
def post(self, request):
user = request.data.get('user', {})
# Notice here that we do not call `serializer.save()` like we did for
Expand Down
3 changes: 2 additions & 1 deletion authors/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'rest_framework',
'django_filters',
'simple_history',
'drf_yasg',

'authors.apps.authentication',
'authors.apps.core',
Expand All @@ -55,7 +56,7 @@
'authors.apps.favorites',
'authors.apps.rate_article',
'authors.apps.article_tags',
'authors.apps.bookmarks'
'authors.apps.bookmarks',
]

MIDDLEWARE = [
Expand Down
21 changes: 20 additions & 1 deletion authors/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@
"""
from django.urls import include, path
from django.contrib import admin
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from rest_framework import permissions

schema_view = get_schema_view(
openapi.Info(
title="Author's Haven For Team Invictus",
default_version='v1',
description="Author's Haven is a community of like minded authors to \
foster inspiration and innovation by leveraging the modern web",
license=openapi.License(name="Andela License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
authentication_classes=(),
)


urlpatterns = [
path('admin/', admin.site.urls),
Expand All @@ -25,5 +42,7 @@
path('api/articles/', include('authors.apps.favorites.urls')),
path('api/', include('authors.apps.rate_article.urls')),
path('api/', include('authors.apps.article_tags.urls')),
path('api/', include('authors.apps.bookmarks.urls'))
path('api/', include('authors.apps.bookmarks.urls')),
path('docs/', schema_view.with_ui('swagger', cache_timeout=0),
name='schema-swagger-ui'),
]
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ autopep8==1.4.4
cachetools==3.1.0
certifi==2019.3.9
chardet==3.0.4
coreapi==2.3.3
coreschema==0.0.4
coverage==4.5.3
coveralls==1.7.0
defusedxml==0.6.0
Expand All @@ -15,6 +17,7 @@ django-filter==2.1.0
django-simple-history==2.7.2
djangorestframework==3.9.2
docopt==0.6.2
drf-yasg==1.15.0
entrypoints==0.3
extras==1.0.0
facebook-sdk==3.1.0
Expand All @@ -25,7 +28,11 @@ future==0.17.1
google-auth==1.6.3
gunicorn==19.9.0
idna==2.8
inflection==0.3.1
itypes==1.1.0
Jinja2==2.10.1
linecache2==1.0.0
MarkupSafe==1.1.1
mccabe==0.6.1
more-itertools==7.0.0
oauthlib==3.0.1
Expand All @@ -50,11 +57,13 @@ pytz==2019.1
requests==2.21.0
requests-oauthlib==1.2.0
rsa==4.0
ruamel.yaml==0.15.94
six==1.12.0
social-auth-core==3.1.0
sqlparse==0.3.0
testtools==2.3.0
traceback2==1.4.0
unittest2==1.1.0
uritemplate==3.0.0
urllib3==1.24.2
whitenoise==4.1.2

0 comments on commit 38732f5

Please sign in to comment.