refac, useApiClient for loader and actions

This commit is contained in:
Simon 2025-01-07 23:13:48 +07:00
parent d33917b541
commit daf09fcc33
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
64 changed files with 221 additions and 1248 deletions

View File

@ -129,7 +129,7 @@ class VideoCommentView(ApiBaseView):
# pylint: disable=unused-argument
self.get_document(video_id)
return Response(self.response, status=self.status_code)
return Response(self.response, status=200)
class VideoSimilarView(ApiBaseView):
@ -153,4 +153,4 @@ class VideoSimilarView(ApiBaseView):
},
}
self.get_document_list(request, pagination=False)
return Response(self.response, status=self.status_code)
return Response(self.response, status=200)

View File

@ -1,8 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
export type AppriseTaskNameType =
| 'update_subscribed'
@ -11,26 +7,10 @@ export type AppriseTaskNameType =
| 'check_reindex';
const createAppriseNotificationUrl = async (taskName: AppriseTaskNameType, url: string) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/task/notification/`, {
return APIClient('/api/task/notification/', {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body: JSON.stringify({ task_name: taskName, url }),
body: { task_name: taskName, url },
});
const appriseNotificationUrl = await response.json();
if (isDevEnvironment()) {
console.log('createAppriseNotificationUrl', appriseNotificationUrl);
}
return appriseNotificationUrl;
};
export default createAppriseNotificationUrl;

View File

@ -1,30 +1,10 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const createCustomPlaylist = async (playlistId: string) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/playlist/`, {
return APIClient('/api/playlist/', {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body: JSON.stringify({ data: { create: playlistId } }),
body: { data: { create: playlistId } },
});
const customPlaylist = await response.json();
if (isDevEnvironment()) {
console.log('createCustomPlaylist', customPlaylist);
}
return customPlaylist;
};
export default createCustomPlaylist;

View File

@ -1,8 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
export type TaskScheduleNameType =
| 'update_subscribed'
@ -28,26 +24,10 @@ type ScheduleConfigType = {
};
const createTaskSchedule = async (taskName: TaskScheduleNameType, schedule: ScheduleConfigType) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/task/schedule/${taskName}/`, {
return APIClient(`/api/task/schedule/${taskName}/`, {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body: JSON.stringify(schedule),
body: schedule,
});
const scheduledTask = await response.json();
if (isDevEnvironment()) {
console.log('createTaskSchedule', scheduledTask);
}
return scheduledTask;
};
export default createTaskSchedule;

View File

@ -1,28 +1,9 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const deleteApiToken = async () => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/appsettings/token/`, {
return APIClient('/api/appsettings/token/', {
method: 'DELETE',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
});
const resetToken = await response.json();
if (isDevEnvironment()) {
console.log('deleteApiToken', resetToken);
}
return resetToken;
};
export default deleteApiToken;

View File

@ -1,8 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
type AppriseTaskNameType =
| 'update_subscribed'
@ -11,26 +7,10 @@ type AppriseTaskNameType =
| 'check_reindex';
const deleteAppriseNotificationUrl = async (taskName: AppriseTaskNameType) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/task/notification/`, {
return APIClient('/api/task/notification/', {
method: 'DELETE',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body: JSON.stringify({ task_name: taskName }),
body: { task_name: taskName },
});
const appriseNotification = await response.json();
if (isDevEnvironment()) {
console.log('deleteAppriseNotificationUrl', appriseNotification);
}
return appriseNotification;
};
export default deleteAppriseNotificationUrl;

View File

@ -1,29 +1,9 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const deleteChannel = async (channelId: string) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/channel/${channelId}/`, {
return APIClient(`/api/channel/${channelId}/`, {
method: 'DELETE',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
});
const channelDeleted = await response.json();
if (isDevEnvironment()) {
console.log('deleteChannel', channelDeleted);
}
return channelDeleted;
};
export default deleteChannel;

View File

