Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored to include Call Multi function, for multiple calls on the same connection #39

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 68 additions & 33 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,29 @@ module.exports = function (classes){
* In HTTP mode, we get to submit exactly one message and receive up to n
* messages.
*/
connectHttp : function (method, params, opts, callback){
connectHttp : function (calls, opts, callback){
if (_.isFunction(opts)) {
callback = opts;
opts = {};
}
opts = opts || {};
calls = calls || [];

var id = 1, self = this;

// First we encode the request into JSON
var requestJSON = JSON.stringify({
'id' : id,
'method' : method,
'params' : params,
'jsonrpc': '2.0'
});
var req;

if (calls && calls instanceof Array) {
req = [];
for (var c = 0; c < calls.length; c++){
var call = calls[c] || {};
EventEmitter.trace('-->', 'Http call (method ' + call.method + '): ' + JSON.stringify(call.params));
req.push({'id': id, 'method' : call.method, 'params' : call.params, 'jsonrpc': '2.0'});
}
}

var requestJSON = JSON.stringify(req);

var headers = {};

Expand All @@ -69,6 +76,9 @@ module.exports = function (classes){
method : 'POST',
headers : headers
};
if (opts.agent){
options.agent = opts.agent;
}
var request;
if(opts.https === true) {
if(opts.rejectUnauthorized !== undefined) {
Expand Down Expand Up @@ -255,37 +265,62 @@ module.exports = function (classes){
opts = {};
}
opts = opts || {};
EventEmitter.trace('-->', 'Http call (method ' + method + '): ' + JSON.stringify(params));

this.connectHttp(method, params, opts, function connectHttp(id, request, response){
// Check if response object exists.
if (!response) {
callback(new Error('Have no response object'));
return;
this.call_multi([{method: method, params: params}], opts, callback);
},
call_multi : function (calls, opts, callback){
// Calls object to be array of object in format [{"method": "method1", "params": ["params1"]}]
if (_.isFunction(opts)) {
callback = opts;
opts = {};
}
opts = opts || {};
calls = calls || [];
this.connectHttp(calls, opts, function connectHttp(id, request, response){
// Check if response object exists.
if (!response) {
callback(new Error('Have no response object'));
return;
}

var data = '';
var data = '';

response.on('data', function responseData(chunk){
data += chunk;
});
response.on('data', function responseData(chunk){
data += chunk;
});

response.on('end', function responseEnd(){
if (response.statusCode !== 200) {
callback(new Error('"' + response.statusCode + '"' + data))
;
return;
}
var decoded = JSON.parse(data);
if (_.isFunction(callback)) {
if (!decoded.error) {
decoded.error = null;
}
callback(decoded.error, decoded.result);
}
response.on('end', function responseEnd(){
if (response.statusCode !== 200) {
callback(new Error('"' + response.statusCode + '"' + data))
;
return;
}
var decoded = JSON.parse(data);
if (_.isFunction(callback)) {
var return_error = null;
if (decoded instanceof Array && decoded.length) {
for (var d in decoded){
if (decoded[d] && !decoded[d].error){
decoded[d].error = null;
} else {
if(!return_error){
return_error = [];
}
return_error.push(decoded[d].error);
}
}
if (decoded.length === 1){
// If singular call, unpack from array
decoded = decoded[0];
}
callback(return_error, decoded);
} else {
callback(new Error('Have no response object'));
return;
}
}
});
});
});
}
}
});

return Client;
Expand Down