Skip to content

Commit 131689e

Browse files
authored
DK kp4cd.js update to implement alternative apis (#993)
* temp save * kp4cd module updated to try hugeampkpncms.org first then try kp4cd if fails * updated fetchWithFallback method for flexibility * Case of empty return covered.
1 parent 3906aa0 commit 131689e

File tree

1 file changed

+152
-75
lines changed

1 file changed

+152
-75
lines changed

src/modules/kp4cd.js

Lines changed: 152 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,51 @@
33
44
*/
55

6+
// Helper function to fetch with fallback: try hugeampkpncms.org first, fallback to kp4cd.org on error
7+
async function fetchWithFallback(PRIMARY_URL, FALLBACK_URL) {
8+
// Replace kp4cd.org with hugeampkpncms.org for the first attempt
9+
const primaryUrl = PRIMARY_URL;
10+
const fallbackUrl = FALLBACK_URL;
11+
12+
let response;
13+
try {
14+
response = await fetch(primaryUrl);
15+
// Check if response is ok (status 200-299)
16+
if (response.ok) {
17+
const jsonData = await response.json();
18+
// Check if the result is an empty array
19+
if (Array.isArray(jsonData) && jsonData.length === 0) {
20+
console.log(`Primary URL (${primaryUrl}) returned empty results [], trying fallback (${fallbackUrl})...`);
21+
// Continue to fallback logic below
22+
} else {
23+
return jsonData;
24+
}
25+
}
26+
} catch (error) {
27+
// Network error or other fetch failure - will try fallback below
28+
console.log(`Primary URL (${primaryUrl}) failed with error: ${error.message}, trying fallback...`);
29+
}
30+
31+
// If we get here, primary either returned non-OK status, threw an error, or returned empty results
32+
// Try fallback URL
33+
if (response && !response.ok) {
34+
console.log(`Primary URL (${primaryUrl}) returned ${response.status}, trying fallback (${fallbackUrl})...`);
35+
}
36+
37+
try {
38+
const fallbackResponse = await fetch(fallbackUrl);
39+
if (!fallbackResponse.ok) {
40+
throw new Error(`Fallback URL (${fallbackUrl}) returned ${fallbackResponse.status}`);
41+
}
42+
console.log(`Fallback URL (${fallbackUrl}) succeeded`);
43+
return await fallbackResponse.json();
44+
} catch (fallbackError) {
45+
// If fallback also fails, throw the error
46+
console.error(`Fallback URL (${fallbackUrl}) also failed: ${fallbackError.message}`);
47+
throw fallbackError;
48+
}
49+
}
50+
651
export default {
752
namespaced: true,
853

@@ -101,123 +146,147 @@ export default {
101146
async getNewsFeed(context, selectedDiseaseGroup) {
102147
let portal = selectedDiseaseGroup || "md";
103148

104-
let json = await fetch(
149+
let json = await fetchWithFallback(
150+
`https://hugeampkpncms.org/rest/views/news2vueportal?portal=` + portal,
105151
`https://kp4cd.org/rest/views/news2vueportal?portal=` + portal
106-
).then((resp) => resp.json());
152+
);
107153
// set the data
108154
context.commit("setNewsFeed", json);
109155
},
110156

111157
async getFrontContents(context, selectedDiseaseGroup) {
112158
let portal = selectedDiseaseGroup || "md";
113-
let json = await fetch(
114-
"https://kp4cd.org/reset/views/portal_front?portal=" + portal
115-
).then((resp) => resp.json());
159+
let json = await fetchWithFallback(
160+
`https://hugeampkpncms.org/reset/views/portal_front?portal=` + portal,
161+
`https://kp4cd.org/reset/views/portal_front?portal=` + portal
162+
);
116163
// set the data
117164
context.commit("setFrontContents", json);
118165
},
119166

120167
async getDatasetsInfo(context, selectedDiseaseGroup) {
121168
let portal =
122169
selectedDiseaseGroup == "md" ? "" : selectedDiseaseGroup;
123-
let json = await fetch(
124-
"https://kp4cd.org/rest/views/kpdatasets?portal=" + portal
125-
).then((resp) => resp.json());
170+
let json = await fetchWithFallback(
171+
`https://hugeampkpncms.org/rest/views/kpdatasets?portal=` + portal,
172+
`https://kp4cd.org/rest/views/kpdatasets?portal=` + portal
173+
);
126174
// set the data
127175
context.commit("setDatasetsInfo", json);
128176
},
129177

130178
async getDatasetInfo(context, datasetId) {
131-
let json = await fetch(
132-
"https://kp4cd.org/rest/views/datasetinfo?datasetid=" +
133-
datasetId
134-
).then((resp) => resp.json());
135-
// set the data
179+
let json = await fetchWithFallback(
180+
`https://hugeampkpncms.org/rest/views/datasetinfo?datasetid=` + datasetId,
181+
`https://kp4cd.org/rest/views/datasetinfo?datasetid=` + datasetId
182+
);
183+
// set the dat
136184
context.commit("setDatasetInfo", json);
137185
},
138186

139187
async getPageInfo(context, query) {
140-
let json = await fetch(
141-
"https://kp4cd.org/rest/views/" +
142-
query.page +
143-
"?portal=" +
144-
query.portal
145-
).then((resp) => resp.json());
188+
let json = await fetchWithFallback(
189+
`https://hugeampkpncms.org/rest/views/${query.page}?portal=${query.portal}`,
190+
`https://kp4cd.org/rest/views/${query.page}?portal=${query.portal}`
191+
);
146192
// set the data
147193
context.commit("setPageInfo", json);
148194
},
149195

150196
async getNewFeatures(context, selectedDiseaseGroup) {
151197
let portal = selectedDiseaseGroup || "md";
152198

153-
let json = await fetch(
154-
"https://kp4cd.org/rest/views/newfeatures?portal=" +
155-
selectedDiseaseGroup
156-
).then((resp) => resp.json());
199+
let json = await fetchWithFallback(
200+
`https://hugeampkpncms.org/rest/views/newfeatures?portal=` + selectedDiseaseGroup,
201+
`https://kp4cd.org/rest/views/newfeatures?portal=` + selectedDiseaseGroup
202+
);
157203
// set the data
158204
context.commit("setNewFeatures", json);
159205
},
160206
async getResources(context, selectedDiseaseGroup) {
161207
let portal = selectedDiseaseGroup || "md";
162208

163-
let json = await fetch(
164-
"https://kp4cd.org/rest/views/newresources?portal=" +
165-
selectedDiseaseGroup
166-
).then((resp) => resp.json());
209+
let json = await fetchWithFallback(
210+
`https://hugeampkpncms.org/rest/views/newresources?portal=` + selectedDiseaseGroup,
211+
`https://kp4cd.org/rest/views/newresources?portal=` + selectedDiseaseGroup
212+
);
167213
// set the data
168214
context.commit("setResources", json);
169215
},
170216
async getResearchMethod(context, methodFrom) {
171-
let json = await fetch(
172-
"https://kp4cd.org/rest/views/eglmethod?from=" + methodFrom
173-
).then((resp) => resp.json());
217+
let json = await fetchWithFallback(
218+
`https://hugeampkpncms.org/rest/views/eglmethod?from=` + methodFrom,
219+
`https://kp4cd.org/rest/views/eglmethod?from=` + methodFrom
220+
);
174221
// set the data
175222
context.commit("setResearchMethod", json);
176223
},
177224
async getEglSummaries(context, selectedDiseaseGroup) {
178225
let portal = selectedDiseaseGroup || "md";
179226

180-
let json = await fetch(
181-
"https://kp4cd.org/rest/views/eglmethodsperportal?portal=" +
182-
selectedDiseaseGroup
183-
).then((resp) => resp.json());
227+
let json = await fetchWithFallback(
228+
`https://hugeampkpncms.org/rest/views/eglmethodsperportal?portal=` +
229+
selectedDiseaseGroup,
230+
`https://kp4cd.org/rest/views/eglmethodsperportal?portal=` +
231+
selectedDiseaseGroup
232+
);
184233
// set the data
185234
context.commit("setEglSummaries", json);
186235
},
187236
async getEglData(context, targetData) {
188-
let json = await fetch(
189-
"https://kp4cd.org/egldata/dataset?dataset=" +
190-
targetData.dataset +
191-
"&trait=" +
192-
targetData.trait
193-
).then((resp) => resp.json());
237+
let json = await fetchWithFallback(
238+
`https://hugeampkpncms.org/egldata/dataset?dataset=` +
239+
targetData.dataset +
240+
"&trait=" +
241+
targetData.trait,
242+
`https://kp4cd.org/egldata/dataset?dataset=` +
243+
targetData.dataset +
244+
"&trait=" +
245+
targetData.trait
246+
);
194247

195248
context.commit("setEglData", json);
196249
},
197250
async getResearchData(context, targetDataPoint) {
198-
let json = await fetch(targetDataPoint).then((resp) => resp.json());
251+
// If it's a kp4cd.org URL, use fallback logic; otherwise fetch directly
252+
let json;
253+
if (targetDataPoint && targetDataPoint.includes('kp4cd.org')) {
254+
json = await fetchWithFallback(targetDataPoint);
255+
} else {
256+
json = await fetch(targetDataPoint).then((resp) => resp.json());
257+
}
199258

200259
context.commit("setResearchData", json);
201260
},
202261
async getResearchDataPage(context, param) {
203-
let json = await fetch(
204-
"https://kp4cd.org/rest/views/research_data?dataid=" +
205-
param.pageID +
206-
"&&reviewerid=" +
207-
param.reviewerID +
208-
"&&reviewercode=" +
209-
param.reviewerCode
210-
).then((resp) => resp.json());
262+
let json = await fetchWithFallback(
263+
`https://hugeampkpncms.org/rest/views/research_data?dataid=` +
264+
param.pageID +
265+
"&&reviewerid=" +
266+
param.reviewerID +
267+
"&&reviewercode=" +
268+
param.reviewerCode,
269+
`https://kp4cd.org/rest/views/research_data?dataid=` +
270+
param.pageID +
271+
"&&reviewerid=" +
272+
param.reviewerID +
273+
"&&reviewercode=" +
274+
param.reviewerCode
275+
);
211276
// set the data
212277
context.commit("setResearchDataPage", json);
213278
},
214279
async getEglConfig(context, targetData) {
215-
let json = await fetch(
216-
"https://kp4cd.org/egldata/config?dataset=" +
217-
targetData.dataset +
218-
"&trait=" +
219-
targetData.trait
220-
).then((resp) => resp.json());
280+
let json = await fetchWithFallback(
281+
`https://hugeampkpncms.org/egldata/config?dataset=` +
282+
targetData.dataset +
283+
"&trait=" +
284+
targetData.trait,
285+
`https://kp4cd.org/egldata/config?dataset=` +
286+
targetData.dataset +
287+
"&trait=" +
288+
targetData.trait
289+
);
221290

222291
context.commit("setEglConfig", json);
223292
},
@@ -229,46 +298,54 @@ export default {
229298
context.commit("setForestPlotData", json);
230299
},
231300
async getStaticContent(context, page) {
232-
let json = await fetch(
233-
"https://kp4cd.org/rest/views/static_content?field_page=" + page
234-
).then((resp) => resp.json());
301+
let json = await fetchWithFallback(
302+
`https://hugeampkpncms.org/rest/views/static_content?field_page=` + page,
303+
`https://kp4cd.org/rest/views/static_content?field_page=` + page
304+
);
235305
// set the data
236306
context.commit("setStaticContent", json);
237307
},
238308
async getPaperMenu(context, paperPage) {
239-
let json = await fetch(
240-
"https://kp4cd.org/rest/views/paperheadermenu?paper=" +
241-
paperPage
242-
).then((resp) => resp.json());
309+
let json = await fetchWithFallback(
310+
`https://hugeampkpncms.org/rest/views/paperheadermenu?paper=` +
311+
paperPage,
312+
`https://kp4cd.org/rest/views/paperheadermenu?paper=` +
313+
paperPage
314+
);
243315
// set the data
244316
context.commit("setPaperMenu", json);
245317
},
246318
async getPortals(context) {
247-
let json = await fetch(
248-
"https://kp4cd.org/rest/views/a2f_community_kps"
249-
).then((resp) => resp.json());
319+
let json = await fetchWithFallback(
320+
`https://hugeampkpncms.org/rest/views/a2f_community_kps`,
321+
`https://kp4cd.org/rest/views/a2f_community_kps`
322+
);
250323
// set the data
251324
context.commit("setPortals", json);
252325
},
253326
async getHelpBook(context) {
254-
let json = await fetch(
255-
"https://kp4cd.org/rest/views/help_book"
256-
).then((resp) => resp.json());
327+
let json = await fetchWithFallback(
328+
`https://hugeampkpncms.org/rest/views/help_book`,
329+
`https://kp4cd.org/rest/views/help_book`
330+
);
257331
// set the data
258332
context.commit("setHelpBook", json);
259333
},
260334
async getContentByID(context, nid) {
261-
let json = await fetch(
262-
"https://kp4cd.org/rest/views/content_by_id?nid=" + nid
263-
).then((resp) => resp.json());
335+
let json = await fetchWithFallback(
336+
`https://hugeampkpncms.org/rest/views/content_by_id?nid=` + nid,
337+
`https://kp4cd.org/rest/views/content_by_id?nid=` + nid
338+
);
264339
// set the data
265340
context.commit("setContentByID", json);
266341
},
267342
async getHelpBookSearch(context, searchKey) {
268-
let json = await fetch(
269-
"https://kp4cd.org/rest/views/help_book_search?body=" +
270-
searchKey
271-
).then((resp) => resp.json());
343+
let json = await fetchWithFallback(
344+
`https://hugeampkpncms.org/rest/views/help_book_search?body=` +
345+
searchKey,
346+
`https://kp4cd.org/rest/views/help_book_search?body=` +
347+
searchKey
348+
);
272349
// set the data
273350
context.commit("setHelpBookSearch", json);
274351
},

0 commit comments

Comments
 (0)