Guest User

Untitled

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