Advertisement
Guest User

index

a guest
Mar 26th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. let http = require('http');
  4. let qs = require('querystring');
  5.  
  6. const defaults = {
  7.   host: 'api.sms-reg.com'
  8.   ,port: 80
  9. };
  10.  
  11. const model = {
  12.   getNum: ['country', 'service', 'appid']
  13.   ,setReady: ['tzid']
  14.   ,getState: ['tzid']
  15.   ,getOperations: ['opstate', 'count']
  16.   ,setOperationOk: ['tzid']
  17.   ,setOperationRevise: ['tzid']
  18.   ,setOperationOver: ['tzid']
  19.   ,getNumRepeat: ['tzid']
  20.   ,getNumRepeatOffline: ['tzid', 'type', 'log', 'pas']
  21.   ,setOperationUsed: ['tzid']
  22.   ,vsimGet: ['country', 'period']
  23.   ,vsimGetSMS: ['order_id']
  24.   ,orderAdd: ['count', 'country', 'service', 'options', 'name', 'age', 'gander', 'city']
  25.   ,orderGetByID: ['order_id']
  26.   ,listOrders: ['count']
  27.   ,setOrderAccOk: ['histid']
  28.   ,setOrderAccRevise: ['histid']
  29.   ,getBalance: []
  30.   ,setRate: ['rate']
  31. };
  32.  
  33. /** Class representing SMS-REG.com API */
  34. class SmsReg {
  35.   /**
  36.    * Create a SmsReg
  37.    * @constructor
  38.    * @arg {string} key - service access API key
  39.    * @arg {string} [host=api.sms-reg.com] - API host base url
  40.    * @arg {number} [port=80] - host's port
  41.    */
  42.   constructor(key, host, port) {
  43.     if (!key || typeof key !== 'string') {
  44.       throw new Error('API key is required');
  45.     }
  46.     this._key = key;
  47.     this._host = host || defaults.host;
  48.     this._port = port || defaults.port;
  49.   }
  50.  
  51.   /**
  52.    * @return Balance information or Error
  53.    */
  54.   getNum(country, service, appid) { return this._method('getNum', country, service, appid); }
  55.   setReady(tzid) { return this._method('setReady', tzid); }
  56.   getState(tzid) { return this._method('getState', tzid); }
  57.   getOperations(opstate, count) { return this._method('getOperations', opstate, count); }
  58.   setOperationOk(tzid) { return this._method('setOperationOk', tzid); }
  59.   setOperationRevise(tzid) { return this._method('setOperationRevise', tzid); }
  60.   setOperationOver(tzid) { return this._method('setOperationOver', tzid); }
  61.   getNumRepeat(tzid) { return this._method('getNumRepeat', tzid); }
  62.   getNumRepeatOffline(tzid, type, log, pas) { return this._method('getNumRepeatOffline', tzid, type, log, pas); }
  63.   setOperationUsed(tzid) { return this._method('setOperationUsed', tzid); }
  64.   vsimGet(country, period) { return this._method('vsimGet', country, period); }
  65.   vsimGetSMS(order_id) { return this._method('vsimGetSMS', order_id); }
  66.   orderAdd(count, country, service, options, name, age, gander, city) { return this._method('orderAdd', count, country, service, options, name, age, gander, city); }
  67.   orderGetByID(order_id) { return this._method('orderGetByID', order_id); }
  68.   listOrders(count) { return this._method('listOrders', count); }
  69.   setOrderAccOk(histid) { return this._method('setOrderAccOk', histid); }
  70.   setOrderAccRevise(histid) { return this._method('setOrderAccRevise', histid); }
  71.   getBalance() { return this._method('getBalance'); }
  72.   setRate(rate) { return this._method('setRate', rate); }
  73.  
  74.   _makeOptions(keys) {
  75.     let r = {};
  76.     for (let i = 0; i < keys.length; ++i) {
  77.       let v = arguments[i + 1];
  78.       if (v !== void 0) {
  79.         r[keys[i]] = v;
  80.       }
  81.     }
  82.     return r;
  83.   }
  84.  
  85.   _method(name) {
  86.     console.log('_method', name);
  87.     return new Promise((resolve, reject) => {
  88.       arguments[0] = model[arguments[0]];
  89.       let options = this._makeOptions.apply(this, arguments);
  90.       options['apikey'] = this._key;
  91.       console.log(options);
  92.       let req = http.request({
  93.         hostname: this._host,
  94.         port: this._port,
  95.         path: `/${name}.php?${qs.stringify(options)}`
  96.       }, res => {
  97.         let data = String();
  98.         res.on('data', chunk => {data += chunk; });
  99.         res.on('end', () => {
  100.           try {
  101.             let o = JSON.parse(data);
  102.             if (!o.response === void 0) {
  103.               reject(`Unexpected response ${data}`);
  104.             }
  105.             if (o.response === 'ERROR') {
  106.               reject(o.error_msg);
  107.             }
  108.             resolve(o);
  109.           } catch (e) {
  110.             reject(`Can't parse '${data}' with error ${e}`);
  111.          }
  112.        });
  113.      });
  114.      req.on('error', error => { reject(error.message); });
  115.      req.end();
  116.    });
  117.  }
  118. };
  119.  
  120. module.exports = SmsReg;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement