Skip to content

Commit

Permalink
Merge pull request bitpay#494 from isocolsky/user-agent
Browse files Browse the repository at this point in the history
Add 'User-Agent' header when sending requests to Insight
  • Loading branch information
matiu committed Apr 8, 2016
2 parents 897c73b + 017d64c commit 1168824
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 35 deletions.
3 changes: 2 additions & 1 deletion lib/blockchainexplorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function BlockChainExplorer(opts) {
return new Insight({
network: network,
url: url,
apiPrefix: opts.apiPrefix
apiPrefix: opts.apiPrefix,
userAgent: opts.userAgent,
});
default:
throw new Error('Provider ' + provider + ' not supported.');
Expand Down
29 changes: 17 additions & 12 deletions lib/blockchainexplorers/insight.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function Insight(opts) {
this.apiPrefix = opts.apiPrefix || '/api';
this.network = opts.network || 'livenet';
this.hosts = opts.url;
this.userAgent = opts.userAgent || 'bws';
};


Expand All @@ -27,6 +28,16 @@ var _parseErr = function(err, res) {
return "Error querying the blockchain";
};

Insight.prototype._doRequest = function(args, cb) {
var opts = {
hosts: this.hosts,
headers: {
'User-Agent': this.userAgent,
}
};
requestList(_.defaults(args, opts), cb);
};

Insight.prototype.getConnectionInfo = function() {
return 'Insight (' + this.network + ') @ ' + this.hosts;
};
Expand All @@ -38,14 +49,13 @@ Insight.prototype.getUtxos = function(addresses, cb) {
var url = this.url + this.apiPrefix + '/addrs/utxo';
var args = {
method: 'POST',
hosts: this.hosts,
path: this.apiPrefix + '/addrs/utxo',
json: {
addrs: [].concat(addresses).join(',')
},
};

requestList(args, function(err, res, unspent) {
this._doRequest(args, function(err, res, unspent) {
if (err || res.statusCode !== 200) return cb(_parseErr(err, res));
return cb(null, unspent);
});
Expand All @@ -57,14 +67,13 @@ Insight.prototype.getUtxos = function(addresses, cb) {
Insight.prototype.broadcast = function(rawTx, cb) {
var args = {
method: 'POST',
hosts: this.hosts,
path: this.apiPrefix + '/tx/send',
json: {
rawtx: rawTx
},
};

requestList(args, function(err, res, body) {
this._doRequest(args, function(err, res, body) {
if (err || res.statusCode !== 200) return cb(_parseErr(err, res));
return cb(null, body ? body.txid : null);
});
Expand All @@ -73,12 +82,11 @@ Insight.prototype.broadcast = function(rawTx, cb) {
Insight.prototype.getTransaction = function(txid, cb) {
var args = {
method: 'GET',
hosts: this.hosts,
path: this.apiPrefix + '/tx/' + txid,
json: true,
};

requestList(args, function(err, res, tx) {
this._doRequest(args, function(err, res, tx) {
if (res && res.statusCode == 404) return cb();
if (err || res.statusCode !== 200)
return cb(_parseErr(err, res));
Expand All @@ -94,14 +102,13 @@ Insight.prototype.getTransactions = function(addresses, from, to, cb) {

var args = {
method: 'POST',
hosts: this.hosts,
path: this.apiPrefix + '/addrs/txs' + (qs.length > 0 ? '?' + qs.join('&') : ''),
json: {
addrs: [].concat(addresses).join(',')
},
};

requestList(args, function(err, res, txs) {
this._doRequest(args, function(err, res, txs) {
if (err || res.statusCode !== 200) return cb(_parseErr(err, res));

if (_.isObject(txs) && txs.items)
Expand All @@ -119,12 +126,11 @@ Insight.prototype.getAddressActivity = function(address, cb) {

var args = {
method: 'GET',
hosts: this.hosts,
path: self.apiPrefix + '/addr/' + address,
json: true,
};

requestList(args, function(err, res, result) {
this._doRequest(args, function(err, res, result) {
if (res && res.statusCode == 404) return cb();
if (err || res.statusCode !== 200)
return cb(_parseErr(err, res));
Expand All @@ -142,11 +148,10 @@ Insight.prototype.estimateFee = function(nbBlocks, cb) {

var args = {
method: 'GET',
hosts: this.hosts,
path: path,
json: true,
};
requestList(args, function(err, res, body) {
this._doRequest(args, function(err, res, body) {
if (err || res.statusCode !== 200) return cb(_parseErr(err, res));
return cb(null, body);
});
Expand Down
3 changes: 3 additions & 0 deletions lib/blockchainmonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var Lock = require('./lock');

var Notification = require('./model/notification');

var WalletService = require('./server');

function BlockchainMonitor() {};

BlockchainMonitor.prototype.start = function(opts, cb) {
Expand All @@ -36,6 +38,7 @@ BlockchainMonitor.prototype.start = function(opts, cb) {
provider: config.provider,
network: network,
url: config.url,
userAgent: WalletService.getServiceVersion(),
});
}
$.checkState(explorer);
Expand Down
22 changes: 0 additions & 22 deletions lib/emailservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ var Utils = require('./common/utils');
var Storage = require('./storage');
var MessageBroker = require('./messagebroker');
var Lock = require('./lock');
var BlockchainExplorer = require('./blockchainexplorer');

var Model = require('./model');

Expand Down Expand Up @@ -99,27 +98,6 @@ EmailService.prototype.start = function(opts, cb) {
self.lock = opts.lock || new Lock(opts.lockOpts);
done();
},
function(done) {
self.explorers = _.indexBy(_.map(['livenet', 'testnet'], function(network) {
var explorer;
if (opts.blockchainExplorers) {
explorer = opts.blockchainExplorers[network];
} else {
var config = {}
if (opts.blockchainExplorerOpts && opts.blockchainExplorerOpts[network]) {
config = opts.blockchainExplorerOpts[network];
}
var explorer = new BlockchainExplorer({
provider: config.provider,
network: network,
url: config.url,
});
}
$.checkState(explorer);
return explorer;
}), 'network');
done();
},
function(done) {
self.mailer = opts.mailer || nodemailer.createTransport(opts.emailOpts);
done();
Expand Down
1 change: 1 addition & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ WalletService.prototype._getBlockchainExplorer = function(network) {
// TODO: provider should be configurable
opts.provider = 'insight';
opts.network = network;
opts.userAgent = WalletService.getServiceVersion();
this.blockchainExplorer = new BlockchainExplorer(opts);
}

Expand Down

0 comments on commit 1168824

Please sign in to comment.