<commit_before>/** @namespace Client.API */
'use strict';

var _ = require('lodash');
var $ = require('preconditions').singleton();
var util = require('util');
var async = require('async');
var events = require('events');
var WalletUtils = require('bitcore-wallet-utils');
var Bitcore = WalletUtils.Bitcore;
var sjcl = require('sjcl');
var url = require('url');
var querystring = require('querystring');

var request;
if (process && !process.browser) {
  request = require('request');
} else {
  request = require('browser-request');
}

var PayPro = require('./paypro');
var log = require('./log');
var Credentials = require('./credentials');
var Verifier = require('./verifier');
var Package = require('../package.json');
var ClientError = require('./errors/clienterror');
var Errors = require('./errors/errordefinitions');



var BASE_URL = 'http://localhost:3232/bws/api';

/**
 * @desc ClientAPI constructor.
 *
 * @param {Object} opts
 * @constructor
 */
function API(opts) {
  opts = opts || {};

  this.verbose = !!opts.verbose;
  this.request = opts.request || request;
  this.baseUrl = opts.baseUrl || BASE_URL;
  var parsedUrl = url.parse(this.baseUrl);
  this.basePath = parsedUrl.path;
  this.baseHost = parsedUrl.protocol + '//' + parsedUrl.host;
  this.payProHttp = null; // Only for testing
  this.doNotVerifyPayPro = opts.doNotVerifyPayPro;

  this.timeout = opts.timeout || 50000;


  if (this.verbose) {
    log.setLevel('debug');
  } else {
    log.setLevel('info');
  }
};
util.inherits(API, events.EventEmitter);

API.privateKeyEncryptionOpts = {
  iter: 10000
};

API.prototype.initNotifications = function(cb) {
  log.warn('DEPRECATED: use initialize() instead.');
  this.initialize(cb);
};

API.prototype.initialize = function(cb) {
  $.checkState(this.credentials);

  var self = this;
  self._initNotifications(cb);
};

API.prototype.dispose = function(cb) {
  var self = this;
  if (self.notificationsIntervalId) {
    clearInterval(self.notificationsIntervalId);
    self.notificationsIntervalId = null;
  }

  return cb();
};

API.prototype._initNotifications = function(cb) {
  var self = this;

  var NOTIFICATION_INTERVAL = 5; // in seconds

  var lastNotificationId;
  self.notificationsIntervalId = setInterval(function() {
    var url = '/v1/notifications/';
    if (lastNotificationId) url += '?notificationId=' + lastNotificationId;

    self.getNotifications({
      lastNotificationId: lastNotificationId
    }, function(err, notifications) {
      if (err) {
        log.warn('Error receiving notifications.');
        log.debug(err);
        return;
      }
      if (notifications.length > 0) {
        lastNotificationId = _.last(notifications).id;
      }

      _.each(notifications, function(notification) {
        self.emit('notification', notification);
      });
    });
  }, NOTIFICATION_INTERVAL * 1000);
};

/**
 * Encrypt a message
 * @private
 * @static
 * @memberof Client.API
 * @param {String} message
 * @param {String} encryptingKey
 */
API._encryptMessage = function(message, encryptingKey) {
  if (!message) return null;
  return WalletUtils.encryptMessage(message, encryptingKey);
};

/**
 * Decrypt a message
 * @private
 * @static
 * @memberof Client.API
 * @param {String} message
 * @param {String} encryptingKey
 */