@ -1,29 +1,9 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const deleteDownloadById = async (youtubeId: string) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/download/${youtubeId}/`, {
return APIClient(`/api/download/${youtubeId}/`, {
method: 'DELETE',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
});
const downloadState = await response.json();
if (isDevEnvironment()) {
console.log('deleteDownloadById', downloadState);
}
return downloadState;
};
export default deleteDownloadById;

View File

@ -1,37 +1,14 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
type FilterType = 'ignore' | 'pending';
const deleteDownloadQueueByFilter = async (filter: FilterType) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const searchParams = new URLSearchParams();
if (filter) searchParams.append('filter', filter);
if (filter) {
searchParams.append('filter', filter);
}
const response = await fetch(`${apiUrl}/api/download/?${searchParams.toString()}`, {
return APIClient(`/api/download/?${searchParams.toString()}`, {
method: 'DELETE',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
});
const downloadState = await response.json();
if (isDevEnvironment()) {
console.log('deleteDownloadQueueByFilter', downloadState);
}
return downloadState;
};
export default deleteDownloadQueueByFilter;

View File

@ -1,34 +1,14 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const deletePlaylist = async (playlistId: string, allVideos = false) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
let params = '';
if (allVideos) {
params = '?delete-videos=true';
}
const response = await fetch(`${apiUrl}/api/playlist/${playlistId}/${params}`, {
return APIClient(`/api/playlist/${playlistId}/${params}`, {
method: 'DELETE',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
});
const playlistDeleted = await response.json();
if (isDevEnvironment()) {
console.log('deletePlaylist', playlistDeleted);
}
return playlistDeleted;
};
export default deletePlaylist;

View File

@ -1,30 +1,10 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
import { TaskScheduleNameType } from './createTaskSchedule';
const deleteTaskSchedule = async (taskName: TaskScheduleNameType) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/task/schedule/${taskName}/`, {
return APIClient(`/api/task/schedule/${taskName}/`, {
method: 'DELETE',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
});
const scheduledTask = await response.json();
if (isDevEnvironment()) {
console.log('deleteTaskSchedule', scheduledTask);
}
return scheduledTask;
};
export default deleteTaskSchedule;

View File

@ -1,29 +1,9 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const deleteVideo = async (videoId: string) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/video/${videoId}/`, {
return APIClient(`/api/video/${videoId}/`, {
method: 'DELETE',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
});
const videoDeleted = await response.json();
if (isDevEnvironment()) {
console.log('deleteVideo', videoDeleted);
}
return videoDeleted;
};
export default deleteVideo;

View File

@ -1,29 +1,9 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const deleteVideoProgressById = async (youtubeId: string) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/video/${youtubeId}/progress/`, {
return APIClient(`/api/video/${youtubeId}/progress/`, {
method: 'DELETE',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
});
const watchedState = await response.json();
if (isDevEnvironment()) {
console.log('deleteVideoProgressById', watchedState);
}
return watchedState;
};
export default deleteVideoProgressById;

View File

@ -1,22 +1,9 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import APIClient from '../../functions/APIClient';
const logOut = async () => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/user/logout/`, {
return APIClient('/api/user/logout/', {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
});
return response;
};
export default logOut;

View File

@ -1,29 +1,9 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const queueBackup = async () => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/appsettings/backup/`, {
return APIClient('/api/appsettings/backup/', {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
});
const backupQueued = await response.json();
if (isDevEnvironment()) {
console.log('queueBackup', backupQueued);
}
return backupQueued;
};
export default queueBackup;

View File

