Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ services:
condition: service_healthy
volumes:
- ./website:/usr/src/website
command: ["./pytest.sh"]
command: ["./pytest.sh"]
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ volumes:

networks:
traefik-public:
external: true
external: true
Empty file.
2 changes: 2 additions & 0 deletions website/apps/core/api/docstring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
HEALTH_DOCS = "Check the health of the API"
HEALTH_RESPONSE_DOCS = "Returns the current health status"
15 changes: 15 additions & 0 deletions website/apps/core/api/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from drf_spectacular.utils import OpenApiResponse, extend_schema
from rest_framework import status

from apps.core.api import docstring

health_schema = extend_schema(
summary="Check Health",
description=docstring.HEALTH_DOCS,
tags=["Health"],
responses={
status.HTTP_200_OK: OpenApiResponse(
description=docstring.HEALTH_RESPONSE_DOCS,
),
},
)
9 changes: 9 additions & 0 deletions website/apps/core/api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path

from apps.core.api.views import health

app_name = "core"

urlpatterns = [
path("health/", health, name="health"),
]
10 changes: 10 additions & 0 deletions website/apps/core/api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from rest_framework.decorators import api_view
from rest_framework.response import Response

from apps.core.api.schema import health_schema


@health_schema
@api_view(["GET"])
def health(request):
return Response({"status": "ok"})
3 changes: 1 addition & 2 deletions website/apps/polls/api/v1/docstring.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
CHECK_HEALTH_DOCS = "Check the health of the API"
CHECK_HEALTH_RESPONSE_DOCS = "Returns empty response"
LIST_POLLS_DOCS = "List all available polls"
14 changes: 7 additions & 7 deletions website/apps/polls/api/v1/schema.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from drf_spectacular.utils import OpenApiResponse, extend_schema
from drf_spectacular.utils import extend_schema
from rest_framework import status

from apps.polls.api.v1 import docstring
from apps.polls.api.v1.serializers import PollSerializer

check_health_schema = extend_schema(
summary="Check Health API",
description=docstring.CHECK_HEALTH_DOCS,
list_polls_schema = extend_schema(
summary="List Polls",
description=docstring.LIST_POLLS_DOCS,
tags=["Polls"],
responses={
status.HTTP_200_OK: OpenApiResponse(
description=docstring.CHECK_HEALTH_RESPONSE_DOCS,
),
status.HTTP_200_OK: PollSerializer(many=True),
},
)
6 changes: 6 additions & 0 deletions website/apps/polls/api/v1/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from rest_framework import serializers


class PollSerializer(serializers.Serializer):
id = serializers.IntegerField()
question = serializers.CharField()
4 changes: 2 additions & 2 deletions website/apps/polls/api/v1/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.urls import path

from .views import CheckHealthView
from .views import list_polls

app_name = "v1"

urlpatterns = [
path("health/", CheckHealthView.as_view(), name="health"),
path("polls/", list_polls, name="polls"),
]
19 changes: 13 additions & 6 deletions website/apps/polls/api/v1/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.views import APIView

from apps.polls.api.v1.schema import check_health_schema
from apps.polls.api.v1.schema import list_polls_schema
from apps.polls.api.v1.serializers import PollSerializer

FAKE_POLLS = [
{"id": 1, "question": "What is your favorite color?"},
{"id": 2, "question": "What is your favorite food?"},
]

class CheckHealthView(APIView):
@check_health_schema
def get(self, request):
return Response({"status": "ok"})

@list_polls_schema
@api_view(["GET"])
def list_polls(request):
serializer = PollSerializer(FAKE_POLLS, many=True)
return Response(serializer.data)
1 change: 1 addition & 0 deletions website/website/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
app_name = "api"

urlpatterns = [
path("", include("apps.core.api.urls")),
path("", include("apps.polls.api.urls")),
]
Loading