rongrequest.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. var superagent = require( 'superagent' );
  2. var sha1 = require('./util').sha1;
  3. var BASEURL = 'http://api.cn.ronghub.com';
  4. var BASEURL_SMS = 'http://api.sms.ronghub.com';
  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. exports.init = function( appKey, appSecret ) {
  13. APPKEY = appKey;
  14. APPSECRET = appSecret;
  15. NONCE = parseInt( Math.random() * 0xffffff );
  16. TIMESTAMP = Date.parse( new Date() )/1000;
  17. SIGNATURE = sha1( APPSECRET + NONCE + TIMESTAMP );
  18. HEADERS['App-Key'] = APPKEY;
  19. HEADERS['Nonce'] = NONCE;
  20. HEADERS['Timestamp'] = TIMESTAMP;
  21. HEADERS['Signature'] = SIGNATURE;
  22. }
  23. exports.request = function( api, params, format, callback ) {
  24. return requestWithDomain( BASEURL, api, params, format, callback );
  25. }
  26. exports.requestSMS = function( api, params, format, callback ) {
  27. return requestWithDomain( BASEURL_SMS, api, params, format, callback );
  28. }
  29. requestWithDomain = function( base_url, api, params, format, callback ) {
  30. var f = 'json';
  31. if( (typeof format ) == 'function' ) { // Use default format 'json'
  32. callback = format;
  33. }
  34. else {
  35. f = ( format === 'xml' ? 'xml' : 'json' );
  36. }
  37. superagent.agent()
  38. .post( base_url + api + '.' + f )
  39. .set( HEADERS )
  40. .type( 'form' )
  41. .send( params )
  42. .end( function( err, result ) {
  43. if( err ) {
  44. return callback( err, result ? result.text : null );
  45. }
  46. else {
  47. return callback( null, result.text );
  48. }
  49. } );
  50. }
  51. exports.requestWithSameFields = function( api, params, fieldAndValues, format, callback ) {
  52. var f = 'json';
  53. if( (typeof format ) == 'function' ) { // Use default format 'json'
  54. callback = format;
  55. }
  56. else {
  57. f = ( format === 'xml' ? 'xml' : 'json' );
  58. }
  59. var agent = superagent.agent()
  60. .post( BASEURL + api + '.' + f )
  61. .set( HEADERS );
  62. for( var i=0; i<fieldAndValues.length; ++i ) {
  63. var field = fieldAndValues[i].field;
  64. var values = fieldAndValues[i].values;
  65. for( var j=0; j<values.length; ++j ) {
  66. agent = agent.send( encodeURIComponent(field) + '=' + encodeURIComponent(values[j]) );
  67. }
  68. }
  69. for( var k in params ) {
  70. agent = agent.send( encodeURIComponent(k) + '=' + encodeURIComponent(params[k]) );
  71. }
  72. agent.end( function( err, result ) {
  73. if( err ) {
  74. return callback( err, result ? result.text : null );
  75. }
  76. else {
  77. return callback( null, result.text );
  78. }
  79. } );
  80. }
  81. exports.validateSignature = function( nonce, timestamp, signature ) {
  82. return sha1( APPSECRET + nonce + timestamp ) == signature;
  83. }
  84. exports.requestWithHeaders = function( api, params, format, headers, callback ) {
  85. var f = 'json';
  86. if( (typeof format ) == 'function' ) { // Use default format 'json'
  87. callback = format;
  88. }
  89. else {
  90. f = ( format === 'xml' ? 'xml' : 'json' );
  91. }
  92. // Attach the specified headers.
  93. var _headers = {};
  94. for( var k in HEADERS ) {
  95. _headers[k] = headers[k] ? headers[k] : HEADERS[k];
  96. }
  97. superagent.agent()
  98. .post( BASEURL + api + '.' + f )
  99. .set( _headers )
  100. .send( params )
  101. .end( function( err, result ) {
  102. if( err ) {
  103. return callback( err, result ? result.text : null );
  104. }
  105. else {
  106. return callback( null, result.text );
  107. }
  108. } );
  109. }
  110. exports.get = function( api, params, callback ) {
  111. superagent.agent()
  112. .get( BASEURL_SMS + api + '.json' )
  113. .query( params )
  114. .end( function( err, result ) {
  115. if( err ) {
  116. return callback( err, result ? result.text : null );
  117. }
  118. else {
  119. return callback( null, result.text );
  120. }
  121. } );
  122. }