wazuh-kibana-app/public/services/testAPI.js

61 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-10-20 19:12:08 +00:00
import chrome from 'ui/chrome';
const app = require('ui/modules').get('app/wazuh', []);
app.service('testAPI', function ($q, $http, $location, $rootScope, appState) {
2017-10-30 10:35:30 +00:00
return {
check_stored: data => {
const headers = {headers:{ "Content-Type": 'application/json' },timeout: 4000};
if(appState.getUserCode()) headers.headers.code = appState.getUserCode();
2017-10-30 10:35:30 +00:00
let defered = $q.defer();
2017-12-28 10:25:35 +00:00
$http
.post(chrome.addBasePath('/api/wazuh-api/checkStoredAPI'), data,headers)
2017-12-28 10:25:35 +00:00
.then(response => {
2017-10-30 10:35:30 +00:00
if (response.error) {
defered.reject(response);
} else {
defered.resolve(response);
}
})
.catch(error => {
if(error.status && error.status === -1){
defered.reject({data: 'request_timeout_checkstored'});
2017-12-28 10:25:35 +00:00
} else {
2017-10-30 10:35:30 +00:00
defered.reject(error);
}
});
return defered.promise;
},
check: data => {
const headers = {headers:{ "Content-Type": 'application/json' },timeout: 4000};
if(appState.getUserCode()) headers.headers.code = appState.getUserCode();
2017-10-30 10:35:30 +00:00
let defered = $q.defer();
2017-12-28 10:25:35 +00:00
const url = chrome.addBasePath("/api/wazuh-api/checkAPI");
$http
.post(url, data, headers)
2017-12-28 10:25:35 +00:00
.then(response => {
2017-10-30 10:35:30 +00:00
if (response.error) {
defered.reject(response);
} else {
defered.resolve(response);
}
})
.catch(error => {
if(error.data && error.data.message && error.data.message.includes('ENOTFOUND')) {
2017-12-21 14:23:09 +00:00
defered.reject({data: 'invalid_url'});
} else if(error.data && error.data.message && error.data.message.includes('ECONNREFUSED')) {
defered.reject({data: 'invalid_port'});
} else if(error.status && error.status === -1){
defered.reject({data: 'request_timeout_checkapi'});
} else if (error.data && error.data.message && error.data.message === 'wrong_credentials') {
defered.reject({data: 'wrong_credentials'});
2017-12-28 10:25:35 +00:00
} else if(error.data && ((error.data.message && error.data.message === 'socket hang up') || (parseInt(error.data.error) === 5))) {
defered.reject({data:'socket_hang_up',https: (data.url && data.url.includes('https'))});
} else {
2017-10-30 10:35:30 +00:00
defered.reject(error);
}
});
return defered.promise;
}
};
});