[API] add channel search endpoint

This commit is contained in:
Simon 2023-08-24 22:46:35 +07:00
parent 15794ebfc8
commit f1e25c9a20
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
2 changed files with 35 additions and 0 deletions

View File

@ -41,6 +41,11 @@ urlpatterns = [
views.ChannelApiListView.as_view(),
name="api-channel-list",
),
path(
"channel/search/",
views.ChannelApiSearchView.as_view(),
name="api-channel-search",
),
path(
"channel/<slug:channel_id>/",
views.ChannelApiView.as_view(),

View File

@ -356,6 +356,36 @@ class ChannelApiListView(ApiBaseView):
)
class ChannelApiSearchView(ApiBaseView):
"""resolves to /api/channel/search/
search for channel
"""
search_base = "ta_channel/_doc/"
def get(self, request):
"""handle get request, search with s parameter"""
query = request.GET.get("q")
if not query:
message = "missing expected q parameter"
return Response({"message": message, "data": False}, status=400)
try:
parsed = Parser(query).parse()[0]
except (ValueError, IndexError, AttributeError):
message = f"channel not found: {query}"
return Response({"message": message, "data": False}, status=404)
if not parsed["type"] == "channel":
message = "expected type channel"
return Response({"message": message, "data": False}, status=400)
self.get_document(parsed["url"])
return Response(self.response, status=self.status_code)
class ChannelApiVideoView(ApiBaseView):
"""resolves to /api/channel/<channel-id>/video
GET: returns a list of videos of channel