import Axios from "axios"; export namespace API { // Base URL for API queries. export let Base_URL = "/api"; // Types of modifications which can be applied to various models. export enum Modification { Add, Delete, Refresh } // Change a channel state. export async function channel_modify(youtubeID : string, modify : Modification): Promise { switch (modify){ case Modification.Add: { let URL = API.Base_URL + '/Channels/' + youtubeID; let resp = await Axios.post(URL).catch((error) => { if (error.response) return error.response.data; else if (error.request) return error.request; else return "Axios request failed for unkown reason."; }); if (typeof resp == "string") return resp; break; } case Modification.Delete: { let URL = API.Base_URL + '/Channels/' + youtubeID; let resp = await Axios.delete(URL).catch(e => console.log(e)); if (resp != null){ if (resp.status != 200) return resp.data(); else return; } else { return "Response is null"; } } case Modification.Refresh: { let URL = API.Base_URL + '/Channels/Update/' + youtubeID; let resp = await Axios.post(URL).catch(e => console.log(e)); if (resp != null){ if (resp.status != 200) return resp.data(); else return; } else { return "Response is null"; } } default: { console.log("Unknown request type, error ..."); break; } } } // Search for channels based on a search string. export async function search_channels(searchstr : string): Promise> { // Temporary holder for data. let Channels : Array = []; // Ask server for data. let resp = await Axios.get(API.Base_URL + '/Channels/' + searchstr).catch(e => console.log(e)); // Handle all our nulls. if (resp == null || resp.data == null) console.log("server /api/Channels/" + searchstr + " return is null"); else { // Parse our videos and add it to the video output. resp.data.forEach((c: any) => Channels.push(new Channel(c))); } // Send back the resulting videos. return Promise.resolve(Channels); } // Search for videos based on a search string. export async function search_videos(searchstr : string): Promise> { // Temporary holder for videos. let Videos : Array