From d832c7bbaffc636fc4d86ad83c8de557bdcbf91d Mon Sep 17 00:00:00 2001 From: Lukas Jukna Date: Sat, 15 May 2021 22:49:30 +0300 Subject: [PATCH] methods to call API endpoints --- Frontend/src/api/api.constants.js | 10 +++ Frontend/src/clients/baseClient.js | 97 +++++++++++++++++++---------- Frontend/src/clients/videoClient.js | 57 ++++++++++++++--- 3 files changed, 124 insertions(+), 40 deletions(-) create mode 100644 Frontend/src/api/api.constants.js diff --git a/Frontend/src/api/api.constants.js b/Frontend/src/api/api.constants.js new file mode 100644 index 0000000..7e38064 --- /dev/null +++ b/Frontend/src/api/api.constants.js @@ -0,0 +1,10 @@ +export const BASE_PATH = "http://localhost:44366"; + +export const EP_GET_HEALTH = "/health"; +export const EP_GET_TRASHED_USER_VIDEOS = (userId) => + `/video/trashed?userId=${userId}`; +export const EP_GET_UNTRASHED_USER_VIDEOS = (userId) => + `/video/untrashed?userId=${userId}`; +export const EP_PUT_TRASH_USER_VIDEO = (userId, videoId) => + `/video/trash?videoId=${videoId}`; +export const EP_DELETE_USER_VIDEO = (videoId) => `/video?videoId=${videoId}`; diff --git a/Frontend/src/clients/baseClient.js b/Frontend/src/clients/baseClient.js index 035e0c5..f9b3f7b 100644 --- a/Frontend/src/clients/baseClient.js +++ b/Frontend/src/clients/baseClient.js @@ -1,36 +1,69 @@ -import { API_URL_PREFIX } from '../constants'; +import { API_URL_PREFIX } from "../constants"; const makeRequest = async (endpoint, method, additional) => { - return fetch( - `${API_URL_PREFIX}${endpoint}`, - { - method, - mode: 'cors', - ...(additional !== undefined && { ...additional }), - } - ).then(response => { - if (response.ok) { - return response.json().catch(error => { - return {}; - }); - } - if (typeof response.text === 'function') { - return response.text().then(text => { - return { - 'error': text, - 'statusCode': response.status, - } - }); - } - return { - 'error': response.statusText, - 'statusCode': response.status, - } - }).then(response => { - return response; - }).catch(error => { - console.log(error); + return fetch(`${API_URL_PREFIX}${endpoint}`, { + method, + mode: "cors", + ...(additional !== undefined && { ...additional }), + }) + .then((response) => { + if (response.ok) { + return response.json().catch((error) => { + return {}; + }); + } + if (typeof response.text === "function") { + return response.text().then((text) => { + return { + error: text, + statusCode: response.status, + }; + }); + } + return { + error: response.statusText, + statusCode: response.status, + }; + }) + .then((response) => { + return response; + }) + .catch((error) => { + console.log(error); }); -} +}; -export default makeRequest; +const makeApiRequest = async (base_path, endpoint, method, additional) => { + return fetch(`${base_path}${endpoint}`, { + method, + mode: "cors", + ...(additional !== undefined && { ...additional }), + }) + .then((response) => { + if (response.ok) { + return response.json().catch((error) => { + return {}; + }); + } + if (typeof response.text === "function") { + return response.text().then((text) => { + return { + error: text, + statusCode: response.status, + }; + }); + } + return { + error: response.statusText, + statusCode: response.status, + }; + }) + .then((response) => { + return response; + }) + .catch((error) => { + console.log(error); + }); +}; + +export { makeRequest, makeApiRequest }; diff --git a/Frontend/src/clients/videoClient.js b/Frontend/src/clients/videoClient.js index 15563c7..668d368 100644 --- a/Frontend/src/clients/videoClient.js +++ b/Frontend/src/clients/videoClient.js @@ -1,12 +1,53 @@ -import makeRequest from "./baseClient"; +import { makeRequest, makeApiRequest } from "./baseClient"; +import { + BASE_PATH, + EP_DELETE_USER_VIDEO, + EP_GET_TRASHED_USER_VIDEOS, + EP_GET_UNTRASHED_USER_VIDEOS, + EP_PUT_TRASH_USER_VIDEO, +} from "../api/api.constants"; -const uploadVideo = async (video) => { - const formData = new FormData(); - formData.append('formFile', video); +const getUntrashedUserVideos = async (userId) => { + const response = makeApiRequest( + BASE_PATH, + EP_GET_UNTRASHED_USER_VIDEOS(userId), + "GET" + ); + return response; +}; + +const getTrashedUserVideos = async (userId) => { + const response = makeApiRequest( + BASE_PATH, + EP_GET_TRASHED_USER_VIDEOS(userId), + "GET" + ); + return response; +}; + +const trashUserVideo = async (userId, videoId) => { + const response = makeApiRequest( + BASE_PATH, + EP_PUT_TRASH_USER_VIDEO(userId, videoId), + "PUT" + ); + return response; +}; - return makeRequest('video', 'POST', { body: formData }); -} +const deleteUserVideo = async (userId, videoId) => { + const response = makeApiRequest( + BASE_PATH, + EP_DELETE_USER_VIDEO(videoId), + "DELETE" + ); + return response; +}; -export { - uploadVideo, +const uploadVideo = async (video) => { + const formData = new FormData(); + formData.append("formFile", video); + + return makeRequest("video", "POST", { body: formData }); }; + +export { uploadVideo };