Advertisement
Guest User

zabbixapi-client

a guest
Apr 13th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. var RequestError = require('./errors').RequestError,
  2. AuthError = require('./errors').AuthError,
  3. ApiError = require('./errors').APIError,
  4. request = require('request');
  5.  
  6. var handleError = function (callback) {
  7. return function(reqerr, res, body) {
  8. var error = null;
  9. if (body) {
  10. try {
  11. body = JSON.parse(body);
  12. } catch(e) {
  13. return callback(e, null);
  14. }
  15. }
  16. if (reqerr) {
  17. error = reqerr;
  18. }
  19. else if (res.statusCode !== 200) {
  20. error = new RequestError("Status code "+ res.statusCode);
  21. } else
  22. if (body && body.error) {
  23. if (body.error.code === -32602) {
  24. error = new AuthError(body.error.message + ' ' + body.error.data);
  25. } else {
  26. error = new ApiError(body.error.message + ' ' + body.error.data);
  27. }
  28. } else
  29. if (!body || (body.result === undefined && !body.error)) {
  30. error = new Error("UnknownError");
  31. }
  32. callback(error, (body && body.result !== undefined) ? body.result : null);
  33. };
  34. };
  35.  
  36. var ApiClient = function(url, user, password) {
  37. if (!url) {
  38. throw new Error("Missing required argument: url");
  39. }
  40. if (!user) {
  41. throw new Error("Missing required argument: user");
  42. }
  43. if(!password) {
  44. throw new Error("Missing required argument: password");
  45. }
  46.  
  47. this.authid = null;
  48. this.rpcid = 0;
  49. this.url = url;
  50. this.user = user;
  51. this.password = password;
  52. this.version = null;
  53. this.authlock = false;
  54. this._pendingQueries = [];
  55. };
  56.  
  57. ApiClient.prototype._rpcRequest = function(method, params, callback) {
  58. request({
  59. method: 'POST',
  60. uri: this.url,
  61. headers: { 'content-type': 'application/json-rpc' },
  62. body: JSON.stringify({
  63. jsonrpc : '2.0',
  64. id: ++this.rpcid,
  65. auth: this.authid,
  66. method: method,
  67. params: params
  68. })
  69. }, handleError(callback));
  70. };
  71.  
  72. ApiClient.prototype._getApiVersion = function(callback) {
  73. request({
  74. method: 'POST',
  75. uri: this.url,
  76. headers: { 'content-type': 'application/json-rpc' },
  77. body: JSON.stringify({
  78. jsonrpc : '2.0',
  79. id: ++this.rpcid,
  80. method: 'apiinfo.version',
  81. params: []
  82. })
  83. }, handleError(callback));
  84. };
  85.  
  86. ApiClient.prototype._authenticate = function () {
  87. this.authlock = true;
  88. this._rpcRequest('user.login', {
  89. user: this.user,
  90. password: this.password
  91. }, function (err, res) {
  92. if (!err) {
  93. this.authid = res;
  94. if (!this.version) {
  95. this._getApiVersion(function (err, res) {
  96. if (!err) {
  97. this.version = res;
  98. }
  99. this.authlock = false;
  100. this._processPendingQueries(err);
  101. }.bind(this));
  102. }
  103. else {
  104. this.authlock = false;
  105. this._processPendingQueries();
  106. }
  107. }
  108. else {
  109. this.authlock = false;
  110. this._processPendingQueries(err);
  111. }
  112.  
  113. }.bind(this));
  114. };
  115.  
  116. ApiClient.prototype._processPendingQueries = function(err) {
  117. while (this._pendingQueries.length > 0) {
  118. var q = this._pendingQueries.pop();
  119. // Если результатом аутентификации была ошибка - все запросы в очереди завершим с ошибкой
  120.  
  121. if (err) {
  122. q.callback(err);
  123. } else {
  124. this.query(q.method, q.options, q.callback);
  125. }
  126. }
  127. };
  128.  
  129. ApiClient.prototype.query = function (method, options, callback) {
  130. if (!this.authid || this.authlock) {
  131. this._pendingQueries.push({
  132. method: method,
  133. options: options,
  134. callback: callback
  135. });
  136. return this._authenticate();
  137. }
  138. this._rpcRequest(method, options, callback);
  139.  
  140. };
  141.  
  142. module.exports = ApiClient;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement