-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathgeneric-request.js
112 lines (104 loc) · 3.39 KB
/
generic-request.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Wazuh app - Generic request
* Copyright (C) 2015-2022 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/
import { AppState } from './app-state';
import { WazuhConfig } from './wazuh-config';
import { ApiCheck } from './wz-api-check';
import { WzMisc } from '../factories/misc';
import { OdfeUtils } from '../utils';
import { getHttp, getDataPlugin } from '../kibana-services';
import { PLUGIN_PLATFORM_REQUEST_HEADERS } from '../../common/constants';
import { request } from '../services/request-handler';
export class GenericRequest {
static async request(method, path, payload = null, returnError = false) {
try {
if (!method || !path) {
throw new Error('Missing parameters');
}
const wazuhConfig = new WazuhConfig();
const { timeout } = wazuhConfig.getConfig();
const requestHeaders = {
...PLUGIN_PLATFORM_REQUEST_HEADERS,
'content-type': 'application/json'
};
const tmpUrl = getHttp().basePath.prepend(path);
try {
requestHeaders.pattern = (await getDataPlugin().indexPatterns.get(AppState.getCurrentPattern())).title;
} catch (error) { };
try {
requestHeaders.id = JSON.parse(AppState.getCurrentAPI()).id;
} catch (error) {
// Intended
}
var options = {};
const data = {};
if (method === 'GET') {
options = {
method: method,
path: path,
timeout: timeout || 20000
};
}
if (method === 'PUT') {
options = {
method: method,
data: payload,
path: path,
timeout: timeout || 20000
};
}
if (method === 'POST') {
options = {
method: method,
data: payload,
path: path,
timeout: timeout || 20000
};
}
if (method === 'DELETE') {
options = {
method: method,
data: payload,
path: path,
timeout: timeout || 20000
};
}
Object.assign(data, await request(options));
if (!data) {
throw new Error(
`Error doing a request to ${tmpUrl}, method: ${method}.`
);
}
return data;
} catch (err) {
OdfeUtils.checkOdfeSessionExpired(err);
//if the requests fails, we need to check if the API is down
const currentApi = JSON.parse(AppState.getCurrentAPI() || '{}');
if (currentApi && currentApi.id) {
try {
await ApiCheck.checkStored(currentApi.id);
} catch (err) {
const wzMisc = new WzMisc();
wzMisc.setApiIsDown(true);
if (!window.location.hash.includes('#/settings') &&
!window.location.hash.includes('#/health-check') &&
!window.location.hash.includes('#/blank-screen')) {
window.location.href = getHttp().basePath.prepend('/app/wazuh#/health-check');
}
}
}
if (returnError) return Promise.reject(err);
return (((err || {}).response || {}).data || {}).message || false
? Promise.reject(err.response.data.message)
: Promise.reject(err || 'Server did not respond');
}
}
}