diff --git a/docker-compose.test.yml b/docker-compose.test.yml index 34946e7..428d92c 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -32,4 +32,4 @@ services: condition: service_healthy volumes: - ./website:/usr/src/website - command: ["./pytest.sh"] \ No newline at end of file + command: ["./pytest.sh"] diff --git a/docker-compose.yml b/docker-compose.yml index d27ed23..587eff0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -67,4 +67,4 @@ volumes: networks: traefik-public: - external: true \ No newline at end of file + external: true diff --git a/website/apps/core/api/__init__.py b/website/apps/core/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/website/apps/core/api/docstring.py b/website/apps/core/api/docstring.py new file mode 100644 index 0000000..8ff60a1 --- /dev/null +++ b/website/apps/core/api/docstring.py @@ -0,0 +1,2 @@ +HEALTH_DOCS = "Check the health of the API" +HEALTH_RESPONSE_DOCS = "Returns the current health status" diff --git a/website/apps/core/api/schema.py b/website/apps/core/api/schema.py new file mode 100644 index 0000000..1a09e5e --- /dev/null +++ b/website/apps/core/api/schema.py @@ -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, + ), + }, +) diff --git a/website/apps/core/api/urls.py b/website/apps/core/api/urls.py new file mode 100644 index 0000000..e3d2c6e --- /dev/null +++ b/website/apps/core/api/urls.py @@ -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"), +] diff --git a/website/apps/core/api/views.py b/website/apps/core/api/views.py new file mode 100644 index 0000000..cc2fc1d --- /dev/null +++ b/website/apps/core/api/views.py @@ -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"}) diff --git a/website/apps/polls/api/v1/docstring.py b/website/apps/polls/api/v1/docstring.py index 493b189..2aee407 100644 --- a/website/apps/polls/api/v1/docstring.py +++ b/website/apps/polls/api/v1/docstring.py @@ -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" diff --git a/website/apps/polls/api/v1/schema.py b/website/apps/polls/api/v1/schema.py index c9eb5c6..fbb8ebd 100644 --- a/website/apps/polls/api/v1/schema.py +++ b/website/apps/polls/api/v1/schema.py @@ -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), }, ) diff --git a/website/apps/polls/api/v1/serializers.py b/website/apps/polls/api/v1/serializers.py new file mode 100644 index 0000000..9ae7c00 --- /dev/null +++ b/website/apps/polls/api/v1/serializers.py @@ -0,0 +1,6 @@ +from rest_framework import serializers + + +class PollSerializer(serializers.Serializer): + id = serializers.IntegerField() + question = serializers.CharField() diff --git a/website/apps/polls/api/v1/urls.py b/website/apps/polls/api/v1/urls.py index 5832ca1..570f901 100644 --- a/website/apps/polls/api/v1/urls.py +++ b/website/apps/polls/api/v1/urls.py @@ -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"), ] diff --git a/website/apps/polls/api/v1/views.py b/website/apps/polls/api/v1/views.py index d4ea82d..fb0909d 100644 --- a/website/apps/polls/api/v1/views.py +++ b/website/apps/polls/api/v1/views.py @@ -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) diff --git a/website/website/api.py b/website/website/api.py index a866667..052aaf9 100644 --- a/website/website/api.py +++ b/website/website/api.py @@ -3,5 +3,6 @@ app_name = "api" urlpatterns = [ + path("", include("apps.core.api.urls")), path("", include("apps.polls.api.urls")), ]