From bee5c12ab7897b01b552be43f23e27d4a989e525 Mon Sep 17 00:00:00 2001 From: vbudko Date: Tue, 6 May 2025 10:25:19 +0100 Subject: [PATCH 1/2] Implement urlFallback --- api/search.js | 28 +++++++++++++++++++++------- env.config.js | 2 ++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/api/search.js b/api/search.js index 229619b9..1aeeeec0 100644 --- a/api/search.js +++ b/api/search.js @@ -6,16 +6,31 @@ export const fetchWorks = async (body) => { const { t } = body const split = t?.split('-') const isUndefined = split?.some((item) => item === undefined) - const url = new URL( + + const urlPrimary = new URL( `/v3/search/works${!isUndefined || t ? `?t=${t}` : ''}`, process.env.API_URL ).href - const { data: dataWorks } = await apiRequest(url, { - body, - method: 'POST', - }) - return dataWorks + const urlFallback = new URL( + `/v3/search/works${!isUndefined || t ? `?t=${t}` : ''}`, + process.env.API_URL_01 + ).href + + try { + const { data: dataWorks } = await apiRequest(urlPrimary, { + method: 'POST', + body, + }) + return dataWorks + } catch (err) { + console.warn(`Primary API failed (${urlPrimary}), retrying fallback…`, err) + const { data: dataWorks } = await apiRequest(urlFallback, { + method: 'POST', + body, + }) + return dataWorks + } } export const fetchAggregations = async (body) => { @@ -32,7 +47,6 @@ export const fetchAggregations = async (body) => { export const downloadResultsInCSV = async (body) => { const url = new URL(`/v3/search/works`, process.env.API_URL).href - await apiRequest(url, { body, method: 'POST', diff --git a/env.config.js b/env.config.js index 2f45da02..3260e3e4 100644 --- a/env.config.js +++ b/env.config.js @@ -6,10 +6,12 @@ const local = { const development = { API_URL: 'https://api-dev.core.ac.uk/internal', + API_URL_01: 'https://api-dev.core.ac.uk/internal', } const production = { API_URL: 'https://api.core.ac.uk/internal', + API_URL_01: 'https://api01.core.ac.uk/internal', } const validate = (config) => From 1e2187a3e26886cce567f2650f0f449476491e42 Mon Sep 17 00:00:00 2001 From: vbudko Date: Tue, 6 May 2025 10:35:39 +0100 Subject: [PATCH 2/2] Implement urlFallback --- api/search.js | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/api/search.js b/api/search.js index 1aeeeec0..174bbf9a 100644 --- a/api/search.js +++ b/api/search.js @@ -24,7 +24,8 @@ export const fetchWorks = async (body) => { }) return dataWorks } catch (err) { - console.warn(`Primary API failed (${urlPrimary}), retrying fallback…`, err) + console.warn('Primary API failed') + console.error(`Primary API failed (${urlPrimary}), retrying fallback…`, err) const { data: dataWorks } = await apiRequest(urlFallback, { method: 'POST', body, @@ -34,15 +35,33 @@ export const fetchWorks = async (body) => { } export const fetchAggregations = async (body) => { - const url = new URL(`/v3/search/works/aggregate`, process.env.API_URL).href - const { data: aggregations } = await apiRequest(url, { - body: { - ...body, - q: body.q || '', - }, - method: 'POST', - }) - return aggregations + const urlPrimary = new URL(`/v3/search/works/aggregate`, process.env.API_URL).href + const urlFallback = new URL(`/v3/search/works/aggregate`, process.env.API_URL_01).href + + try { + const { data: aggregations } = await apiRequest(urlPrimary, { + body: { + ...body, + q: body.q || '', + }, + method: 'POST', + }) + return aggregations + } catch (err) { + console.warn('Primary API failed') + console.error( + `Primary API failed (${urlPrimary}), retrying fallback for aggregate …`, + err + ) + const { data: aggregations } = await apiRequest(urlFallback, { + body: { + ...body, + q: body.q || '', + }, + method: 'POST', + }) + return aggregations + } } export const downloadResultsInCSV = async (body) => {