var superagent = require( 'superagent' ); // require('superagent-proxy')(superagent); var sha1 = require( 'sha1' ); var BASEURL = 'https://api.cn.rong.io'; var APPKEY = undefined; var APPSECRET = undefined; var TIMESTAMP = undefined; var SIGNATURE = undefined; var NONCE = undefined; var HEADERS = {}; var FORMAT = 'json'; // var proxy = process.env.http_proxy exports.init = function( appKey, appSecret ) { APPKEY = appKey; APPSECRET = appSecret; NONCE = parseInt( Math.random() * 0xffffff ); TIMESTAMP = Date.parse( new Date() )/1000; SIGNATURE = sha1( APPSECRET + NONCE + TIMESTAMP ); HEADERS['App-Key'] = APPKEY; HEADERS['Nonce'] = NONCE; HEADERS['Timestamp'] = TIMESTAMP; HEADERS['Signature'] = SIGNATURE; HEADERS['Content-Type'] = 'application/x-www-form-urlencoded'; } exports.request = function( api, params, format, callback ) { var f = 'json'; if( (typeof format ) == 'function' ) { // Use default format 'json' callback = format; } else { f = ( format === 'xml' ? 'xml' : 'json' ); } superagent.agent() .post( BASEURL + api + '.' + f ) .set( HEADERS ) .send( params ) .end( function( err, result ) { if( err ) { return callback( err, null ); } else { return callback( null, result.text ); } } ); } exports.requestWithSameFields = function( api, params, fieldAndValues, format, callback ) { var f = 'json'; if( (typeof format ) == 'function' ) { // Use default format 'json' callback = format; } else { f = ( format === 'xml' ? 'xml' : 'json' ); } var agent = superagent.agent() .post( BASEURL + api + '.' + f ) // .proxy(proxy) .set( HEADERS ); for( var i=0; i