add userAgent opt

This commit is contained in:
Ivan Socolsky 2016-04-07 14:52:23 -03:00
parent 533ddaf3b4
commit 4c9b685e1c
No known key found for this signature in database
GPG Key ID: FAECE6A05FAA4F56
4 changed files with 25 additions and 13 deletions

View File

@ -30,7 +30,8 @@ function BlockChainExplorer(opts) {
return new Insight({ return new Insight({
network: network, network: network,
url: url, url: url,
apiPrefix: opts.apiPrefix apiPrefix: opts.apiPrefix,
userAgent: opts.userAgent,
}); });
default: default:
throw new Error('Provider ' + provider + ' not supported.'); throw new Error('Provider ' + provider + ' not supported.');

View File

@ -15,6 +15,7 @@ function Insight(opts) {
this.apiPrefix = opts.apiPrefix || '/api'; this.apiPrefix = opts.apiPrefix || '/api';
this.network = opts.network || 'livenet'; this.network = opts.network || 'livenet';
this.hosts = opts.url; this.hosts = opts.url;
this.userAgent = opts.userAgent || 'bws';
}; };
@ -27,6 +28,18 @@ var _parseErr = function(err, res) {
return "Error querying the blockchain"; return "Error querying the blockchain";
}; };
Insight.prototype._doRequest = function(args, cb) {
var opts = {
hosts: this.hosts,
headers: {
'User-Agent': this.userAgent,
}
};
console.log('*** [insight.js ln38] opts:', opts); // TODO
requestList(_.defaults(args, opts), cb);
};
Insight.prototype.getConnectionInfo = function() { Insight.prototype.getConnectionInfo = function() {
return 'Insight (' + this.network + ') @ ' + this.hosts; return 'Insight (' + this.network + ') @ ' + this.hosts;
}; };
@ -38,14 +51,13 @@ Insight.prototype.getUtxos = function(addresses, cb) {
var url = this.url + this.apiPrefix + '/addrs/utxo'; var url = this.url + this.apiPrefix + '/addrs/utxo';
var args = { var args = {
method: 'POST', method: 'POST',
hosts: this.hosts,
path: this.apiPrefix + '/addrs/utxo', path: this.apiPrefix + '/addrs/utxo',
json: { json: {
addrs: [].concat(addresses).join(',') 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)); if (err || res.statusCode !== 200) return cb(_parseErr(err, res));
return cb(null, unspent); return cb(null, unspent);
}); });
@ -57,14 +69,13 @@ Insight.prototype.getUtxos = function(addresses, cb) {
Insight.prototype.broadcast = function(rawTx, cb) { Insight.prototype.broadcast = function(rawTx, cb) {
var args = { var args = {
method: 'POST', method: 'POST',
hosts: this.hosts,
path: this.apiPrefix + '/tx/send', path: this.apiPrefix + '/tx/send',
json: { json: {
rawtx: rawTx 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)); if (err || res.statusCode !== 200) return cb(_parseErr(err, res));
return cb(null, body ? body.txid : null); return cb(null, body ? body.txid : null);
}); });
@ -73,12 +84,11 @@ Insight.prototype.broadcast = function(rawTx, cb) {
Insight.prototype.getTransaction = function(txid, cb) { Insight.prototype.getTransaction = function(txid, cb) {
var args = { var args = {
method: 'GET', method: 'GET',
hosts: this.hosts,
path: this.apiPrefix + '/tx/' + txid, path: this.apiPrefix + '/tx/' + txid,
json: true, json: true,
}; };
requestList(args, function(err, res, tx) { this._doRequest(args, function(err, res, tx) {
if (res && res.statusCode == 404) return cb(); if (res && res.statusCode == 404) return cb();
if (err || res.statusCode !== 200) if (err || res.statusCode !== 200)
return cb(_parseErr(err, res)); return cb(_parseErr(err, res));
@ -94,14 +104,13 @@ Insight.prototype.getTransactions = function(addresses, from, to, cb) {
var args = { var args = {
method: 'POST', method: 'POST',
hosts: this.hosts,
path: this.apiPrefix + '/addrs/txs' + (qs.length > 0 ? '?' + qs.join('&') : ''), path: this.apiPrefix + '/addrs/txs' + (qs.length > 0 ? '?' + qs.join('&') : ''),
json: { json: {
addrs: [].concat(addresses).join(',') 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 (err || res.statusCode !== 200) return cb(_parseErr(err, res));
if (_.isObject(txs) && txs.items) if (_.isObject(txs) && txs.items)
@ -119,12 +128,11 @@ Insight.prototype.getAddressActivity = function(address, cb) {
var args = { var args = {
method: 'GET', method: 'GET',
hosts: this.hosts,
path: self.apiPrefix + '/addr/' + address, path: self.apiPrefix + '/addr/' + address,
json: true, json: true,
}; };
requestList(args, function(err, res, result) { this._doRequest(args, function(err, res, result) {
if (res && res.statusCode == 404) return cb(); if (res && res.statusCode == 404) return cb();
if (err || res.statusCode !== 200) if (err || res.statusCode !== 200)
return cb(_parseErr(err, res)); return cb(_parseErr(err, res));
@ -142,11 +150,10 @@ Insight.prototype.estimateFee = function(nbBlocks, cb) {
var args = { var args = {
method: 'GET', method: 'GET',
hosts: this.hosts,
path: path, path: path,
json: true, 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)); if (err || res.statusCode !== 200) return cb(_parseErr(err, res));
return cb(null, body); return cb(null, body);
}); });

View File

@ -13,6 +13,8 @@ var Lock = require('./lock');
var Notification = require('./model/notification'); var Notification = require('./model/notification');
var WalletService = require('./server');
function BlockchainMonitor() {}; function BlockchainMonitor() {};
BlockchainMonitor.prototype.start = function(opts, cb) { BlockchainMonitor.prototype.start = function(opts, cb) {
@ -36,6 +38,7 @@ BlockchainMonitor.prototype.start = function(opts, cb) {
provider: config.provider, provider: config.provider,
network: network, network: network,
url: config.url, url: config.url,
userAgent: WalletService.getServiceVersion(),
}); });
} }
$.checkState(explorer); $.checkState(explorer);

View File

@ -864,6 +864,7 @@ WalletService.prototype._getBlockchainExplorer = function(network) {
// TODO: provider should be configurable // TODO: provider should be configurable
opts.provider = 'insight'; opts.provider = 'insight';
opts.network = network; opts.network = network;
opts.userAgent = WalletService.getServiceVersion();
this.blockchainExplorer = new BlockchainExplorer(opts); this.blockchainExplorer = new BlockchainExplorer(opts);
} }