forked from bitpay/bitcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request bitpay#462 from matiu/feat/multiple-insights
adds multiple insight server support
- Loading branch information
Showing
5 changed files
with
270 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
var _ = require('lodash'); | ||
var async = require('async'); | ||
var $ = require('preconditions').singleton(); | ||
|
||
var log = require('npmlog'); | ||
log.debug = log.verbose; | ||
|
||
/** | ||
* Query a server, using one of the given options | ||
* | ||
* @param {Object} opts | ||
* @param {Array} opts.hosts Array of hosts to query. Until the first success one. | ||
* @param {Array} opts.path Path to request in each server | ||
*/ | ||
var requestList = function(args, cb) { | ||
$.checkArgument(args.hosts); | ||
request = args.request || require('request'); | ||
|
||
if (!_.isArray(args.hosts)) | ||
args.hosts = [args.hosts]; | ||
|
||
var urls = _.map(args.hosts, function(x) { | ||
return (x + args.path); | ||
}); | ||
var nextUrl, result, success; | ||
|
||
async.whilst( | ||
function() { | ||
nextUrl = urls.shift(); | ||
return nextUrl && !success; | ||
}, | ||
function(a_cb) { | ||
args.uri = nextUrl; | ||
request(args, function(err, res, body) { | ||
if (err) { | ||
log.warn('REQUEST FAIL: ' + nextUrl + ' ERROR: ' + err); | ||
} | ||
|
||
if (res) { | ||
success = !!res.statusCode.toString().match(/^[1234]../); | ||
if (!success) { | ||
log.warn('REQUEST FAIL: ' + nextUrl + ' STATUS CODE: ' + res.statusCode); | ||
} | ||
} | ||
|
||
result = [err, res, body]; | ||
return a_cb(); | ||
}); | ||
}, | ||
function(err) { | ||
if (err) return cb(err); | ||
return cb(result[0], result[1], result[2]); | ||
} | ||
); | ||
}; | ||
|
||
module.exports = requestList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.