2018-04-22 20:16:38 +00:00
|
|
|
/*
|
|
|
|
* Wazuh app - API test service
|
2019-01-14 16:36:47 +00:00
|
|
|
* Copyright (C) 2015-2019 Wazuh, Inc.
|
2018-04-22 20:16:38 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-09-10 08:32:49 +00:00
|
|
|
import chrome from 'ui/chrome';
|
2018-04-21 11:31:47 +00:00
|
|
|
|
2018-09-11 09:52:54 +00:00
|
|
|
export class ApiTester {
|
2018-12-12 11:47:05 +00:00
|
|
|
/**
|
|
|
|
* Class constructor
|
2018-12-13 10:02:53 +00:00
|
|
|
* @param {*} $http
|
|
|
|
* @param {*} appState
|
|
|
|
* @param {*} wzMisc
|
|
|
|
* @param {*} wazuhConfig
|
2018-12-12 11:47:05 +00:00
|
|
|
*/
|
2018-09-10 08:32:49 +00:00
|
|
|
constructor($http, appState, wzMisc, wazuhConfig) {
|
|
|
|
this.$http = $http;
|
|
|
|
this.appState = appState;
|
|
|
|
this.wzMisc = wzMisc;
|
|
|
|
this.wazuhConfig = wazuhConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
async checkStored(data) {
|
|
|
|
try {
|
|
|
|
const configuration = this.wazuhConfig.getConfig();
|
2019-04-10 13:07:56 +00:00
|
|
|
const timeout = configuration ? configuration.timeout : 20000;
|
2018-09-10 08:32:49 +00:00
|
|
|
const headers = {
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
2019-04-10 13:07:56 +00:00
|
|
|
timeout: timeout || 20000
|
2018-09-10 08:32:49 +00:00
|
|
|
};
|
2019-10-09 10:18:50 +00:00
|
|
|
const payload = { id: data };
|
2019-04-15 13:05:06 +00:00
|
|
|
const result = await this.$http.post(
|
|
|
|
chrome.addBasePath('/api/check-stored-api'),
|
2019-10-09 10:13:14 +00:00
|
|
|
payload,
|
2019-04-15 13:05:06 +00:00
|
|
|
headers
|
|
|
|
);
|
2018-09-10 08:32:49 +00:00
|
|
|
|
2019-04-15 13:05:06 +00:00
|
|
|
this.appState.setPatternSelector(configuration['ip.selector']);
|
2018-09-10 08:32:49 +00:00
|
|
|
|
2019-04-15 13:05:06 +00:00
|
|
|
if (result.error) {
|
|
|
|
return Promise.reject(result);
|
2018-09-10 08:32:49 +00:00
|
|
|
}
|
2019-04-15 13:05:06 +00:00
|
|
|
return result;
|
2018-09-10 08:32:49 +00:00
|
|
|
} catch (error) {
|
2019-03-27 15:26:44 +00:00
|
|
|
if (((error || {}).data || {}).code === 3099) {
|
|
|
|
// Do nothing
|
|
|
|
return 3099;
|
|
|
|
}
|
2018-09-10 08:32:49 +00:00
|
|
|
if (error.status && error.status === -1) {
|
|
|
|
this.wzMisc.setApiIsDown(true);
|
|
|
|
}
|
|
|
|
return Promise.reject(error);
|
2018-08-20 07:00:50 +00:00
|
|
|
}
|
2018-09-10 08:32:49 +00:00
|
|
|
}
|
2018-08-20 07:00:50 +00:00
|
|
|
|
2018-09-10 08:32:49 +00:00
|
|
|
async check(data) {
|
|
|
|
try {
|
|
|
|
const { timeout } = this.wazuhConfig.getConfig();
|
2018-08-20 07:00:50 +00:00
|
|
|
|
2018-09-10 08:32:49 +00:00
|
|
|
const headers = {
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
2019-04-10 13:07:56 +00:00
|
|
|
timeout: timeout || 20000
|
2018-09-10 08:32:49 +00:00
|
|
|
};
|
2018-04-22 20:16:38 +00:00
|
|
|
|
2018-10-01 07:56:50 +00:00
|
|
|
const url = chrome.addBasePath('/api/check-api');
|
2018-09-10 08:32:49 +00:00
|
|
|
const response = await this.$http.post(url, data, headers);
|
2018-04-02 10:44:43 +00:00
|
|
|
|
2018-09-10 08:32:49 +00:00
|
|
|
if (response.error) {
|
|
|
|
return Promise.reject(response);
|
|
|
|
}
|
2018-08-20 07:00:50 +00:00
|
|
|
|
2018-09-10 08:32:49 +00:00
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
return Promise.reject(error);
|
2018-08-20 07:00:50 +00:00
|
|
|
}
|
2018-09-10 08:32:49 +00:00
|
|
|
}
|
2018-08-20 07:00:50 +00:00
|
|
|
}
|