2017-11-02 09:22:46 +00:00
|
|
|
const prepError = require('plugins/wazuh/services/prep-error');
|
2017-10-20 19:12:08 +00:00
|
|
|
import chrome from 'ui/chrome';
|
2017-10-27 11:56:40 +00:00
|
|
|
|
2018-01-29 11:39:33 +00:00
|
|
|
require('ui/modules').get('app/wazuh', []).service('genericReq', function ($q, $http, $location, $rootScope, appState,errorHandler) {
|
2017-10-27 11:56:40 +00:00
|
|
|
|
2017-12-12 16:59:19 +00:00
|
|
|
const _request = (method, url, payload = null) => {
|
|
|
|
let defered = $q.defer();
|
2017-10-27 11:56:40 +00:00
|
|
|
|
2017-12-12 16:59:19 +00:00
|
|
|
if (!method || !url) {
|
|
|
|
defered.reject({
|
|
|
|
'error': -1,
|
|
|
|
'message': 'Missing parameters'
|
|
|
|
});
|
|
|
|
return defered.promise;
|
|
|
|
}
|
2018-01-30 10:45:33 +00:00
|
|
|
let requestHeaders = { headers: { "Content-Type": 'application/json' }, timeout: $rootScope.userTimeout || 8000 };
|
2017-12-12 16:59:19 +00:00
|
|
|
|
|
|
|
let tmpUrl = chrome.addBasePath(url), tmp = null;
|
2018-01-25 12:43:47 +00:00
|
|
|
if(appState.getUserCode()) requestHeaders.headers.code = appState.getUserCode();
|
2018-01-31 15:19:09 +00:00
|
|
|
const id = appState.getCurrentAPI() ? JSON.parse(appState.getCurrentAPI()).id : false;
|
2018-01-31 15:08:04 +00:00
|
|
|
if(id) requestHeaders.headers.id = id;
|
2017-12-12 16:59:19 +00:00
|
|
|
if (method === "GET") tmp = $http.get(tmpUrl, requestHeaders);
|
|
|
|
if (method === "PUT") tmp = $http.put(tmpUrl, payload, requestHeaders);
|
|
|
|
if (method === "POST") tmp = $http.post(tmpUrl, payload, requestHeaders);
|
|
|
|
if (method === "DELETE") tmp = $http.delete(tmpUrl);
|
|
|
|
|
|
|
|
if(!tmp) {
|
|
|
|
defered.reject({
|
2018-01-29 12:17:26 +00:00
|
|
|
error: -2,
|
|
|
|
message: `Error doing a request to ${tmpUrl}, method: ${method}.`
|
2017-12-12 16:59:19 +00:00
|
|
|
});
|
|
|
|
return defered.promise;
|
|
|
|
}
|
2017-10-27 11:56:40 +00:00
|
|
|
|
2017-12-12 16:59:19 +00:00
|
|
|
tmp
|
2017-12-14 15:07:16 +00:00
|
|
|
.then(data => {
|
2017-12-12 16:59:19 +00:00
|
|
|
if (data.error && data.error !== '0') {
|
|
|
|
defered.reject(data);
|
|
|
|
} else {
|
|
|
|
defered.resolve(data);
|
|
|
|
}
|
|
|
|
})
|
2017-12-14 15:07:16 +00:00
|
|
|
.catch(error => {
|
2018-01-29 11:39:33 +00:00
|
|
|
if(error.status && error.status === -1){
|
2018-01-25 12:43:47 +00:00
|
|
|
defered.reject({data: 'request_timeout_genericreq', url });
|
|
|
|
}else {
|
2018-01-08 09:59:23 +00:00
|
|
|
defered.reject(error);
|
2017-10-20 19:12:08 +00:00
|
|
|
}
|
2017-12-12 16:59:19 +00:00
|
|
|
});
|
2017-10-20 19:12:08 +00:00
|
|
|
|
2017-12-12 16:59:19 +00:00
|
|
|
return defered.promise;
|
|
|
|
};
|
2017-11-02 09:22:46 +00:00
|
|
|
|
2017-12-12 16:59:19 +00:00
|
|
|
return {
|
|
|
|
request: (method, path, payload = null) => {
|
|
|
|
let defered = $q.defer();
|
2017-10-20 19:12:08 +00:00
|
|
|
|
2017-12-12 16:59:19 +00:00
|
|
|
if (!method || !path) {
|
|
|
|
defered.reject(prepError({
|
|
|
|
'error': -1,
|
|
|
|
'message': 'Missing parameters'
|
|
|
|
}));
|
|
|
|
return defered.promise;
|
|
|
|
}
|
2017-10-20 19:12:08 +00:00
|
|
|
|
2017-12-12 16:59:19 +00:00
|
|
|
_request(method, path, payload)
|
|
|
|
.then((data) => defered.resolve(data))
|
2018-01-25 12:43:47 +00:00
|
|
|
.catch(error => {
|
|
|
|
if(error.status && error.status === 401){
|
|
|
|
defered.reject(error);
|
|
|
|
} else {
|
|
|
|
defered.reject(prepError(error));
|
|
|
|
}
|
|
|
|
});
|
2017-10-27 11:56:40 +00:00
|
|
|
|
2017-12-12 16:59:19 +00:00
|
|
|
return defered.promise;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|