Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var RequestError = require('./errors').RequestError,
- AuthError = require('./errors').AuthError,
- ApiError = require('./errors').APIError,
- request = require('request');
- var handleError = function (callback) {
- return function(reqerr, res, body) {
- var error = null;
- if (body) {
- try {
- body = JSON.parse(body);
- } catch(e) {
- return callback(e, null);
- }
- }
- if (reqerr) {
- error = reqerr;
- }
- else if (res.statusCode !== 200) {
- error = new RequestError("Status code "+ res.statusCode);
- } else
- if (body && body.error) {
- if (body.error.code === -32602) {
- error = new AuthError(body.error.message + ' ' + body.error.data);
- } else {
- error = new ApiError(body.error.message + ' ' + body.error.data);
- }
- } else
- if (!body || (body.result === undefined && !body.error)) {
- error = new Error("UnknownError");
- }
- callback(error, (body && body.result !== undefined) ? body.result : null);
- };
- };
- var ApiClient = function(url, user, password) {
- if (!url) {
- throw new Error("Missing required argument: url");
- }
- if (!user) {
- throw new Error("Missing required argument: user");
- }
- if(!password) {
- throw new Error("Missing required argument: password");
- }
- this.authid = null;
- this.rpcid = 0;
- this.url = url;
- this.user = user;
- this.password = password;
- this.version = null;
- this.authlock = false;
- this._pendingQueries = [];
- };
- ApiClient.prototype._rpcRequest = function(method, params, callback) {
- request({
- method: 'POST',
- uri: this.url,
- headers: { 'content-type': 'application/json-rpc' },
- body: JSON.stringify({
- jsonrpc : '2.0',
- id: ++this.rpcid,
- auth: this.authid,
- method: method,
- params: params
- })
- }, handleError(callback));
- };
- ApiClient.prototype._getApiVersion = function(callback) {
- request({
- method: 'POST',
- uri: this.url,
- headers: { 'content-type': 'application/json-rpc' },
- body: JSON.stringify({
- jsonrpc : '2.0',
- id: ++this.rpcid,
- method: 'apiinfo.version',
- params: []
- })
- }, handleError(callback));
- };
- ApiClient.prototype._authenticate = function () {
- this.authlock = true;
- this._rpcRequest('user.login', {
- user: this.user,
- password: this.password
- }, function (err, res) {
- if (!err) {
- this.authid = res;
- if (!this.version) {
- this._getApiVersion(function (err, res) {
- if (!err) {
- this.version = res;
- }
- this.authlock = false;
- this._processPendingQueries(err);
- }.bind(this));
- }
- else {
- this.authlock = false;
- this._processPendingQueries();
- }
- }
- else {
- this.authlock = false;
- this._processPendingQueries(err);
- }
- }.bind(this));
- };
- ApiClient.prototype._processPendingQueries = function(err) {
- while (this._pendingQueries.length > 0) {
- var q = this._pendingQueries.pop();
- // Если результатом аутентификации была ошибка - все запросы в очереди завершим с ошибкой
- if (err) {
- q.callback(err);
- } else {
- this.query(q.method, q.options, q.callback);
- }
- }
- };
- ApiClient.prototype.query = function (method, options, callback) {
- if (!this.authid || this.authlock) {
- this._pendingQueries.push({
- method: method,
- options: options,
- callback: callback
- });
- return this._authenticate();
- }
- this._rpcRequest(method, options, callback);
- };
- module.exports = ApiClient;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement