meshblu-http
Version:
Meshblu HTTP API
162 lines (142 loc) • 4.7 kB
JavaScript
// Generated by CoffeeScript 1.12.6
(function() {
var MeshbluRequest, SrvFailover, _, dns, request, url,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
dns = require('dns');
_ = require('lodash');
request = require('request');
SrvFailover = require('srv-failover');
url = require('url');
MeshbluRequest = (function() {
function MeshbluRequest(options, dependencies) {
var domain, protocol, ref, resolveSrv, secure, service;
if (options == null) {
options = {};
}
if (dependencies == null) {
dependencies = {};
}
this._resolveBaseUrl = bind(this._resolveBaseUrl, this);
this._doRequest = bind(this._doRequest, this);
this._addDefaultOptions = bind(this._addDefaultOptions, this);
this.put = bind(this.put, this);
this.post = bind(this.post, this);
this.patch = bind(this.patch, this);
this.get = bind(this.get, this);
this["delete"] = bind(this["delete"], this);
this.dns = dependencies.dns, this.request = dependencies.request;
if (this.request == null) {
this.request = request;
}
this.requestOptions = options.request;
this.protocol = options.protocol, this.hostname = options.hostname, this.port = options.port;
service = options.service, domain = options.domain, secure = options.secure, resolveSrv = options.resolveSrv;
if (!resolveSrv) {
return;
}
protocol = 'https';
if (secure === false) {
protocol = 'http';
}
this.srvFailover = new SrvFailover({
service: service,
domain: domain,
protocol: protocol
}, {
dns: (ref = dependencies.dns) != null ? ref : dns
});
}
MeshbluRequest.prototype["delete"] = function(uri, options, callback) {
return this._doRequest({
method: 'delete',
uri: uri,
options: options
}, callback);
};
MeshbluRequest.prototype.get = function(uri, options, callback) {
return this._doRequest({
method: 'get',
uri: uri,
options: options
}, callback);
};
MeshbluRequest.prototype.patch = function(uri, options, callback) {
return this._doRequest({
method: 'patch',
uri: uri,
options: options
}, callback);
};
MeshbluRequest.prototype.post = function(uri, options, callback) {
return this._doRequest({
method: 'post',
uri: uri,
options: options
}, callback);
};
MeshbluRequest.prototype.put = function(uri, options, callback) {
return this._doRequest({
method: 'put',
uri: uri,
options: options
}, callback);
};
MeshbluRequest.prototype._addDefaultOptions = function(options, arg) {
var baseUrl, method, uri;
method = arg.method, uri = arg.uri, baseUrl = arg.baseUrl;
return _.defaults({}, options, this.requestOptions, {
method: method,
uri: uri,
baseUrl: baseUrl
});
};
MeshbluRequest.prototype._doRequest = function(arg, callback) {
var method, options, uri;
method = arg.method, uri = arg.uri, options = arg.options;
return this._resolveBaseUrl(uri, (function(_this) {
return function(error, baseUrl) {
if (error != null) {
return callback(error);
}
return _this.request(_this._addDefaultOptions(options, {
method: method,
uri: uri,
baseUrl: baseUrl
}), callback);
};
})(this));
};
MeshbluRequest.prototype._resolveBaseUrl = function(uri, callback) {
if (this.srvFailover == null) {
return callback(null, url.format({
protocol: this.protocol,
hostname: this.hostname,
port: this.port
}));
}
return this.srvFailover.resolveUrl((function(_this) {
return function(error, baseUrl) {
if (error != null) {
return callback(error);
}
return _this.request({
baseUrl: baseUrl,
uri: uri,
method: 'options'
}, function(error, response) {
if ((error != null) || response.statusCode !== 204) {
_this.srvFailover.markBadUrl(baseUrl, {
ttl: 60000
});
return _this._resolveBaseUrl(uri, callback);
}
return callback(null, baseUrl);
});
};
})(this));
};
return MeshbluRequest;
})();
module.exports = MeshbluRequest;
}).call(this);
//# sourceMappingURL=meshblu-request.js.map