generated from RedHatInsights/frontend-starter-app
-
Notifications
You must be signed in to change notification settings - Fork 22
/
api.js
95 lines (78 loc) · 2.13 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import axios from 'axios';
import {
TASKS_API_ROOT,
AVAILABLE_TASKS_ROOT,
EXECUTED_TASK_ROOT,
SYSTEMS_ROOT,
JOB_ROOT,
} from './src/constants';
const returnErrOrData = (response) => {
if (response.status === 200) {
return response.data;
} else {
return response;
}
};
const getTasks = async (path) => {
const response = await axios
.get(TASKS_API_ROOT.concat(path))
.catch(function (error) {
return error;
});
return returnErrOrData(response);
};
const postTask = async (path, data) => {
const response = await axios
.post(TASKS_API_ROOT.concat(path), data)
.catch(function (error) {
return error;
});
return returnErrOrData(response);
};
const deleteTask = async (path) => {
const response = await axios
.delete(TASKS_API_ROOT.concat(path))
.catch(function (error) {
return error;
});
return returnErrOrData(response);
};
export const fetchAvailableTasks = (path = '') => {
return getTasks(AVAILABLE_TASKS_ROOT.concat(path));
};
export const fetchAvailableTask = (slug) => {
return getTasks(AVAILABLE_TASKS_ROOT.concat(`/${slug}`));
};
export const fetchExecutedTasks = (path) => {
if (path) {
return getTasks(EXECUTED_TASK_ROOT.concat(path));
} else {
return getTasks(EXECUTED_TASK_ROOT);
}
};
export const fetchExecutedTask = (id, jobs_path = '') => {
let idPath = `/${id}${jobs_path}`;
return getTasks(EXECUTED_TASK_ROOT.concat(idPath));
};
export const fetchExecutedTaskJobs = (id, path) => {
return fetchExecutedTask(id, `/jobs${path}`);
};
export const fetchSystems = (path = '', slug = '') => {
if (slug) {
return getTasks(AVAILABLE_TASKS_ROOT.concat(`/${slug}/systems${path}`));
} else {
return getTasks(SYSTEMS_ROOT.concat(path));
}
};
export const executeTask = (body) => {
return postTask(EXECUTED_TASK_ROOT, body);
};
export const deleteExecutedTask = (id) => {
return deleteTask(EXECUTED_TASK_ROOT.concat(`/${id}`));
};
export const cancelExecutedTask = (id) => {
return postTask(EXECUTED_TASK_ROOT.concat(`/${id}/cancel`));
};
export const getLogs = (id) => {
return getTasks(JOB_ROOT.concat(`/${id}/stdout`))
};