API._decryptMessage = function(message, encryptingKey) {
  if (!message) return '';
  try {
    return WalletUtils.decryptMessage(message, encryptingKey);<commit_msg>make notification interval configurable and allow modifying at runtime<commit_after>/** @namespace Client.API */
'use strict';

var _ = require('lodash');
var $ = require('preconditions').singleton();
var util = require('util');
var async = require('async');
var events = require('events');
var WalletUtils = require('bitcore-wallet-utils');
var Bitcore = WalletUtils.Bitcore;
var sjcl = require('sjcl');
var url = require('url');
var querystring = require('querystring');

var request;
if (process && !process.browser) {
  request = require('request');
} else {
  request = require('browser-request');
}

var PayPro = require('./paypro');
var log = require('./log');
var Credentials = require('./credentials');
var Verifier = require('./verifier');
var Package = require('../package.json');
var ClientError = require('./errors/clienterror');
var Errors = require('./errors/errordefinitions');



var BASE_URL = 'http://localhost:3232/bws/api';

/**
 * @desc ClientAPI constructor.
 *
 * @param {Object} opts
 * @constructor
 */
function API(opts) {
  opts = opts || {};

  this.verbose = !!opts.verbose;
  this.request = opts.request || request;
  this.baseUrl = opts.baseUrl || BASE_URL;
  var parsedUrl = url.parse(this.baseUrl);
  this.basePath = parsedUrl.path;
  this.baseHost = parsedUrl.protocol + '//' + parsedUrl.host;
  this.payProHttp = null; // Only for testing
  this.doNotVerifyPayPro = opts.doNotVerifyPayPro;

  this.timeout = opts.timeout || 50000;


  if (this.verbose) {
    log.setLevel('debug');
  } else {
    log.setLevel('info');
  }
};
util.inherits(API, events.EventEmitter);

API.privateKeyEncryptionOpts = {
  iter: 10000
};

API.prototype.initNotifications = function(cb) {
  log.warn('DEPRECATED: use initialize() instead.');
  this.initialize(cb);
};

API.prototype.initialize = function(cb) {
  $.checkState(this.credentials);

  var self = this;
  self._initNotifications();
  return cb();
};

API.prototype.dispose = function(cb) {
  var self = this;
  self._disposeNotifications();
  return cb();
};

API.prototype._initNotifications = function(opts) {
  var self = this;

  opts = opts || {};

  var interval = opts.notificationIntervalSeconds || 5;

  var lastNotificationId;
  self.notificationsIntervalId = setInterval(function() {
    var url = '/v1/notifications/';
    if (lastNotificationId) url += '?notificationId=' + lastNotificationId;

    self.getNotifications({
      lastNotificationId: lastNotificationId
    }, function(err, notifications) {
      if (err) {
        log.warn('Error receiving notifications.');
        log.debug(err);
        return;
      }
      if (notifications.length > 0) {
        lastNotificationId = _.last(notifications).id;
      }

      _.each(notifications, function(notification) {
        self.emit('notification', notification);
      });
    });
  }, interval * 1000);
};

API.prototype._disposeNotifications = function() {
  if (self.notificationsIntervalId) {
    clearInterval(self.notificationsIntervalId);
    self.notificationsIntervalId = null;
  }
};


/**
 * Reset notification polling with new interval
 * @memberof Client.API
 * @param {Numeric} notificationIntervalSeconds - use 0 to pause notifications
 */
API.prototype.setNotificationsInterval = function(notificationIntervalSeconds) {
  var self = this;
  self._disposeNotifications();
  if (notificationIntervalSeconds > 0) {
    self._initNotifications({
      notificationIntervalSeconds: notificationIntervalSeconds
    });
  }
};


/**
 * Encrypt a message
 * @private
 * @static
 * @memberof Client.API
 * @param {String} message
 * @param {String} encryptingKey
 */
API._encryptMessage = function(message, encryptingKey) {
  if (!message) return null;
  return WalletUtils.encryptMessage(message, encryptingKey);
};

/**
 * Decrypt a message
 * @private
 * @static
 * @memberof Client.API
 * @param {String} message
 * @param {String} encryptingKey
 */
API._decryptMessage = function(message, encryptingKey) {
  if (!message) return '';
  try {
    return WalletUtils.decryptMessage(message, encryptingKey);
{'fraction_non_alphanumeric': 0.14789200691581328, 'fraction_numerical': 0.005851842000265992, 'mean_word_length': 4.050369375419745, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4953873', 'n_tokens_mistral': 2338, 'n_tokens_neox': 2262, 'n_words': 503}