toshi
Version:
node.js wrapper for the toshi bitcoin node
86 lines (78 loc) • 2.32 kB
JavaScript
// Generated by CoffeeScript 1.8.0
(function() {
var Req, request;
request = require('request');
module.exports = Req = (function() {
function Req(url) {
this.url = url;
}
Req.prototype.get = function(path, params, cb) {
var url;
if (typeof params === 'function') {
cb = params;
} else {
if ((params['limit'] != null) && (params['offset'] != null)) {
path += '?limit=' + params['limit'] + '&offset=' + params['offset'];
} else if (params['limit'] != null) {
path += '?limit=' + params['limit'];
} else if (params['offset'] != null) {
path += '?offset=' + params['offset'];
} else {
return cb(new Error('bad params: options are limit and offset'));
}
}
url = this.url + path;
return request({
url: url,
method: "GET",
timeout: 15000
}, function(err, response, body) {
var error, result;
if (err || (response.statusCode !== 200 && response.statusCode !== 400)) {
return cb(new Error(err != null ? err : response.statusCode));
}
try {
result = JSON.parse(body);
} catch (_error) {
error = _error;
return cb(null, {
messsage: body.toString()
});
}
if (result.message != null) {
return cb(new Error(result.message));
}
return cb(null, result);
});
};
Req.prototype.post = function(path, body, cb) {
var url;
url = this.url + path;
return request({
url: url,
method: "POST",
timeout: 15000,
body: body,
json: true
}, function(err, response, body) {
var error, result;
if (err || (response.statusCode !== 200 && response.statusCode !== 400)) {
return cb(new Error(err != null ? err : response.statusCode));
}
try {
result = JSON.parse(body);
} catch (_error) {
error = _error;
return cb(null, {
messsage: body.toString()
});
}
if (result.message != null) {
return cb(new Error(result.message));
}
return cb(null, result);
});
};
return Req;
})();
}).call(this);