Skip to content

Commit 9163d88

Browse files
committed
Introduce swagger REST API
1 parent b3873cd commit 9163d88

File tree

6 files changed

+43
-5
lines changed

6 files changed

+43
-5
lines changed

README.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,18 @@ Please use `admin` superuser for management.
7878

7979
### REST Api
8080

81-
Rest api is build with `djangorestframework` followed by https://www.django-rest-framework.org documentation.
81+
Rest api is build with `djangorestframework` and `drf-yasg` (swagger) libraries.
8282

8383
Here are available api endpoints:
84-
85-
- `/api`: _Retrieves all quotes_
86-
- `/api/<id>`: _Retrieves a single quote by it's id_
84+
- `/api`:
85+
- `GET`: _Retrieves all quotes_
86+
- `POST`: _Creates a new quote_
87+
- `/api/<id>`:
88+
- `GET`: _Retrieves a single quote by it's id_
89+
- `PUT`: _Updates a single quote by it's id_
90+
- `DELETE`: _Deletes a quote by it's id_
91+
92+
> Please refer to `/api/docs` endpoint provides a neat swagger REST API documentation.
8793
8894
### Testing
8995

quotes/api/urls.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
"""Represents URL endpoints for an application."""
22
from typing import Any, List
33
from django.urls import include, path
4+
from rest_framework import permissions
5+
from drf_yasg.views import get_schema_view
6+
from drf_yasg import openapi
47
from .views import QuoteDetail, Quotes
58

9+
610
urlpatterns: List[Any] = [
711
path("", Quotes.as_view()),
812
path("<int:pk>", QuoteDetail.as_view()),
913
path("api-auth/", include("rest_framework.urls")),
14+
path(
15+
"docs/",
16+
get_schema_view(
17+
openapi.Info(
18+
title="Quotes REST API",
19+
default_version="v1",
20+
description="Swagger documentation for Quotes REST API",
21+
terms_of_service="https://www.google.com/policies/terms/",
22+
contact=openapi.Contact(email="[email protected]"),
23+
license=openapi.License(name="MIT License"),
24+
),
25+
public=True,
26+
permission_classes=(permissions.AllowAny,),
27+
).with_ui("swagger", cache_timeout=0),
28+
name="schema-swagger-ui",
29+
),
1030
]

quotes/api/views.py

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class Quotes(ListCreateAPIView):
1010
"""Responsible for retrieving all quotes from an application.
1111
1212
Endpoint is `/api/`
13+
14+
``GET``: retrieve all quotes
15+
16+
``POST``: creates a new quote
1317
"""
1418

1519
queryset: List[Quote] = Quote.objects.all()
@@ -20,6 +24,12 @@ class QuoteDetail(RetrieveUpdateDestroyAPIView):
2024
"""Responsible for retrieving a single quote from an application.
2125
2226
Endpoint is `/api/<id>`
27+
28+
``GET``: retrieve a single quote
29+
30+
``PUT``: updates a single quote
31+
32+
``DELETE``: deletes a single quote
2333
"""
2434

2535
permission_classes = (IsOwnerOrReadOnly,)

quotes/app/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Quote(Model):
1313
cover: Field = URLField(blank=True, null=True)
1414
added: Field = DateTimeField(auto_now_add=True)
1515
edited: Field = DateTimeField(auto_now=True)
16-
user = ForeignKey(User, on_delete=CASCADE, blank=True, null=True)
16+
user: Field = ForeignKey(User, on_delete=CASCADE, blank=True, null=True)
1717

1818
def __str__(self) -> str:
1919
"""Returns quote as a string."""

quotes/manager/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"django.contrib.humanize",
3131
"django_registration",
3232
"rest_framework",
33+
"drf_yasg",
3334
"app",
3435
"api",
3536
)

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ django==3.0.4
22
dj-database-url==0.5.0
33
django-registration==3.1
44
djangorestframework==3.11.0
5+
drf-yasg==1.17.1
56
requests==2.23.0
67
gunicorn==20.0.4
78
psycopg2-binary==2.8.4

0 commit comments

Comments
 (0)