Refactor: improved working with promises in zabbixAPICore.service.
This commit is contained in:
@@ -17,8 +17,7 @@ class ZabbixAPICoreService {
|
|||||||
* @return {object} response.result
|
* @return {object} response.result
|
||||||
*/
|
*/
|
||||||
request(api_url, method, params, options, auth) {
|
request(api_url, method, params, options, auth) {
|
||||||
var deferred = this.$q.defer();
|
let requestData = {
|
||||||
var requestData = {
|
|
||||||
jsonrpc: '2.0',
|
jsonrpc: '2.0',
|
||||||
method: method,
|
method: method,
|
||||||
params: params,
|
params: params,
|
||||||
@@ -27,20 +26,19 @@ class ZabbixAPICoreService {
|
|||||||
|
|
||||||
if (auth === "") {
|
if (auth === "") {
|
||||||
// Reject immediately if not authenticated
|
// Reject immediately if not authenticated
|
||||||
deferred.reject({data: "Not authorised."});
|
return Promise.reject(new ZabbixAPIError({data: "Not authorised."}));
|
||||||
return deferred.promise;
|
|
||||||
} else if (auth) {
|
} else if (auth) {
|
||||||
// Set auth parameter only if it needed
|
// Set auth parameter only if it needed
|
||||||
requestData.auth = auth;
|
requestData.auth = auth;
|
||||||
}
|
}
|
||||||
|
|
||||||
var requestOptions = {
|
let requestOptions = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
url: api_url,
|
||||||
|
data: requestData,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
}
|
||||||
url: api_url,
|
|
||||||
data: requestData
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Set request options for basic auth
|
// Set request options for basic auth
|
||||||
@@ -51,24 +49,23 @@ class ZabbixAPICoreService {
|
|||||||
requestOptions.headers.Authorization = options.basicAuth;
|
requestOptions.headers.Authorization = options.basicAuth;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.backendSrv.datasourceRequest(requestOptions)
|
return this.datasourceRequest(requestOptions);
|
||||||
.then((response) => {
|
}
|
||||||
// General connection issues
|
|
||||||
if (!response.data) {
|
datasourceRequest(requestOptions) {
|
||||||
deferred.reject(response);
|
return this.backendSrv.datasourceRequest(requestOptions)
|
||||||
}
|
.then(response => {
|
||||||
|
if (!response.data) {
|
||||||
|
return Promise.reject(new ZabbixAPIError({data: "General Error, no data"}));
|
||||||
|
} else if (response.data.error) {
|
||||||
|
|
||||||
// Handle Zabbix API errors
|
// Handle Zabbix API errors
|
||||||
else if (response.data.error) {
|
return Promise.reject(new ZabbixAPIError(response.data.error));
|
||||||
deferred.reject(response.data.error);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
deferred.resolve(response.data.result);
|
// Success
|
||||||
}, (error) => {
|
return response.data.result;
|
||||||
deferred.reject(error.err);
|
});
|
||||||
});
|
|
||||||
|
|
||||||
return deferred.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -76,7 +73,7 @@ class ZabbixAPICoreService {
|
|||||||
* @return {string} auth token
|
* @return {string} auth token
|
||||||
*/
|
*/
|
||||||
login(api_url, username, password, options) {
|
login(api_url, username, password, options) {
|
||||||
var params = {
|
let params = {
|
||||||
user: username,
|
user: username,
|
||||||
password: password
|
password: password
|
||||||
};
|
};
|
||||||
@@ -93,15 +90,18 @@ class ZabbixAPICoreService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Define zabbix API exception type
|
// Define zabbix API exception type
|
||||||
function ZabbixException(error) {
|
export class ZabbixAPIError {
|
||||||
this.code = error.code;
|
constructor(error) {
|
||||||
this.errorType = error.message;
|
this.code = error.code;
|
||||||
this.message = error.data;
|
this.name = error.data;
|
||||||
}
|
this.message = error.data;
|
||||||
|
this.data = error.data;
|
||||||
|
}
|
||||||
|
|
||||||
ZabbixException.prototype.toString = function() {
|
toString() {
|
||||||
return this.errorType + ": " + this.message;
|
return this.name + ": " + this.message;
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
angular
|
angular
|
||||||
.module('grafana.services')
|
.module('grafana.services')
|
||||||
|
|||||||
Reference in New Issue
Block a user