mirror of
https://github.com/tubearchivist/tubearchivist-frontend.git
synced 2024-11-22 03:40:14 +00:00
new django api app, implementing basic get views
This commit is contained in:
parent
edc9f3fe15
commit
917e73ec4d
14
tubearchivist/api/README.md
Normal file
14
tubearchivist/api/README.md
Normal 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>/
|
0
tubearchivist/api/__init__.py
Normal file
0
tubearchivist/api/__init__.py
Normal file
3
tubearchivist/api/admin.py
Normal file
3
tubearchivist/api/admin.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.contrib import admin # noqa: F401
|
||||
|
||||
# Register your models here.
|
10
tubearchivist/api/apps.py
Normal file
10
tubearchivist/api/apps.py
Normal 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"
|
0
tubearchivist/api/migrations/__init__.py
Normal file
0
tubearchivist/api/migrations/__init__.py
Normal file
5
tubearchivist/api/models.py
Normal file
5
tubearchivist/api/models.py
Normal file
@ -0,0 +1,5 @@
|
||||
"""api models"""
|
||||
|
||||
from django.db import models # noqa: F401
|
||||
|
||||
# Create your models here.
|
0
tubearchivist/api/serializers.py
Normal file
0
tubearchivist/api/serializers.py
Normal file
3
tubearchivist/api/tests.py
Normal file
3
tubearchivist/api/tests.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.test import TestCase # noqa: F401
|
||||
|
||||
# Create your tests here.
|
33
tubearchivist/api/urls.py
Normal file
33
tubearchivist/api/urls.py
Normal 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",
|
||||
),
|
||||
]
|
95
tubearchivist/api/views.py
Normal file
95
tubearchivist/api/views.py
Normal 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)
|
@ -44,6 +44,8 @@ INSTALLED_APPS = [
|
||||
"whitenoise.runserver_nostatic",
|
||||
"django.contrib.staticfiles",
|
||||
"django.contrib.humanize",
|
||||
"rest_framework",
|
||||
"api",
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
@ -18,5 +18,6 @@ from django.urls import include, path
|
||||
|
||||
urlpatterns = [
|
||||
path("", include("home.urls")),
|
||||
path("api/", include("api.urls")),
|
||||
path("admin/", admin.site.urls),
|
||||
]
|
||||
|
@ -1,6 +1,7 @@
|
||||
beautifulsoup4==4.10.0
|
||||
celery==5.2.3
|
||||
Django==4.0.1
|
||||
djangorestframework==3.13.1
|
||||
Pillow==9.0.0
|
||||
redis==4.1.0
|
||||
requests==2.27.1
|
||||
|
Loading…
Reference in New Issue
Block a user