import * as config from "./config"; import { log, LogLevel } from "./logging" import fetch, { Response as FetchResponse } from "node-fetch"; export async function apiRequest(endpoint: string, parameters: URLSearchParams): Promise { log(LogLevel.DEBUG,`Call: youtube.apiRequest(${endpoint})`); parameters.set('key',config.youtubeSecret); var requestUrl = "https://www.googleapis.com/youtube/v3/" + endpoint + "?" + parameters.toString(); return fetch(requestUrl) .then(async (res: FetchResponse) => { if (res.status == 200) { return res.json(); } else { log(LogLevel.WARNING,"Failed Youtube API request:"); log(LogLevel.WARNING,"Request URL: " + requestUrl); log(LogLevel.WARNING,"Response:"); log(LogLevel.WARNING,JSON.stringify(await res.json(),null,2)); return false; } }) }