Fix API initialization, closes #861

This commit is contained in:
Alexander Zobnin
2020-05-20 14:54:21 +03:00
parent 4c462e72dc
commit d18e6cc675
3 changed files with 18 additions and 8 deletions

View File

@@ -62,12 +62,16 @@ export class ZabbixAPIConnector {
return this.zabbixAPICore.request(this.url, method, params, this.requestOptions, this.auth)
.catch(error => {
if (isNotAuthorized(error.data)) {
if (isNotInitialized(error.data)) {
// If API not initialized yet (auth is empty), login first
return this.loginOnce()
.then(() => this.request(method, params));
} else if (isNotAuthorized(error.data)) {
// Handle auth errors
this.loginErrorCount++;
if (this.loginErrorCount > this.maxLoginAttempts) {
this.loginErrorCount = 0;
return null;
return Promise.resolve();
} else {
return this.loginOnce()
.then(() => this.request(method, params));
@@ -237,7 +241,7 @@ export class ZabbixAPIConnector {
};
return this.request('item.get', params)
.then(utils.expandItems);
.then(items => utils.expandItems(items));
}
getMacros(hostids) {
@@ -662,6 +666,10 @@ function filterTriggersByAcknowledge(triggers, acknowledged) {
}
}
function isNotInitialized(message) {
return message === "Not initialized";
}
function isNotAuthorized(message) {
return (
message === "Session terminated, re-login, please." ||

View File

@@ -19,7 +19,7 @@ export class ZabbixAPICore {
if (auth === "") {
// Reject immediately if not authenticated
return Promise.reject(new ZabbixAPIError({data: "Not authorised."}));
return Promise.reject(new ZabbixAPIError({data: "Not initialized"}));
} else if (auth) {
// Set auth parameter only if it needed
requestData.auth = auth;

View File

@@ -86,10 +86,12 @@ function cacheRequest(func, funcName, funcScope, self) {
} else {
return func.apply(funcScope, arguments)
.then(result => {
if (result !== undefined) {
cacheObject[hash] = {
value: result,
timestamp: Date.now()
};
}
return result;
});
}