Skip to content

Commit e36f467

Browse files
Fix: Expose endpoint with version/hash (#1434)
* fix: address issue #1432 * refactor: use viewsets Co-authored-by: gabrnavarro <gabrnavarro@users.noreply.github.com> Co-authored-by: Naramsim <igougi.ui@gmail.com>
1 parent fd3bdea commit e36f467

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

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 PokeapiMetaViewset(viewsets.ViewSet):
1079+
def list(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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from datetime import datetime
23
from rest_framework import status
34
from rest_framework.test import APITestCase
45
from pokemon_v2.models import *
@@ -5855,3 +5856,12 @@ def test_case_insensitive_api(self):
58555856
self.assertEqual(uppercase_response.status_code, status.HTTP_200_OK)
58565857

58575858
self.assertEqual(lowercase_response.data, uppercase_response.data)
5859+
5860+
# Meta Tests
5861+
def test_meta_api(self):
5862+
response = self.client.get("{}/meta/".format(API_V2))
5863+
self.assertEqual(response.status_code, status.HTTP_200_OK)
5864+
self.assertTrue(datetime.fromisoformat(response.data["deploy_date"]))
5865+
self.assertEqual(25, len(response.data["deploy_date"]))
5866+
self.assertEqual(40, len(response.data["hash"]))
5867+
self.assertIn("tag", response.data)

pokemon_v2/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
router.register(r"location", LocationResource)
3939
router.register(r"location-area", LocationAreaResource)
4040
router.register(r"machine", MachineResource)
41+
router.register(r"meta", PokeapiMetaViewset, basename="meta")
4142
router.register(r"move", MoveResource)
4243
router.register(r"move-ailment", MoveMetaAilmentResource)
4344
router.register(r"move-battle-style", MoveBattleStyleResource)

0 commit comments

Comments
 (0)