rongrequest.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var superagent = require( 'superagent' );
  2. // require('superagent-proxy')(superagent);
  3. var sha1 = require( 'sha1' );
  4. var BASEURL = 'https://api.cn.rong.io';
  5. var APPKEY = undefined;
  6. var APPSECRET = undefined;
  7. var TIMESTAMP = undefined;
  8. var SIGNATURE = undefined;
  9. var NONCE = undefined;
  10. var HEADERS = {};
  11. var FORMAT = 'json';
  12. // var proxy = process.env.http_proxy
  13. exports.init = function( appKey, appSecret ) {
  14. APPKEY = appKey;
  15. APPSECRET = appSecret;
  16. NONCE = parseInt( Math.random() * 0xffffff );
  17. TIMESTAMP = Date.parse( new Date() )/1000;
  18. SIGNATURE = sha1( APPSECRET + NONCE + TIMESTAMP );
  19. HEADERS['App-Key'] = APPKEY;
  20. HEADERS['Nonce'] = NONCE;
  21. HEADERS['Timestamp'] = TIMESTAMP;
  22. HEADERS['Signature'] = SIGNATURE;
  23. HEADERS['Content-Type'] = 'application/x-www-form-urlencoded';
  24. }
  25. exports.request = function( api, params, format, callback ) {
  26. var f = 'json';
  27. if( (typeof format ) == 'function' ) { // Use default format 'json'
  28. callback = format;
  29. }
  30. else {
  31. f = ( format === 'xml' ? 'xml' : 'json' );
  32. }
  33. superagent.agent()
  34. .post( BASEURL + api + '.' + f )
  35. .set( HEADERS )
  36. .send( params )
  37. .end( function( err, result ) {
  38. if( err ) {
  39. return callback( err, null );
  40. }
  41. else {
  42. return callback( null, result.text );
  43. }
  44. } );
  45. }
  46. exports.requestWithSameFields = function( api, params, fieldAndValues, format, callback ) {
  47. var f = 'json';
  48. if( (typeof format ) == 'function' ) { // Use default format 'json'
  49. callback = format;
  50. }
  51. else {
  52. f = ( format === 'xml' ? 'xml' : 'json' );
  53. }
  54. var agent = superagent.agent()
  55. .post( BASEURL + api + '.' + f )
  56. // .proxy(proxy)
  57. .set( HEADERS );
  58. for( var i=0; i<fieldAndValues.length; ++i ) {
  59. var field = fieldAndValues[i].field;
  60. var values = fieldAndValues[i].values;
  61. for( var j=0; j<values.length; ++j ) {
  62. agent = agent.send( encodeURIComponent(field) + '=' + encodeURIComponent(values[j]) );
  63. }
  64. }
  65. for( var k in params ) {
  66. agent = agent.send( encodeURIComponent(k) + '=' + encodeURIComponent(params[k]) );
  67. }
  68. agent.end( function( err, result ) {
  69. if( err ) {
  70. return callback( err, null );
  71. }
  72. else {
  73. return callback( null, result.text );
  74. }
  75. } );
  76. }
  77. exports.validateSignature = function( nonce, timestamp, signature ) {
  78. return sha1( APPSECRET + nonce + timestamp ) == signature;
  79. }
  80. exports.requestWithHeaders = function( api, params, format, headers, callback ) {
  81. var f = 'json';
  82. if( (typeof format ) == 'function' ) { // Use default format 'json'
  83. callback = format;
  84. }
  85. else {
  86. f = ( format === 'xml' ? 'xml' : 'json' );
  87. }
  88. // Attach the specified headers.
  89. var _headers = {};
  90. for( var k in HEADERS ) {
  91. _headers[k] = headers[k] ? headers[k] : HEADERS[k];
  92. }
  93. superagent.agent()
  94. .post( BASEURL + api + '.' + f )
  95. .set( _headers )
  96. .send( params )
  97. .end( function( err, result ) {
  98. if( err ) {
  99. return callback( err, null );
  100. }
  101. else {
  102. return callback( null, result.text );
  103. }
  104. } );
  105. }