Skip to content

Commit d3d9548

Browse files
committed
fix: address issue #1432
1 parent 955fdf8 commit d3d9548

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

pokemon_v2/api.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
import subprocess
23
from rest_framework import viewsets
34
from rest_framework.response import Response
45
from rest_framework.views import APIView
@@ -1058,3 +1059,62 @@ def get(self, request, pokemon_id):
10581059
)
10591060

10601061
return Response(encounters_list)
1062+
1063+
1064+
@extend_schema(
1065+
description="Returns metadata about the current deployed version of the API, including the git commit hash, deploy date, and tag (if any).",
1066+
tags=["utility"],
1067+
responses={
1068+
200: {
1069+
"type": "object",
1070+
"properties": {
1071+
"deploy_date": {"type": "string", "nullable": True},
1072+
"hash": {"type": "string", "nullable": True},
1073+
"tag": {"type": "string", "nullable": True},
1074+
},
1075+
}
1076+
},
1077+
)
1078+
class APIMetaView(APIView):
1079+
def get(self, request):
1080+
try:
1081+
git_hash = (
1082+
subprocess.check_output(
1083+
["git", "rev-parse", "HEAD"], stderr=subprocess.DEVNULL
1084+
)
1085+
.decode()
1086+
.strip()
1087+
)
1088+
except Exception:
1089+
git_hash = None
1090+
1091+
try:
1092+
deploy_date = (
1093+
subprocess.check_output(
1094+
["git", "log", "-1", "--format=%cI"], stderr=subprocess.DEVNULL
1095+
)
1096+
.decode()
1097+
.strip()
1098+
)
1099+
except Exception:
1100+
deploy_date = None
1101+
1102+
try:
1103+
tag_output = (
1104+
subprocess.check_output(
1105+
["git", "tag", "--points-at", "HEAD"], stderr=subprocess.DEVNULL
1106+
)
1107+
.decode()
1108+
.strip()
1109+
)
1110+
tag = tag_output if tag_output else None
1111+
except Exception:
1112+
tag = None
1113+
1114+
return Response(
1115+
{
1116+
"deploy_date": deploy_date,
1117+
"hash": git_hash,
1118+
"tag": tag,
1119+
}
1120+
)

pokemon_v2/tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5855,3 +5855,11 @@ def test_case_insensitive_api(self):
58555855
self.assertEqual(uppercase_response.status_code, status.HTTP_200_OK)
58565856

58575857
self.assertEqual(lowercase_response.data, uppercase_response.data)
5858+
5859+
# Meta Tests
5860+
def test_meta_api(self):
5861+
response = self.client.get("{}/meta".format(API_V2))
5862+
self.assertEqual(response.status_code, status.HTTP_200_OK)
5863+
self.assertIn("deploy_date", response.data)
5864+
self.assertIn("hash", response.data)
5865+
self.assertIn("tag", response.data)

pokemon_v2/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,5 @@
7676
PokemonEncounterView.as_view(),
7777
name="pokemon_encounters",
7878
),
79+
path("api/v2/meta", APIMetaView.as_view(), name="api_meta"),
7980
]

0 commit comments

Comments
 (0)