new django api app, implementing basic get views

This commit is contained in:
simon 2022-01-10 22:51:52 +07:00
parent edc9f3fe15
commit 917e73ec4d
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
13 changed files with 167 additions and 0 deletions

View File

@ -0,0 +1,14 @@
# TubeArchivist API endpoints
Documentation of available API endpoints
## Videos
/api/video/\<video_id>/
## Channels
/api/channel/\<channel_id>/
## Playlists
/api/playlist/\<playlist_id>/
## Download Queue
/api/download/\<video_id>/

View File

View File

@ -0,0 +1,3 @@
from django.contrib import admin # noqa: F401
# Register your models here.

10
tubearchivist/api/apps.py Normal file
View File

@ -0,0 +1,10 @@
"""apps file for api package"""
from django.apps import AppConfig
class ApiConfig(AppConfig):
"""app config"""
default_auto_field = "django.db.models.BigAutoField"
name = "api"

View File

View File

@ -0,0 +1,5 @@
"""api models"""
from django.db import models # noqa: F401
# Create your models here.

View File

View File

@ -0,0 +1,3 @@
from django.test import TestCase # noqa: F401
# Create your tests here.

33
tubearchivist/api/urls.py Normal file
View File

@ -0,0 +1,33 @@
"""all api urls"""
from api.views import (
ChannelApiView,
DownloadApiView,
PlaylistApiView,
VideoApiView,
)
from django.contrib.auth.decorators import login_required
from django.urls import path
urlpatterns = [
path(
"video/<slug:video_id>/",
login_required(VideoApiView.as_view()),
name="api-video",
),
path(
"channel/<slug:channel_id>/",
login_required(ChannelApiView.as_view()),
name="api-channel",
),
path(
"playlist/<slug:playlist_id>/",
login_required(PlaylistApiView.as_view()),
name="api-playlist",
),
path(
"download/<slug:video_id>/",
login_required(DownloadApiView.as_view()),
name="api-download",
),
]

View File

@ -0,0 +1,95 @@
"""all API views"""
import requests
from home.src.config import AppConfig
from rest_framework.response import Response
from rest_framework.views import APIView
class ApiBaseView(APIView):
"""base view to inherit from"""
search_base = False
def __init__(self):
super().__init__()
self.response = False
self.status_code = False
self.context = False
def config_builder(self):
"""build confic context"""
default_conf = AppConfig().config
self.context = {
"es_url": default_conf["application"]["es_url"],
"es_auth": default_conf["application"]["es_auth"],
}
def get_document(self, document_id):
"""get single document from es"""
es_url = self.context["es_url"]
url = f"{es_url}{self.search_base}{document_id}"
print(url)
response = requests.get(url, auth=self.context["es_auth"])
self.response = response.json()["_source"]
self.status_code = response.status_code
class VideoApiView(ApiBaseView):
"""resolves to /api/video/<video_id>/
GET: returns metadata dict of video
"""
search_base = "/ta_video/_doc/"
def get(self, request, video_id):
# pylint: disable=unused-argument
"""get request"""
self.config_builder()
self.get_document(video_id)
return Response(self.response)
class ChannelApiView(ApiBaseView):
"""resolves to /api/channel/<channel_id>/
GET: returns metadata dict of channel
"""
search_base = "/ta_channel/_doc/"
def get(self, request, channel_id):
# pylint: disable=unused-argument
"""get request"""
self.config_builder()
self.get_document(channel_id)
return Response(self.response)
class PlaylistApiView(ApiBaseView):
"""resolves to /api/playlist/<playlist_id>/
GET: returns metadata dict of playlist
"""
search_base = "/ta_playlist/_doc/"
def get(self, request, playlist_id):
# pylint: disable=unused-argument
"""get request"""
self.config_builder()
self.get_document(playlist_id)
return Response(self.response)
class DownloadApiView(ApiBaseView):
"""resolves to /api/download/<video_id>/
GET: returns metadata dict of an item in the download queue
"""
search_base = "/ta_download/_doc/"
def get(self, request, video_id):
# pylint: disable=unused-argument
"""get request"""
self.config_builder()
self.get_document(video_id)
return Response(self.response)

View File

@ -44,6 +44,8 @@ INSTALLED_APPS = [
"whitenoise.runserver_nostatic", "whitenoise.runserver_nostatic",
"django.contrib.staticfiles", "django.contrib.staticfiles",
"django.contrib.humanize", "django.contrib.humanize",
"rest_framework",
"api",
] ]
MIDDLEWARE = [ MIDDLEWARE = [

View File

@ -18,5 +18,6 @@ from django.urls import include, path
urlpatterns = [ urlpatterns = [
path("", include("home.urls")), path("", include("home.urls")),
path("api/", include("api.urls")),
path("admin/", admin.site.urls), path("admin/", admin.site.urls),
] ]

View File

@ -1,6 +1,7 @@
beautifulsoup4==4.10.0 beautifulsoup4==4.10.0
celery==5.2.3 celery==5.2.3
Django==4.0.1 Django==4.0.1
djangorestframework==3.13.1
Pillow==9.0.0 Pillow==9.0.0
redis==4.1.0 redis==4.1.0
requests==2.27.1 requests==2.27.1