network-resurrector/src/api/useApi.js

124 lines
2.7 KiB
JavaScript
Raw Normal View History

2022-01-18 14:08:04 +02:00
import { useToast } from "../hooks";
import { get, post } from "../utils/axios";
const networkRoute = `${process.env.REACT_APP_NETWORK_RESURRECTOR_API_URL}/network`;
2023-03-25 12:57:51 +02:00
const systemRoute = `${process.env.REACT_APP_NETWORK_RESURRECTOR_API_URL}/system`;
2022-01-18 14:08:04 +02:00
const powerActionsRoute = `${process.env.REACT_APP_NETWORK_RESURRECTOR_API_URL}/resurrector`;
const useApi = () => {
const { error } = useToast();
const handleError = err => {
let message;
2022-01-18 14:56:18 +02:00
switch (err?.status) {
2022-01-18 14:08:04 +02:00
case 500:
2023-01-30 00:50:00 +02:00
message = err.title;
2022-01-18 14:08:04 +02:00
break;
2022-01-18 14:56:18 +02:00
case 404:
2022-01-18 14:08:04 +02:00
message = err.message;
2022-01-18 14:56:18 +02:00
break;
default:
2023-01-30 00:50:00 +02:00
message = err.title;
2022-01-18 14:08:04 +02:00
}
error(message);
};
2022-01-18 17:18:30 +02:00
const defaultOptions = {
onCompleted: () => {},
onError: handleError
};
2022-01-18 14:08:04 +02:00
const call = async (request, options) => {
const internalOptions = { ...defaultOptions, ...options };
const { onCompleted, onError } = internalOptions;
try {
const result = await request();
onCompleted(result);
} catch (error) {
onError(error);
}
};
2023-03-26 02:43:28 +02:00
const getSystemVersion = (options = defaultOptions) => {
const releaseNotesPromise = call(
() => get(`${systemRoute}/version`),
options
);
return releaseNotesPromise;
};
2023-03-25 12:57:51 +02:00
const readReleaseNotes = (options = defaultOptions) => {
const releaseNotesPromise = call(
() => get(`${systemRoute}/release-notes`),
options
);
return releaseNotesPromise;
};
2022-01-18 14:08:04 +02:00
const readMachines = (options = defaultOptions) => {
const machinesPromise = call(
() => get(`${networkRoute}/machines`),
options
);
return machinesPromise;
};
const wakeMachine = (machineId, options = defaultOptions) => {
const promise = call(
() => post(`${powerActionsRoute}/wake`, { machineId }),
options
);
return promise;
};
const pingMachine = (machineId, options = defaultOptions) => {
const promise = call(
() => post(`${powerActionsRoute}/ping`, { machineId }),
options
);
return promise;
};
2022-01-18 17:18:30 +02:00
const shutdownMachine = (
machineId,
delay,
force,
options = defaultOptions
) => {
const promise = call(
() => post(`${powerActionsRoute}/shutdown`, { machineId, delay, force }),
options
);
return promise;
};
const restartMachine = (
machineId,
delay,
force,
options = defaultOptions
) => {
const promise = call(
() => post(`${powerActionsRoute}/restart`, { machineId, delay, force }),
options
);
return promise;
};
return {
2023-03-26 02:43:28 +02:00
getSystemVersion,
2023-03-25 12:57:51 +02:00
readReleaseNotes,
2022-01-18 17:18:30 +02:00
readMachines,
wakeMachine,
pingMachine,
shutdownMachine,
restartMachine
};
2022-01-18 14:08:04 +02:00
};
export default useApi;