implement api return status code

This commit is contained in:
simon 2022-01-11 16:16:28 +07:00
parent 52a54fbe31
commit 8d5b4ac242
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4

View File

@ -40,7 +40,11 @@ class ApiBaseView(APIView):
url = f"{es_url}{self.search_base}{document_id}" url = f"{es_url}{self.search_base}{document_id}"
print(url) print(url)
response = requests.get(url, auth=self.context["es_auth"]) response = requests.get(url, auth=self.context["es_auth"])
self.response["data"] = response.json()["_source"] try:
self.response["data"] = response.json()["_source"]
except KeyError:
print(f"item not found: {document_id}")
self.response["data"] = False
self.status_code = response.status_code self.status_code = response.status_code
def get_paginate(self): def get_paginate(self):
@ -70,7 +74,7 @@ class VideoApiView(ApiBaseView):
"""get request""" """get request"""
self.config_builder() self.config_builder()
self.get_document(video_id) self.get_document(video_id)
return Response(self.response) return Response(self.response, status=self.status_code)
class ChannelApiView(ApiBaseView): class ChannelApiView(ApiBaseView):
@ -85,7 +89,7 @@ class ChannelApiView(ApiBaseView):
"""get request""" """get request"""
self.config_builder() self.config_builder()
self.get_document(channel_id) self.get_document(channel_id)
return Response(self.response) return Response(self.response, status=self.status_code)
class PlaylistApiView(ApiBaseView): class PlaylistApiView(ApiBaseView):
@ -100,7 +104,7 @@ class PlaylistApiView(ApiBaseView):
"""get request""" """get request"""
self.config_builder() self.config_builder()
self.get_document(playlist_id) self.get_document(playlist_id)
return Response(self.response) return Response(self.response, status=self.status_code)
class DownloadApiView(ApiBaseView): class DownloadApiView(ApiBaseView):
@ -115,7 +119,7 @@ class DownloadApiView(ApiBaseView):
"""get request""" """get request"""
self.config_builder() self.config_builder()
self.get_document(video_id) self.get_document(video_id)
return Response(self.response) return Response(self.response, status=self.status_code)
class DownloadApiListView(ApiBaseView): class DownloadApiListView(ApiBaseView):