Comparing version 0.0.10 to 0.0.11
@@ -48,3 +48,3 @@ /* | ||
(config.encoding ? config.encoding : 'utf-8'); | ||
logger.trace('pack urlencode data succ ServiceID=' + config.serviceID); | ||
logger.trace('pack urlencode data succ ServiceID=' + config.serviceID + ' data=' + buffer); | ||
return buffer; | ||
@@ -51,0 +51,0 @@ }; |
@@ -41,3 +41,3 @@ /* | ||
} | ||
logger.trace('pack json data succ ServiceID=' + config.serviceID); | ||
logger.trace('pack json data succ ServiceID=' + config.serviceID + ' data=' + buffer); | ||
return buffer; | ||
@@ -44,0 +44,0 @@ }; |
@@ -30,3 +30,3 @@ /* | ||
config.headers['Content-Type'] = 'application/json'; | ||
logger.trace('pack querystring data succ ServiceID=' + config.serviceID); | ||
logger.trace('pack querystring data succ ServiceID=' + config.serviceID + ' data=' + JSON.stringify(config.query)); | ||
return null; | ||
@@ -33,0 +33,0 @@ }; |
@@ -42,3 +42,3 @@ /* | ||
} | ||
logger.trace('pack string data succ ServiceID=' + config.serviceID); | ||
logger.trace('pack string data succ ServiceID=' + config.serviceID + ' data=' + buffer); | ||
return buffer; | ||
@@ -45,0 +45,0 @@ }; |
@@ -23,12 +23,16 @@ /* | ||
function RAL(serviceName, options){ | ||
return new RalRunner(serviceName, options); | ||
function RAL(serviceName, data, options){ | ||
return new RalRunner(serviceName, data, options); | ||
} | ||
function RalRunner(serviceName, options){ | ||
logger.trace('request start'); | ||
function RalRunner(serviceName, data, options){ | ||
var me = this; | ||
this._retryTimes = 0; | ||
this._requestID = Math.ceil(now()*100000); | ||
this.serviceName = serviceName; | ||
logger.trace('request start requestID=' + this._requestID); | ||
EventEmitter.call(this); | ||
options = options || {}; | ||
options.data = data; | ||
var payload, request; | ||
@@ -38,23 +42,26 @@ var timer = this.timer = new Timer(['request','talk', 'pack', 'write', 'read', 'unpack']); | ||
var conf = this.conf = config.getConf(serviceName); | ||
if (!conf){ | ||
process.nextTick(function(){ | ||
me.emit('error', new Error('invalid service name')) | ||
}); | ||
}else{ | ||
var context = conf.context; | ||
//normalize conf and options for merge to request options | ||
context.protocol.normalizeConfig(conf); | ||
context.protocol.normalizeConfig(options); | ||
ralUtil.merge(conf, options); | ||
this.doRequest(); | ||
var context = conf.context; | ||
//normalize conf and options for merge to request options | ||
context.protocol.normalizeConfig(conf); | ||
context.protocol.normalizeConfig(options); | ||
this._retryTimes = 0; | ||
this._requestID = Math.ceil(now()*100000); | ||
ralUtil.merge(conf, options); | ||
this.doRequest(); | ||
this.on('retry', function(err){ | ||
if (this._retryTimes >= conf.retry){ | ||
me.emit('error', err); | ||
}else{ | ||
this._retryTimes ++; | ||
timer.start('request'); | ||
timer.start('talk'); | ||
logger.trace('start retry request'); | ||
me.doRequest(payload); | ||
} | ||
}) | ||
this.on('retry', function(err){ | ||
if (this._retryTimes >= conf.retry){ | ||
me.emit('error', err); | ||
}else{ | ||
this._retryTimes ++; | ||
timer.start('request'); | ||
timer.start('talk'); | ||
logger.trace('start retry request'); | ||
me.doRequest(payload); | ||
} | ||
}) | ||
} | ||
} | ||
@@ -61,0 +68,0 @@ |
{ | ||
"name": "node-ral", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "a rpc client for node", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -77,9 +77,9 @@ node-ral | ||
var request = RAL('SOME_SERVICE', { | ||
//RAL(serviceName, data, opt) | ||
var request = RAL('SOME_SERVICE',{ | ||
'name': 'hefangshi', | ||
'city': 'Beijing', | ||
'gender': 'Male' | ||
}, { | ||
path: '/user/info' | ||
data: { | ||
'name': 'hefangshi', | ||
'city': 'Beijing', | ||
'gender': 'Male' | ||
} | ||
}); | ||
@@ -86,0 +86,0 @@ |
@@ -85,6 +85,4 @@ /* | ||
var req = RAL('GET_QS_SERV', { | ||
data: { | ||
msg: 'hi', | ||
name: '何方石' | ||
} | ||
}); | ||
@@ -106,3 +104,3 @@ req.on('data', function(data){ | ||
}); | ||
var req = RAL('GET_QS_SERV', { | ||
var req = RAL('GET_QS_SERV',null, { | ||
path : '/404', | ||
@@ -130,3 +128,3 @@ tag: 404 | ||
}); | ||
var req = RAL('GET_QS_SERV', { | ||
var req = RAL('GET_QS_SERV',null, { | ||
path: '/largecontent' | ||
@@ -149,5 +147,4 @@ }); | ||
var req = RAL('GET_QS_SERV', { | ||
data: { | ||
msg: 'hi' | ||
}, | ||
msg: 'hi' | ||
},{ | ||
encoding: 'blah' | ||
@@ -165,8 +162,7 @@ }); | ||
}); | ||
var req = RAL('GET_QS_SERV', { | ||
var req = RAL('GET_QS_SERV',{ | ||
msg: 'hi', | ||
name: 'timeout' | ||
}, { | ||
path: '/timeout', | ||
data: { | ||
msg: 'hi', | ||
name: 'timeout' | ||
}, | ||
retry: 2, | ||
@@ -189,6 +185,4 @@ timeout: 100 | ||
var req = RAL('POST_QS_SERV', { | ||
data: { | ||
msg: 'hi', | ||
name: '何方石' | ||
} | ||
msg: 'hi', | ||
name: '何方石' | ||
}); | ||
@@ -204,2 +198,16 @@ req.on('data', function(data){ | ||
}); | ||
it('should throw error when use a invalid service', function (done) { | ||
before(function( ok ){ | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('POST_QS_SERV_INVALID', { | ||
msg: 'hi', | ||
name: '何方石' | ||
}); | ||
req.on('error', function(err){ | ||
err.toString().should.be.match(/invalid service name/); | ||
done(); | ||
}); | ||
}); | ||
}); |
249374
6743