Skip to content
Open
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
60 changes: 60 additions & 0 deletions pokemon_v2/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import subprocess
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.views import APIView
Expand Down Expand Up @@ -1058,3 +1059,62 @@ def get(self, request, pokemon_id):
)

return Response(encounters_list)


@extend_schema(
description="Returns metadata about the current deployed version of the API, including the git commit hash, deploy date, and tag (if any).",
tags=["utility"],
responses={
200: {
"type": "object",
"properties": {
"deploy_date": {"type": "string", "nullable": True},
"hash": {"type": "string", "nullable": True},
"tag": {"type": "string", "nullable": True},
},
}
},
)
class APIMetaView(APIView):
def get(self, request):
try:
git_hash = (
subprocess.check_output(
["git", "rev-parse", "HEAD"], stderr=subprocess.DEVNULL
)
.decode()
.strip()
)
except Exception:
git_hash = None

try:
deploy_date = (
subprocess.check_output(
["git", "log", "-1", "--format=%cI"], stderr=subprocess.DEVNULL
)
.decode()
.strip()
)
except Exception:
deploy_date = None

try:
tag_output = (
subprocess.check_output(
["git", "tag", "--points-at", "HEAD"], stderr=subprocess.DEVNULL
)
.decode()
.strip()
)
tag = tag_output if tag_output else None
except Exception:
tag = None

return Response(
{
"deploy_date": deploy_date,
"hash": git_hash,
"tag": tag,
}
)
8 changes: 8 additions & 0 deletions pokemon_v2/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5855,3 +5855,11 @@ def test_case_insensitive_api(self):
self.assertEqual(uppercase_response.status_code, status.HTTP_200_OK)

self.assertEqual(lowercase_response.data, uppercase_response.data)

# Meta Tests
def test_meta_api(self):
response = self.client.get("{}/meta".format(API_V2))
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn("deploy_date", response.data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests will always pass, because it is looking for the key, which is always there.

self.assertIn("hash", response.data)
self.assertIn("tag", response.data)
1 change: 1 addition & 0 deletions pokemon_v2/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@
PokemonEncounterView.as_view(),
name="pokemon_encounters",
),
path("api/v2/meta", APIMetaView.as_view(), name="api_meta"),
]