Skip to content

Instantly share code, notes, and snippets.

@alexanderzobnin
Created April 24, 2017 16:35
Show Gist options
  • Save alexanderzobnin/afe55d2376300d07985b69f4374fec42 to your computer and use it in GitHub Desktop.
Save alexanderzobnin/afe55d2376300d07985b69f4374fec42 to your computer and use it in GitHub Desktop.
Zabbix API JS login
request(method, params) {
return this.zabbixAPICore.request(this.url, method, params, this.requestOptions, this.auth)
.catch(error => {
if (isNotAuthorized(error.data)) {
// Handle auth errors
this.loginErrorCount++;
if (this.loginErrorCount > this.maxLoginAttempts) {
this.loginErrorCount = 0;
return null;
} else {
return this.loginOnce()
.then(() => this.request(method, params));
}
} else {
// Handle API errors
let message = error.data ? error.data : error.statusText;
this.alertAPIError(message);
}
});
}
function isNotAuthorized(message) {
return (
message === "Session terminated, re-login, please." ||
message === "Not authorised." ||
message === "Not authorized."
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment