124 lines
2.7 KiB
JavaScript
124 lines
2.7 KiB
JavaScript
import { useToast } from "../hooks";
|
|
import { get, post } from "../utils/axios";
|
|
|
|
const networkRoute = `${process.env.REACT_APP_NETWORK_RESURRECTOR_API_URL}/network`;
|
|
const systemRoute = `${process.env.REACT_APP_NETWORK_RESURRECTOR_API_URL}/system`;
|
|
const powerActionsRoute = `${process.env.REACT_APP_NETWORK_RESURRECTOR_API_URL}/resurrector`;
|
|
|
|
const useApi = () => {
|
|
const { error } = useToast();
|
|
|
|
const handleError = err => {
|
|
let message;
|
|
switch (err?.status) {
|
|
case 500:
|
|
message = err.title;
|
|
break;
|
|
|
|
case 404:
|
|
message = err.message;
|
|
break;
|
|
|
|
default:
|
|
message = err.title;
|
|
}
|
|
|
|
error(message);
|
|
};
|
|
|
|
const defaultOptions = {
|
|
onCompleted: () => {},
|
|
onError: handleError
|
|
};
|
|
|
|
const call = async (request, options) => {
|
|
const internalOptions = { ...defaultOptions, ...options };
|
|
const { onCompleted, onError } = internalOptions;
|
|
|
|
try {
|
|
const result = await request();
|
|
onCompleted(result);
|
|
} catch (error) {
|
|
onError(error);
|
|
}
|
|
};
|
|
|
|
const getSystemVersion = (options = defaultOptions) => {
|
|
const releaseNotesPromise = call(
|
|
() => get(`${systemRoute}/version`),
|
|
options
|
|
);
|
|
return releaseNotesPromise;
|
|
};
|
|
|
|
const readReleaseNotes = (options = defaultOptions) => {
|
|
const releaseNotesPromise = call(
|
|
() => get(`${systemRoute}/release-notes`),
|
|
options
|
|
);
|
|
return releaseNotesPromise;
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
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 {
|
|
getSystemVersion,
|
|
readReleaseNotes,
|
|
readMachines,
|
|
wakeMachine,
|
|
pingMachine,
|
|
shutdownMachine,
|
|
restartMachine
|
|
};
|
|
};
|
|
|
|
export default useApi;
|