add batched call support to RpcClient

This commit is contained in:
Stephen Pair 2013-07-18 16:21:48 -04:00
parent ab311fd95e
commit 0b7db07ef2

View File

@ -14,6 +14,14 @@ function ClassSpec(b) {
this.user = opts.user || 'user'; this.user = opts.user || 'user';
this.pass = opts.pass || 'pass'; this.pass = opts.pass || 'pass';
this.protocol = (opts.protocol == 'http') ? http : https; this.protocol = (opts.protocol == 'http') ? http : https;
this.batchedCalls = null;
}
RpcClient.prototype.batch = function(batchCallback, resultCallback) {
this.batchedCalls = [];
batchCallback();
rpc.call(this, this.batchedCalls, resultCallback);
this.batchedCalls = null;
} }
var callspec = { var callspec = {
@ -90,10 +98,16 @@ function ClassSpec(b) {
function generateRPCMethods(constructor, apiCalls, rpc) { function generateRPCMethods(constructor, apiCalls, rpc) {
function createRPCMethod(methodName, argMap) { function createRPCMethod(methodName, argMap) {
return function() { return function() {
for (var i=0; i<arguments.length - 1; i++) { var limit = arguments.length - 1;
if(this.batchedCalls) var limit = arguments.length;
for (var i=0; i<limit; i++) {
if(argMap[i]) arguments[i] = argMap[i](arguments[i]); if(argMap[i]) arguments[i] = argMap[i](arguments[i]);
}; };
rpc.call(this, methodName, slice(arguments, 0, arguments.length - 1), arguments[arguments.length - 1]); if(this.batchedCalls) {
this.batchedCalls.push({jsonrpc: '2.0', method: methodName, params: slice(arguments)});
} else {
rpc.call(this, {method: methodName, params: slice(arguments, 0, arguments.length - 1)}, arguments[arguments.length - 1]);
}
}; };
}; };
@ -119,14 +133,9 @@ function ClassSpec(b) {
} }
} }
function rpc(method, params, callback) { function rpc(request, callback) {
var self = this; var self = this;
var request; var request;
if(params) {
request = {method: method, params: params};
} else {
request = {method: method};
}
request = JSON.stringify(request); request = JSON.stringify(request);
var auth = Buffer(self.user + ':' + self.pass).toString('base64'); var auth = Buffer(self.user + ':' + self.pass).toString('base64');