<commit_before>var request = require('request');
var async = require('async');
var url     = require('url');

function Insight(href, opts) {
  this.opts = opts || {};
  this.url = url.parse(href);
  if(!url.port)
    url.port = 3000;
}

Insight.prototype.listUnspent = function(addresses, callback) {
  var self = this;

  var all = [];
  async.each(addresses,function(addr, a_c) {
    self.request('/addr/'+addr+'/unspents', function(err,res) {
      if (res.length > 0) all = all.concat(res);
      return a_c();
    });
  }, function(err) {
    return callback(undefined,all);
  });
};

Insight.prototype.request = function(path, callback) {
  request({uri: this.url.href  + path, method: 'GET'}, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      var ret;
      try {
        ret = JSON.parse(body);
      } catch (e) {
        callback({message: "Wrong response from insight"});
        return;
      }
      callback(undefined, ret);   
      return;
    }

    if(error) {
      if(error.message == 'connect ECONNREFUSED') {
        console.log('insight error: connection refused');
        callback({message: "Could not connect to the insight server"});
        return;<commit_msg>support the new /utxo path in insight<commit_after>var request = require('request');
var async = require('async');
var url     = require('url');

function Insight(href, opts) {
  this.opts = opts || {};
  this.url = url.parse(href);
  if(!url.port)
    url.port = 3000;
}

Insight.prototype.listUnspent = function(addresses, callback) {
  var self = this;

  if (!addresses || !addresses.length) return callback(undefined,[]);

  var all = [];
  async.each(addresses,function(addr, a_c) {
    self.request('/addr/'+addr+'/utxo', function(err,res) {
      if (res && res.length > 0) all = all.concat(res);
      return a_c();
    });
  }, function(err) {
    return callback(undefined,all);
  });
};

Insight.prototype.request = function(path, callback) {
  var u = this.url.href  + path;
  request({uri: u, method: 'GET'}, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      var ret;
      try {
        ret = JSON.parse(body);
      } catch (e) {
        callback({message: "Wrong response from insight"});
        return;
      }
      callback(undefined, ret);   
      return;
    }

    if(error) {
      if(error.message == 'connect ECONNREFUSED') {
        console.log('insight error: connection refused');
        callback({message: "Could not connect to the insight server"});
        return;
{'fraction_non_alphanumeric': 0.16140350877192983, 'fraction_numerical': 0.006237816764132553, 'mean_word_length': 2.9476923076923076, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7366473', 'n_tokens_mistral': 832, 'n_tokens_neox': 805, 'n_words': 194}