@ -1,8 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
export type ReindexType = 'channel' | 'video' | 'playlist';
@ -13,36 +9,15 @@ export const ReindexTypeEnum = {
};
const queueReindex = async (id: string, type: ReindexType, reindexVideos = false) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
let params = '';
if (reindexVideos) {
params = '?extract_videos=true';
}
const body = JSON.stringify({
[type]: [id],
});
const response = await fetch(`${apiUrl}/api/refresh/${params}`, {
return APIClient(`/api/refresh/${params}`, {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body,
body: { [type]: [id] },
});
const channelDeleted = await response.json();
if (isDevEnvironment()) {
console.log('queueReindex', channelDeleted);
}
return channelDeleted;
};
export default queueReindex;

View File

@ -1,29 +1,9 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const queueSnapshot = async () => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/appsettings/snapshot/`, {
return APIClient('/api/appsettings/snapshot/', {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
});
const snapshotQueued = await response.json();
if (isDevEnvironment()) {
console.log('queueSnapshot', snapshotQueued);
}
return snapshotQueued;
};
export default queueSnapshot;

View File

@ -1,29 +1,9 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const restoreBackup = async (fileName: string) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/appsettings/backup/${fileName}/`, {
return APIClient(`/api/appsettings/backup/${fileName}/`, {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
});
const backupRestored = await response.json();
if (isDevEnvironment()) {
console.log('restoreBackup', backupRestored);
}
return backupRestored;
};
export default restoreBackup;

View File

@ -1,29 +1,9 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const restoreSnapshot = async (snapshotId: string) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/appsettings/snapshot/${snapshotId}/`, {
return APIClient(`/api/appsettings/snapshot/${snapshotId}/`, {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
});
const backupRestored = await response.json();
if (isDevEnvironment()) {
console.log('restoreSnapshot', backupRestored);
}
return backupRestored;
};
export default restoreSnapshot;

View File

@ -12,6 +12,7 @@ export type LoginResponseType = {
};
const signIn = async (username: string, password: string, saveLogin: boolean) => {
// works differently, response status is checked
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');

View File

@ -1,27 +1,10 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import APIClient from '../../functions/APIClient';
const stopTaskByName = async (taskId: string) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/task/by-id/${taskId}/`, {
APIClient(`/api/task/by-id/${taskId}/`, {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body: JSON.stringify({ command: 'stop' }),
body: { command: 'stop' },
});
const downloadQueueState = await response.json();
console.log('stopTaskByName', downloadQueueState);
return downloadQueueState;
};
export default stopTaskByName;

View File

@ -1,7 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import APIClient from '../../functions/APIClient';
import { AppSettingsConfigType } from '../loader/loadAppsettingsConfig';
type ObjectType = Record<
@ -32,23 +29,10 @@ function flattenObject(ob: ObjectType) {
}
const updateAppsettingsConfig = async (config: AppSettingsConfigType) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/appsettings/config/`, {
return APIClient('/api/appsettings/config/', {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body: JSON.stringify(flattenObject(config)),
body: flattenObject(config),
});
const appSettingsConfig = await response.json();
console.log('updateAppsettingsConfig', appSettingsConfig);
return appSettingsConfig;
};
export default updateAppsettingsConfig;

View File

@ -1,37 +1,20 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import APIClient from '../../functions/APIClient';
const updateChannelOverwrites = async (
channelId: string,
configKey: string,
configValue: string | boolean | number | null,
) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const data = {
channel_overwrites: {
[configKey]: configValue,
},
};
const response = await fetch(`${apiUrl}/api/channel/${channelId}/`, {
return APIClient(`/api/channel/${channelId}/`, {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body: JSON.stringify(data),
body: data,
});
const channelSubscription = await response.json();
console.log('updateChannelOverwrites', channelSubscription);
return channelSubscription;
};
export default updateChannelOverwrites;

View File

@ -1,12 +1,6 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import APIClient from '../../functions/APIClient';
const updateChannelSubscription = async (channelIds: string, status: boolean) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const channels = [];
const containsMultiple = channelIds.includes('\n');
@ -20,23 +14,10 @@ const updateChannelSubscription = async (channelIds: string, status: boolean) =>
channels.push({ channel_id: channelIds, channel_subscribed: status });
}
const response = await fetch(`${apiUrl}/api/channel/`, {
return APIClient('/api/channel/', {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body: JSON.stringify({
data: [...channels],
}),
body: { data: [...channels] },
});
const channelSubscription = await response.json();
console.log('updateChannelSubscription', channelSubscription);
return channelSubscription;
};
export default updateChannelSubscription;

View File

@ -1,7 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import APIClient from '../../functions/APIClient';
export type ValidatedCookieType = {
cookie_enabled: boolean;
@ -12,22 +9,9 @@ export type ValidatedCookieType = {
};
const updateCookie = async (): Promise<ValidatedCookieType> => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/appsettings/cookie/`, {
return APIClient('/api/appsettings/cookie/', {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
});
const validatedCookie = await response.json();
console.log('updateCookie', validatedCookie);
return validatedCookie;
};
export default updateCookie;

View File

@ -1,7 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import APIClient from '../../functions/APIClient';
type CustomPlaylistActionType = 'create' | 'up' | 'down' | 'top' | 'bottom' | 'remove';
@ -10,24 +7,10 @@ const updateCustomPlaylist = async (
playlistId: string,
videoId: string,
) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/playlist/${playlistId}/`, {
return APIClient(`/api/playlist/${playlistId}/`, {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body: JSON.stringify({ action, video_id: videoId }),
body: { action, video_id: videoId },
});
const customPlaylist = await response.json();
console.log('updateCustomPlaylist', action, customPlaylist);
return customPlaylist;
};
export default updateCustomPlaylist;

View File

@ -1,12 +1,6 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import APIClient from '../../functions/APIClient';
const updateDownloadQueue = async (youtubeIdStrings: string, autostart: boolean) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const urls = [];
const containsMultiple = youtubeIdStrings.includes('\n');
@ -25,23 +19,10 @@ const updateDownloadQueue = async (youtubeIdStrings: string, autostart: boolean)
params = '?autostart=true';
}
const response = await fetch(`${apiUrl}/api/download/${params}`, {
return APIClient(`/api/download/${params}`, {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body: JSON.stringify({
data: [...urls],
}),
body: { data: [...urls] },
});
const downloadState = await response.json();
console.log('updateDownloadQueue', downloadState);
return downloadState;
};
export default updateDownloadQueue;

View File

@ -1,31 +1,12 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import APIClient from '../../functions/APIClient';
export type DownloadQueueStatus = 'ignore' | 'pending' | 'priority';
const updateDownloadQueueStatusById = async (youtubeId: string, status: DownloadQueueStatus) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/download/${youtubeId}/`, {
return APIClient(`/api/download/${youtubeId}/`, {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body: JSON.stringify({
status,
}),
body: { status: status },
});
const downloadQueueState = await response.json();
console.log('updateDownloadQueueStatusById', downloadQueueState);
return downloadQueueState;
};
export default updateDownloadQueueStatusById;

View File

@ -1,12 +1,6 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import APIClient from '../../functions/APIClient';
const updatePlaylistSubscription = async (playlistIds: string, status: boolean) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const playlists = [];
const containsMultiple = playlistIds.includes('\n');
@ -20,23 +14,10 @@ const updatePlaylistSubscription = async (playlistIds: string, status: boolean)
playlists.push({ playlist_id: playlistIds, playlist_subscribed: status });
}
const response = await fetch(`${apiUrl}/api/playlist/`, {
return APIClient('/api/playlist/', {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body: JSON.stringify({
data: [...playlists],
}),
body: { data: [...playlists] },
});
const playlistSubscription = await response.json();
console.log('updatePlaylistSubscription', playlistSubscription);
return playlistSubscription;
};
export default updatePlaylistSubscription;

View File

@ -1,7 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import APIClient from '../../functions/APIClient';
type TaskNamesType =
| 'download_pending'
@ -11,22 +8,9 @@ type TaskNamesType =
| 'rescan_filesystem';
const updateTaskByName = async (taskName: TaskNamesType) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/task/by-name/${taskName}/`, {
return APIClient(`/api/task/by-name/${taskName}/`, {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
});
const downloadQueueState = await response.json();
console.log('updateTaskByName', downloadQueueState);
return downloadQueueState;
};
export default updateTaskByName;

View File

@ -1,8 +1,5 @@
import { SortByType, SortOrderType, ViewLayoutType } from '../../pages/Home';
import getApiUrl from '../../configuration/getApiUrl';
import defaultHeaders from '../../configuration/defaultHeaders';
import getCookie from '../../functions/getCookie';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import APIClient from '../../functions/APIClient';
export type UserMeType = {
id: number;
@ -33,24 +30,10 @@ export type UserConfigType = {
};
const updateUserConfig = async (config: Partial<UserConfigType>): Promise<UserConfigType> => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/user/me/`, {
return APIClient('/api/user/me/', {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body: JSON.stringify({ config }),
body: { config: config },
});
const userConfig = await response.json();
console.log('updateUserConfig', userConfig);
return userConfig;
};
export default updateUserConfig;

View File

@ -1,7 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import APIClient from '../../functions/APIClient';
type VideoProgressProp = {
youtubeId: string;
@ -9,26 +6,10 @@ type VideoProgressProp = {
};
const updateVideoProgressById = async ({ youtubeId, currentProgress }: VideoProgressProp) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
const response = await fetch(`${apiUrl}/api/video/${youtubeId}/progress/`, {
return APIClient(`/api/video/${youtubeId}/progress/`, {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body: JSON.stringify({
position: currentProgress,
}),
body: { position: currentProgress },
});
const userConfig = await response.json();
console.log('updateVideoProgressById', userConfig);
return userConfig;
};
export default updateVideoProgressById;

View File

@ -1,7 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import APIClient from '../../functions/APIClient';
import deleteVideoProgressById from './deleteVideoProgressById';
export type Watched = {
@ -10,28 +7,14 @@ export type Watched = {
};
const updateWatchedState = async (watched: Watched) => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
if (watched.is_watched) {
await deleteVideoProgressById(watched.id);
}
const response = await fetch(`${apiUrl}/api/watched/`, {
return APIClient('/api/watched/', {
method: 'POST',
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
body: JSON.stringify(watched),
body: watched,
});
const watchedState = await response.json();
console.log('updateWatchedState', watchedState);
return watchedState;
};
export default updateWatchedState;

View File

@ -1,36 +1,11 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
type ApiTokenResponse = {
token: string;
};
const loadApiToken = async (): Promise<ApiTokenResponse> => {
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');
try {
const response = await fetch(`${apiUrl}/api/appsettings/token/`, {
headers: {
...defaultHeaders,
'X-CSRFToken': csrfCookie || '',
},
credentials: getFetchCredentials(),
});
const apiToken = await response.json();
if (isDevEnvironment()) {
console.log('loadApiToken', apiToken);
}
return apiToken;
} catch {
return { token: '' };
}
return APIClient('/api/appsettings/token/');
};
export default loadApiToken;

View File

@ -1,7 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
export type AppriseNotificationType = {
check_reindex?: {
@ -23,20 +20,7 @@ export type AppriseNotificationType = {
};
const loadAppriseNotification = async (): Promise<AppriseNotificationType> => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/task/notification/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const notification = await response.json();
if (isDevEnvironment()) {
console.log('loadAppriseNotification', notification);
}
return notification;
return APIClient('/api/task/notification/');
};
export default loadAppriseNotification;

View File

@ -1,7 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
export type AppSettingsConfigType = {
subscriptions: {
@ -35,20 +32,7 @@ export type AppSettingsConfigType = {
};
const loadAppsettingsConfig = async (): Promise<AppSettingsConfigType> => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/appsettings/config/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const appSettingsConfig = await response.json();
if (isDevEnvironment()) {
console.log('loadApplicationConfig', appSettingsConfig);
}
return appSettingsConfig;
return APIClient('/api/appsettings/config/');
};
export default loadAppsettingsConfig;

View File

@ -4,6 +4,7 @@ import getFetchCredentials from '../../configuration/getFetchCredentials';
import getCookie from '../../functions/getCookie';
const loadAuth = async () => {
// works differently, return response to check for status in main.tsx
const apiUrl = getApiUrl();
const csrfCookie = getCookie('csrftoken');

View File

@ -1,23 +1,7 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const loadBackupList = async () => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/appsettings/backup/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const backupList = await response.json();
if (isDevEnvironment()) {
console.log('loadBackupList', backupList);
}
return backupList;
return APIClient('/api/appsettings/backup/');
};
export default loadBackupList;

View File

@ -1,7 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
export type ChannelAggsType = {
total_items: {
@ -17,20 +14,7 @@ export type ChannelAggsType = {
};
const loadChannelAggs = async (channelId: string): Promise<ChannelAggsType> => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/channel/${channelId}/aggs/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const channel = await response.json();
if (isDevEnvironment()) {
console.log('loadChannelAggs', channel);
}
return channel;
return APIClient(`/api/channel/${channelId}/aggs/`);
};
export default loadChannelAggs;

View File

@ -1,23 +1,7 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const loadChannelById = async (youtubeChannelId: string) => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/channel/${youtubeChannelId}/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const channel = await response.json();
if (isDevEnvironment()) {
console.log('loadChannelById', channel);
}
return channel;
return APIClient(`/api/channel/${youtubeChannelId}/`);
};
export default loadChannelById;

View File

@ -1,33 +1,14 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const loadChannelList = async (page: number, showSubscribed: boolean) => {
const apiUrl = getApiUrl();
const searchParams = new URLSearchParams();
if (page) {
searchParams.append('page', page.toString());
}
if (page) searchParams.append('page', page.toString());
if (showSubscribed) searchParams.append('filter', 'subscribed');
if (showSubscribed) {
searchParams.append('filter', 'subscribed');
}
const endpoint = `/api/channel/${searchParams.toString() ? `?${searchParams.toString()}` : ''}`;
const response = await fetch(`${apiUrl}/api/channel/?${searchParams.toString()}`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const channels = await response.json();
if (isDevEnvironment()) {
console.log('loadChannelList', channels);
}
return channels;
return APIClient(endpoint);
};
export default loadChannelList;

View File

@ -1,7 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
export type ChannelNavResponseType = {
has_streams: boolean;
@ -11,20 +8,7 @@ export type ChannelNavResponseType = {
};
const loadChannelNav = async (youtubeChannelId: string): Promise<ChannelNavResponseType> => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/channel/${youtubeChannelId}/nav/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const channel = await response.json();
if (isDevEnvironment()) {
console.log('loadChannelNav', channel);
}
return channel;
return APIClient(`/api/channel/${youtubeChannelId}/nav/`);
};
export default loadChannelNav;

View File

@ -1,23 +1,7 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const loadCommentsbyVideoId = async (youtubeId: string) => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/video/${youtubeId}/comment/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const comments = await response.json();
if (isDevEnvironment()) {
console.log('loadCommentsbyVideoId', comments);
}
return comments;
return APIClient(`/api/video/${youtubeId}/comment/`);
};
export default loadCommentsbyVideoId;

View File

@ -1,7 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
type DownloadAggsBucket = {
key: string[];
@ -18,20 +15,7 @@ export type DownloadAggsType = {
};
const loadDownloadAggs = async (): Promise<DownloadAggsType> => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/download/aggs/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const downloadAggs = await response.json();
if (isDevEnvironment()) {
console.log('loadDownloadAggs', downloadAggs);
}
return downloadAggs;
return APIClient('/api/download/aggs/');
};
export default loadDownloadAggs;

View File

@ -1,7 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
import { DownloadResponseType } from '../../pages/Download';
const loadDownloadQueue = async (
@ -9,32 +6,15 @@ const loadDownloadQueue = async (
channelId: string | null,
showIgnored: boolean,
): Promise<DownloadResponseType> => {
const apiUrl = getApiUrl();
const searchParams = new URLSearchParams();
if (page) {
searchParams.append('page', page.toString());
}
if (channelId) {
searchParams.append('channel', channelId);
}
if (page) searchParams.append('page', page.toString());
if (channelId) searchParams.append('channel', channelId);
searchParams.append('filter', showIgnored ? 'ignore' : 'pending');
const response = await fetch(`${apiUrl}/api/download/?${searchParams.toString()}`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const endpoint = `/api/download/${searchParams.toString() ? `?${searchParams.toString()}` : ''}`;
const playlist = await response.json();
if (isDevEnvironment()) {
console.log('loadDownloadQueue', playlist);
}
return playlist;
return APIClient(endpoint);
};
export default loadDownloadQueue;

View File

@ -1,30 +1,16 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
export type NotificationPages = 'download' | 'settings' | 'channel' | 'all';
const loadNotifications = async (pageName: NotificationPages, includeReindex = false) => {
const apiUrl = getApiUrl();
const searchParams = new URLSearchParams();
let params = '';
if (!includeReindex && pageName !== 'all') {
params = `?filter=${pageName}`;
searchParams.append('filter', pageName);
}
const response = await fetch(`${apiUrl}/api/notification/${params}`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const notifications = await response.json();
if (isDevEnvironment()) {
console.log('loadNotifications', notifications);
}
return notifications;
const endpoint = `/api/notification/${searchParams.toString() ? `?${searchParams.toString()}` : ''}`;
return APIClient(endpoint);
};
export default loadNotifications;

View File

@ -1,23 +1,7 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const loadPlaylistById = async (playlistId: string | undefined) => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/playlist/${playlistId}/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const videos = await response.json();
if (isDevEnvironment()) {
console.log('loadPlaylistById', videos);
}
return videos;
return APIClient(`/api/playlist/${playlistId}/`);
};
export default loadPlaylistById;

View File

@ -1,7 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
type PlaylistType = 'regular' | 'custom';
@ -13,38 +10,15 @@ type LoadPlaylistListProps = {
};
const loadPlaylistList = async ({ channel, page, subscribed, type }: LoadPlaylistListProps) => {
const apiUrl = getApiUrl();
const searchParams = new URLSearchParams();
if (channel) {
searchParams.append('channel', channel);
}
if (channel) searchParams.append('channel', channel);
if (page) searchParams.append('page', page.toString());
if (subscribed) searchParams.append('subscribed', subscribed.toString());
if (type) searchParams.append('type', type);
if (page) {
searchParams.append('page', page.toString());
}
if (subscribed) {
searchParams.append('subscribed', subscribed.toString());
}
if (type) {
searchParams.append('type', type);
}
const response = await fetch(`${apiUrl}/api/playlist/?${searchParams.toString()}`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const playlist = await response.json();
if (isDevEnvironment()) {
console.log('loadPlaylistList', playlist);
}
return playlist;
const endpoint = `/api/playlist/${searchParams.toString() ? `?${searchParams.toString()}` : ''}`;
return APIClient(endpoint);
};
export default loadPlaylistList;

View File

@ -1,7 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
type ScheduleType = {
name: string;
@ -17,20 +14,7 @@ type ScheduleType = {
export type ScheduleResponseType = ScheduleType[];
const loadSchedule = async (): Promise<ScheduleResponseType> => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/task/schedule/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const schedule = await response.json();
if (isDevEnvironment()) {
console.log('loadSchedule', schedule);
}
return schedule;
return APIClient('/api/task/schedule/');
};
export default loadSchedule;

View File

@ -1,23 +1,7 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const loadSearch = async (query: string) => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/search/?query=${query}`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const searchResults = await response.json();
if (isDevEnvironment()) {
console.log('loadSearch', searchResults);
}
return searchResults;
return APIClient(`/api/search/?query=${query}`);
};
export default loadSearch;

View File

@ -1,23 +1,7 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const loadSimmilarVideosById = async (youtubeId: string) => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/video/${youtubeId}/similar/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const videos = await response.json();
if (isDevEnvironment()) {
console.log('loadSimmilarVideosById', videos);
}
return videos;
return APIClient(`/api/video/${youtubeId}/similar/`);
};
export default loadSimmilarVideosById;

View File

@ -1,23 +1,7 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const loadSnapshots = async () => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/appsettings/snapshot/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const backupList = await response.json();
if (isDevEnvironment()) {
console.log('loadSnapshots', backupList);
}
return backupList;
return APIClient('/api/appsettings/snapshot/');
};
export default loadSnapshots;

View File

@ -1,28 +1,12 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
type BiggestChannelsOrderType = 'doc_count' | 'duration' | 'media_size';
const loadStatsBiggestChannels = async (order: BiggestChannelsOrderType) => {
const apiUrl = getApiUrl();
const searchParams = new URLSearchParams();
searchParams.append('order', order);
const response = await fetch(`${apiUrl}/api/stats/biggestchannels/?${searchParams.toString()}`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const notifications = await response.json();
if (isDevEnvironment()) {
console.log('loadStatsBiggestChannels', notifications);
}
return notifications;
return APIClient(`/api/stats/biggestchannels/?${searchParams.toString()}`);
};
export default loadStatsBiggestChannels;

View File

@ -1,23 +1,7 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const loadStatsChannel = async () => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/stats/channel/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const notifications = await response.json();
if (isDevEnvironment()) {
console.log('loadStatsChannel', notifications);
}
return notifications;
return APIClient('/api/stats/channel/');
};
export default loadStatsChannel;

View File

@ -1,23 +1,7 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const loadStatsDownload = async () => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/stats/download/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const notifications = await response.json();
if (isDevEnvironment()) {
console.log('loadStatsDownload', notifications);
}
return notifications;
return APIClient('/api/stats/download/');
};
export default loadStatsDownload;

View File

@ -1,23 +1,7 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const loadStatsDownloadHistory = async () => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/stats/downloadhist/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const notifications = await response.json();
if (isDevEnvironment()) {
console.log('loadStatsDownloadHistory', notifications);
}
return notifications;
return APIClient('/api/stats/downloadhist/');
};
export default loadStatsDownloadHistory;

View File

@ -1,23 +1,7 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const loadStatsPlaylist = async () => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/stats/playlist/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const notifications = await response.json();
if (isDevEnvironment()) {
console.log('loadStatsPlaylist', notifications);
}
return notifications;
return APIClient('/api/stats/playlist/');
};
export default loadStatsPlaylist;

View File

@ -1,23 +1,7 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const loadStatsVideo = async () => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/stats/video/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const notifications = await response.json();
if (isDevEnvironment()) {
console.log('loadStatsVideo', notifications);
}
return notifications;
return APIClient('/api/stats/video/');
};
export default loadStatsVideo;

View File

@ -1,23 +1,7 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
const loadStatsWatchProgress = async () => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/stats/watch/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const notifications = await response.json();
if (isDevEnvironment()) {
console.log('loadStatsWatchProgress', notifications);
}
return notifications;
return APIClient('/api/stats/watch/');
};
export default loadStatsWatchProgress;

View File

@ -1,24 +1,8 @@
import { UserMeType } from '../actions/updateUserConfig';
import isDevEnvironment from '../../functions/isDevEnvironment';
import getApiUrl from '../../configuration/getApiUrl';
import defaultHeaders from '../../configuration/defaultHeaders';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import APIClient from '../../functions/APIClient';
const loadUserMeConfig = async (): Promise<UserMeType> => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/user/me/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const userConfig = await response.json();
if (isDevEnvironment()) {
console.log('loadUserMeConfig', userConfig);
}
return userConfig;
return APIClient('/api/user/me/');
};
export default loadUserMeConfig;

View File

@ -1,24 +1,8 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
import { VideoResponseType } from '../../pages/Video';
const loadVideoById = async (youtubeId: string): Promise<VideoResponseType> => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/video/${youtubeId}/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const videos = await response.json();
if (isDevEnvironment()) {
console.log('loadVideoById', videos);
}
return videos;
return APIClient(`/api/video/${youtubeId}/`);
};
export default loadVideoById;

View File

@ -1,9 +1,6 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import { ConfigType, SortByType, SortOrderType, VideoType } from '../../pages/Home';
import { PaginationType } from '../../components/Pagination';
import APIClient from '../../functions/APIClient';
export type VideoListByFilterResponseType = {
data?: VideoType[];
@ -27,48 +24,22 @@ type FilterType = {
const loadVideoListByFilter = async (
filter: FilterType,
): Promise<VideoListByFilterResponseType> => {
const apiUrl = getApiUrl();
const searchParams = new URLSearchParams();
if (filter.page) {
searchParams.append('page', filter.page.toString());
}
if (filter.playlist) {
searchParams.append('playlist', filter.playlist);
} else if (filter.channel) {
searchParams.append('channel', filter.channel);
}
if (filter.watch) {
searchParams.append('watch', filter.watch);
}
if (filter.page) searchParams.append('page', filter.page.toString());
if (filter.watch) searchParams.append('watch', filter.watch);
if (filter.sort) searchParams.append('sort', filter.sort);
if (filter.order) searchParams.append('order', filter.order);
if (filter.type) searchParams.append('type', filter.type);
if (filter.sort) {
searchParams.append('sort', filter.sort);
}
if (filter.order) {
searchParams.append('order', filter.order);
}
if (filter.type) {
searchParams.append('type', filter.type);
}
const response = await fetch(`${apiUrl}/api/video/?${searchParams.toString()}`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const videos = await response.json();
if (isDevEnvironment()) {
console.log('loadVideoListByFilter', filter, videos);
}
return videos;
const endpoint = `/api/video/${searchParams.toString() ? `?${searchParams.toString()}` : ''}`;
return APIClient(endpoint);
};
export default loadVideoListByFilter;

View File

@ -1,7 +1,4 @@
import defaultHeaders from '../../configuration/defaultHeaders';
import getApiUrl from '../../configuration/getApiUrl';
import getFetchCredentials from '../../configuration/getFetchCredentials';
import isDevEnvironment from '../../functions/isDevEnvironment';
import APIClient from '../../functions/APIClient';
export type VideoNavResponseType = {
playlist_meta: {
@ -29,20 +26,7 @@ export type VideoNavResponseType = {
};
const loadVideoNav = async (youtubeVideoId: string): Promise<VideoNavResponseType[]> => {
const apiUrl = getApiUrl();
const response = await fetch(`${apiUrl}/api/video/${youtubeVideoId}/nav/`, {
headers: defaultHeaders,
credentials: getFetchCredentials(),
});
const videoNav = await response.json();
if (isDevEnvironment()) {
console.log('loadVideoNav', videoNav);
}
return videoNav;
return APIClient(`/api/video/${youtubeVideoId}/nav/`);
};
export default loadVideoNav;

View File

@ -0,0 +1,60 @@
import defaultHeaders from '../configuration/defaultHeaders';
import getApiUrl from '../configuration/getApiUrl';
import getFetchCredentials from '../configuration/getFetchCredentials';
import logOut from '../api/actions/logOut';
import getCookie from './getCookie';
export interface ApiClientOptions extends Omit<RequestInit, 'body'> {
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
body?: Record<string, unknown> | string;
}
const APIClient = async (
endpoint: string,
{ method = 'GET', body, headers = {}, ...options }: ApiClientOptions = {},
) => {
const apiUrl = getApiUrl();
const csrfToken = getCookie('csrftoken');
const response = await fetch(`${apiUrl}${endpoint}`, {
method,
headers: {
...defaultHeaders,
...(csrfToken ? { 'X-CSRFToken': csrfToken } : {}),
...headers,
},
credentials: getFetchCredentials(),
body: body ? JSON.stringify(body) : undefined,
...options,
});
// Handle common errors
if (response.status === 401) {
logOut();
window.location.href = '/login';
throw new Error('Unauthorized: Redirecting to login.');
}
if (response.status === 403) {
logOut();
window.location.href = '/login';
throw new Error('Forbidden: Access denied.');
}
// Try parsing response data
let data;
try {
data = await response.json();
} catch (error) {
data = null;
console.error(`error fetching data: ${error}`);
}
if (!response.ok) {
throw new Error(data?.detail || 'An error occurred while processing the request.');
}
return data;
};
export default APIClient;