From 995c855ac2b77001b2541b3010bb944076ec6bf8 Mon Sep 17 00:00:00 2001 From: Megh Rathod Date: Sun, 14 Apr 2024 16:19:51 +0530 Subject: [PATCH] added useFallback to decide which TMDB url to use Signed-off-by: Megh Rathod --- src/backend/metadata/tmdb.ts | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/backend/metadata/tmdb.ts b/src/backend/metadata/tmdb.ts index 2e675f10..67a79490 100644 --- a/src/backend/metadata/tmdb.ts +++ b/src/backend/metadata/tmdb.ts @@ -143,8 +143,9 @@ export function decodeTMDBId( }; } +const baseURL = "https://api.themoviedb.org/3"; const otherUrl = "https://api.tmdb.org/3"; -let baseURL = "https://api.themoviedb.org/3"; +let useFallback = false; const apiKey = conf().TMDB_READ_API_KEY; @@ -155,33 +156,26 @@ const headers = { async function get(url: string, params?: object): Promise { if (!apiKey) throw new Error("TMDB API key not set"); - let res: T; - try { res = await mwFetch(encodeURI(url), { headers, - baseURL, + baseURL: !useFallback ? baseURL : otherUrl, params: { ...params, }, - signal: AbortSignal.timeout(baseURL !== otherUrl ? 5000 : 30000), + signal: AbortSignal.timeout(!useFallback ? 5000 : 30000), }); } catch (err) { - if (baseURL !== otherUrl) { - baseURL = otherUrl; - res = await mwFetch(encodeURI(url), { - headers, - baseURL, - params: { - ...params, - }, - }); - } else { - res = Promise.reject(); - } + useFallback = true; + res = await mwFetch(encodeURI(url), { + headers, + baseURL: otherUrl, + params: { + ...params, + }, + }); } - return res; }