mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-30 20:06:22 -05:00
Wrapping most API requests in trys/catches.
This commit is contained in:
parent
8ab61bd8a8
commit
6a654cd3af
10 changed files with 355 additions and 123 deletions
132
app/src/api.js
132
app/src/api.js
|
|
@ -111,7 +111,16 @@ export async function deleteChannel(id) {
|
|||
|
||||
export async function getChannels() {
|
||||
let response = await apiGet(`${VIDEOS_API}/channels`);
|
||||
return (await response.json())['channels'];
|
||||
if (response.stack === 200) {
|
||||
return (await response.json())['channels'];
|
||||
} else {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Unexpected server response',
|
||||
description: 'Failed to get channels. See server logs.',
|
||||
time: 5000,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function getChannel(id) {
|
||||
|
|
@ -138,7 +147,12 @@ export async function searchVideos(offset, limit, channel_id, searchStr, order_b
|
|||
let data = await response.json();
|
||||
return [data['videos'], data['totals']['videos']];
|
||||
} else {
|
||||
throw Error(`Unable to search videos`);
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Unexpected server response',
|
||||
description: 'Failed to search videos. See server logs.',
|
||||
time: 5000,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -171,7 +185,16 @@ export async function getDirectories(search_str) {
|
|||
|
||||
export async function getSettings() {
|
||||
let response = await apiGet(`${API_URI}/settings`);
|
||||
return await response.json();
|
||||
if (response === 200) {
|
||||
return await response.json();
|
||||
} else {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Unexpected server response',
|
||||
description: 'Could not get settings',
|
||||
time: 5000,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveSettings(settings) {
|
||||
|
|
@ -180,8 +203,16 @@ export async function saveSettings(settings) {
|
|||
|
||||
export async function getDownloads() {
|
||||
let response = await apiGet(`${API_URI}/download`);
|
||||
let data = await response.json();
|
||||
return data;
|
||||
if (response.status === 200) {
|
||||
return await response.json();
|
||||
} else {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Unexpected server response',
|
||||
description: 'Could not get downloads',
|
||||
time: 5000,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -198,7 +229,16 @@ export async function favoriteVideo(video_id, favorite) {
|
|||
|
||||
export async function getStatistics() {
|
||||
let response = await apiGet(`${VIDEOS_API}/statistics`);
|
||||
return (await response.json())['statistics'];
|
||||
if (response.status === 200) {
|
||||
return (await response.json())['statistics'];
|
||||
} else {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Unexpected server response',
|
||||
description: 'Could not get statistics',
|
||||
time: 5000,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function refresh() {
|
||||
|
|
@ -233,51 +273,87 @@ export async function refreshChannel(id) {
|
|||
export async function encryptOTP(otp, plaintext) {
|
||||
let body = {otp, plaintext};
|
||||
let response = await apiPost(`${OTP_API}/encrypt_otp`, body);
|
||||
if (response.status !== 200) {
|
||||
if (response.status === 200) {
|
||||
return await response.json();
|
||||
} else {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Error!',
|
||||
description: 'Failed to encrypt OTP',
|
||||
time: 5000,
|
||||
});
|
||||
return;
|
||||
}
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
export async function decryptOTP(otp, ciphertext) {
|
||||
let body = {otp, ciphertext};
|
||||
let response = await apiPost(`${OTP_API}/decrypt_otp`, body);
|
||||
if (response.status !== 200) {
|
||||
if (response.status === 200) {
|
||||
return await response.json();
|
||||
} else {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Error!',
|
||||
description: 'Failed to decrypt OTP',
|
||||
time: 5000,
|
||||
});
|
||||
return;
|
||||
}
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
export async function getCategories() {
|
||||
let response = await apiGet(`${API_URI}/inventory/categories`);
|
||||
return (await response.json())['categories'];
|
||||
if (response.status === 200) {
|
||||
return (await response.json())['categories'];
|
||||
} else {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Unexpected server response',
|
||||
description: 'Could not get categories',
|
||||
time: 5000,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function getBrands() {
|
||||
let response = await apiGet(`${API_URI}/inventory/brands`);
|
||||
return (await response.json())['brands'];
|
||||
if (response.status === 200) {
|
||||
return (await response.json())['brands'];
|
||||
} else {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Unexpected server response',
|
||||
description: 'Could not get brands',
|
||||
time: 5000,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function getInventories() {
|
||||
let response = await apiGet(`${API_URI}/inventory`);
|
||||
return (await response.json())['inventories'];
|
||||
if (response.status === 200) {
|
||||
return (await response.json())['inventories'];
|
||||
} else {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Unexpected server response',
|
||||
description: 'Could not get inventories',
|
||||
time: 5000,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function getInventory(inventoryId) {
|
||||
let response = await apiGet(`${API_URI}/inventory/${inventoryId}`);
|
||||
return await response.json();
|
||||
if (response.status === 200) {
|
||||
return await response.json();
|
||||
} else {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Unexpected server response',
|
||||
description: 'Could not get inventory',
|
||||
time: 5000,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveInventory(inventory) {
|
||||
|
|
@ -388,7 +464,7 @@ export async function getDownloaders() {
|
|||
let response = await apiGet(`${API_URI}/downloaders`);
|
||||
return await response.json();
|
||||
} catch (e) {
|
||||
return {};
|
||||
return {downloaders: []};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -440,8 +516,10 @@ export async function deleteFile(file) {
|
|||
|
||||
export async function getVersion() {
|
||||
let response = await apiGet(`${API_URI}/settings`);
|
||||
let data = await response.json();
|
||||
return data['version'];
|
||||
if (response.status === 200) {
|
||||
let data = await response.json();
|
||||
return data['version'];
|
||||
}
|
||||
}
|
||||
|
||||
export async function getHotspotStatus() {
|
||||
|
|
@ -514,7 +592,16 @@ export async function setThrottle(on) {
|
|||
|
||||
export async function getMapImportStatus() {
|
||||
let response = await apiGet(`${API_URI}/map/files`);
|
||||
return await response.json();
|
||||
if (response.status === 200) {
|
||||
return await response.json();
|
||||
} else {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Unexpected server response',
|
||||
description: 'Could not get import status',
|
||||
time: 5000,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function importMapFiles(paths) {
|
||||
|
|
@ -559,3 +646,8 @@ export async function clearFailedDownloads() {
|
|||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAPIStatus() {
|
||||
let response = await apiGet(`${API_URI}/echo`);
|
||||
return response.status === 200;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue