Skip to content

Commit

Permalink
fix: improve ping view response and add schema documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
wilfredinni committed Nov 20, 2024
1 parent 6ae92af commit dc6553e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion apps/core/tests/test_core_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ def test_ping_view_method_not_allowed():
client = Client()
response = client.post(reverse("ping"))
assert response.status_code == 405
assert response.json() == {"error": "Method Not Allowed"}
assert response.json() == {"detail": 'Method "POST" not allowed.'}
26 changes: 19 additions & 7 deletions apps/core/views.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
from django.http import JsonResponse
from drf_spectacular.utils import extend_schema
from rest_framework.decorators import api_view

from .tasks import test_task


@extend_schema(
description="Handles a ping request to check if the server is responsive.",
responses={
200: {
"type": "object",
"properties": {"ping": {"type": "string"}},
"example": {"ping": "pong"},
},
405: {
"type": "object",
"properties": {"detail": {"type": "string"}},
"example": {"detail": 'Method "POST" not allowed.'},
},
},
)
@api_view(["GET"])
def ping(request):
"""
Handles a ping request to check if the server is responsive.
"""
if request.method == "GET":
return JsonResponse({"ping": "pong"})

return JsonResponse({"error": "Method Not Allowed"}, status=405)
return JsonResponse({"ping": "pong"})


def fire_task(request):
Expand Down

0 comments on commit dc6553e

Please sign in to comment.