meshblu-http
Version:
Meshblu HTTP API
1,042 lines (979 loc) • 36.9 kB
JavaScript
// Generated by CoffeeScript 1.12.6
(function() {
var MeshbluHttp, _, debug, stableStringify, url,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
slice = [].slice;
_ = require('lodash');
url = require('url');
debug = require('debug')('meshblu-http');
stableStringify = require('json-stable-stringify');
MeshbluHttp = (function() {
MeshbluHttp.SUBSCRIPTION_TYPES = ['broadcast', 'sent', 'received', 'config', 'broadcast.received', 'broadcast.sent', 'configure.received', 'configure.sent', 'message.received', 'message.sent'];
function MeshbluHttp(options, dependencies) {
var auth, bearerToken, domain, hostname, port, protocol, ref, request, resolveSrv, secure, service, token, uuid;
if (options == null) {
options = {};
}
this.dependencies = dependencies != null ? dependencies : {};
this._userError = bind(this._userError, this);
this._update = bind(this._update, this);
this._subscriptionUrl = bind(this._subscriptionUrl, this);
this._subscriptions = bind(this._subscriptions, this);
this._possiblySerializeHeaderValue = bind(this._possiblySerializeHeaderValue, this);
this._message = bind(this._message, this);
this._handleResponse = bind(this._handleResponse, this);
this._handleError = bind(this._handleError, this);
this._getRawRequestOptions = bind(this._getRawRequestOptions, this);
this._getMetadataHeaders = bind(this._getMetadataHeaders, this);
this._getDefaultRequestOptions = bind(this._getDefaultRequestOptions, this);
this._getDefaultHeaders = bind(this._getDefaultHeaders, this);
this._devices = bind(this._devices, this);
this._buildUrlRequest = bind(this._buildUrlRequest, this);
this._buildSrvRequest = bind(this._buildSrvRequest, this);
this._buildRequest = bind(this._buildRequest, this);
this._assertNoUrl = bind(this._assertNoUrl, this);
this._assertNoSrv = bind(this._assertNoSrv, this);
this.whoami = bind(this.whoami, this);
this.verify = bind(this.verify, this);
this._updateDangerously = bind(this._updateDangerously, this);
this.updateDangerously = bind(this.updateDangerously, this);
this.update = bind(this.update, this);
this.unregister = bind(this.unregister, this);
this.subscriptions = bind(this.subscriptions, this);
this.sign = bind(this.sign, this);
this.setPrivateKey = bind(this.setPrivateKey, this);
this.searchTokens = bind(this.searchTokens, this);
this.search = bind(this.search, this);
this.revokeTokenByQuery = bind(this.revokeTokenByQuery, this);
this.revokeToken = bind(this.revokeToken, this);
this.resetToken = bind(this.resetToken, this);
this.register = bind(this.register, this);
this.publicKey = bind(this.publicKey, this);
this.mydevices = bind(this.mydevices, this);
this.message = bind(this.message, this);
this.healthcheck = bind(this.healthcheck, this);
this.getServerPublicKey = bind(this.getServerPublicKey, this);
this.generateKeyPair = bind(this.generateKeyPair, this);
this._generateAndStoreToken = bind(this._generateAndStoreToken, this);
this.generateAndStoreTokenWithOptions = bind(this.generateAndStoreTokenWithOptions, this);
this.generateAndStoreToken = bind(this.generateAndStoreToken, this);
this.findAndUpdate = bind(this.findAndUpdate, this);
this.devices = bind(this.devices, this);
this._device = bind(this._device, this);
this.device = bind(this.device, this);
this.deleteSubscription = bind(this.deleteSubscription, this);
this.deleteSubscriptions = bind(this.deleteSubscriptions, this);
this.createSubscription = bind(this.createSubscription, this);
this.createHook = bind(this.createHook, this);
this.authenticate = bind(this.authenticate, this);
options = _.cloneDeep(options);
bearerToken = options.bearerToken, uuid = options.uuid, token = options.token, hostname = options.hostname, port = options.port, protocol = options.protocol, domain = options.domain, service = options.service, secure = options.secure, resolveSrv = options.resolveSrv, auth = options.auth, this.raw = options.raw, this.keepAlive = options.keepAlive, this.gzip = options.gzip, this.timeout = options.timeout, this.serviceName = options.serviceName;
if (this.keepAlive == null) {
this.keepAlive = true;
}
if (this.gzip == null) {
this.gzip = true;
}
if (((uuid != null) || (token != null)) && (bearerToken != null)) {
throw new Error('cannot provide uuid, token, and bearerToken');
}
if ((uuid != null) && (token == null) && (bearerToken == null)) {
throw new Error('a uuid is provided but the token is not');
}
if ((token != null) && (uuid == null) && (bearerToken == null)) {
throw new Error('a token is provided but the uuid is not');
}
if ((uuid != null) && (token != null)) {
if (auth == null) {
auth = {
username: uuid,
password: token
};
}
}
if (bearerToken != null) {
auth = {
bearer: bearerToken
};
}
ref = this.dependencies, request = ref.request, this.MeshbluRequest = ref.MeshbluRequest, this.NodeRSA = ref.NodeRSA;
if (this.MeshbluRequest == null) {
this.MeshbluRequest = require('./meshblu-request');
}
if (this.NodeRSA == null) {
this.NodeRSA = require('node-rsa');
}
this.request = this._buildRequest({
request: request,
protocol: protocol,
hostname: hostname,
port: port,
service: service,
domain: domain,
secure: secure,
resolveSrv: resolveSrv,
auth: auth
});
}
MeshbluHttp.prototype.authenticate = function(callback) {
var options;
options = this._getDefaultRequestOptions();
return this.request.post("/authenticate", options, (function(_this) {
return function(error, response, body) {
debug("authenticate", error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.createHook = function(uuid, type, url, callback) {
var error, obj, updateRequest;
error = new Error("Hook type not supported. supported types are: " + (MeshbluHttp.SUBSCRIPTION_TYPES.join(', ')));
if (indexOf.call(MeshbluHttp.SUBSCRIPTION_TYPES, type) < 0) {
return callback(error);
}
updateRequest = {
$addToSet: (
obj = {},
obj["meshblu.forwarders." + type] = {
type: 'webhook',
url: url,
method: 'POST',
generateAndForwardMeshbluCredentials: true
},
obj
)
};
return this.updateDangerously(uuid, updateRequest, callback);
};
MeshbluHttp.prototype.createSubscription = function() {
var arg, callback, emitterUuid, metadata, options, rest, subscriberUuid, type;
arg = arguments[0], rest = 2 <= arguments.length ? slice.call(arguments, 1) : [];
subscriberUuid = arg.subscriberUuid, emitterUuid = arg.emitterUuid, type = arg.type;
callback = rest[0];
if (_.isPlainObject(callback)) {
metadata = rest[0], callback = rest[1];
}
url = this._subscriptionUrl({
subscriberUuid: subscriberUuid,
emitterUuid: emitterUuid,
type: type
});
options = this._getDefaultRequestOptions();
options.headers = _.extend({}, this._getMetadataHeaders(metadata), options.headers);
return this.request.post(url, options, (function(_this) {
return function(error, response, body) {
debug('createSubscription', error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.deleteSubscriptions = function() {
var arg, callback, emitterUuid, metadata, options, rest, subscriberUuid, type;
arg = arguments[0], rest = 2 <= arguments.length ? slice.call(arguments, 1) : [];
subscriberUuid = arg.subscriberUuid, emitterUuid = arg.emitterUuid, type = arg.type;
callback = rest[0];
if (_.isPlainObject(callback)) {
metadata = rest[0], callback = rest[1];
}
url = "/v2/devices/" + subscriberUuid + "/subscriptions";
options = this._getDefaultRequestOptions();
options.headers = _.extend({}, this._getMetadataHeaders(metadata), options.headers);
options.json = {
emitterUuid: emitterUuid,
type: type
};
return this.request["delete"](url, options, (function(_this) {
return function(error, response, body) {
debug('deleteSubscriptions', error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.deleteSubscription = function() {
var arg, callback, emitterUuid, metadata, options, rest, subscriberUuid, type;
arg = arguments[0], rest = 2 <= arguments.length ? slice.call(arguments, 1) : [];
subscriberUuid = arg.subscriberUuid, emitterUuid = arg.emitterUuid, type = arg.type;
callback = rest[0];
if (_.isPlainObject(callback)) {
metadata = rest[0], callback = rest[1];
}
url = this._subscriptionUrl({
subscriberUuid: subscriberUuid,
emitterUuid: emitterUuid,
type: type
});
options = this._getDefaultRequestOptions();
options.headers = _.extend({}, this._getMetadataHeaders(metadata), options.headers);
return this.request["delete"](url, options, (function(_this) {
return function(error, response, body) {
debug('deleteSubscription', error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.device = function() {
var callback, metadata, rest, uuid;
uuid = arguments[0], rest = 2 <= arguments.length ? slice.call(arguments, 1) : [];
callback = rest[0];
if (_.isPlainObject(callback)) {
metadata = rest[0], callback = rest[1];
}
if (metadata == null) {
metadata = {};
}
return this._device(uuid, metadata, callback);
};
MeshbluHttp.prototype._device = function(uuid, metadata, callback) {
var options;
if (callback == null) {
callback = function() {};
}
options = this._getDefaultRequestOptions();
options.headers = _.extend({}, this._getMetadataHeaders(metadata), options.headers);
return this.request.get("/v2/devices/" + uuid, options, (function(_this) {
return function(error, response, body) {
debug("device", error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.devices = function() {
var callback, metadata, query, rest;
query = arguments[0], rest = 2 <= arguments.length ? slice.call(arguments, 1) : [];
if (query == null) {
query = {};
}
callback = rest[0];
if (_.isPlainObject(callback)) {
metadata = rest[0], callback = rest[1];
}
if (metadata == null) {
metadata = {};
}
return this._devices(query, metadata, callback);
};
MeshbluHttp.prototype.findAndUpdate = function() {
var callback, metadata, options, params, rest, uuid;
uuid = arguments[0], params = arguments[1], rest = 3 <= arguments.length ? slice.call(arguments, 2) : [];
callback = rest[0];
if (_.isPlainObject(callback)) {
metadata = rest[0], callback = rest[1];
}
if (metadata == null) {
metadata = {};
}
options = this._getDefaultRequestOptions();
options.json = params;
options.headers = _.extend({}, this._getMetadataHeaders(metadata), options.headers);
return this.request.put("/v2/devices/" + uuid + "/find-and-update", options, (function(_this) {
return function(error, response, body) {
debug("update", error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.generateAndStoreToken = function() {
var callback, metadata, params, rest, uuid;
uuid = arguments[0], rest = 2 <= arguments.length ? slice.call(arguments, 1) : [];
callback = rest[0];
if (_.isPlainObject(callback)) {
metadata = rest[0], callback = rest[1];
}
if (metadata == null) {
metadata = {};
}
params = true;
return this._generateAndStoreToken(uuid, params, metadata, callback);
};
MeshbluHttp.prototype.generateAndStoreTokenWithOptions = function() {
var callback, metadata, params, rest, uuid;
uuid = arguments[0], params = arguments[1], rest = 3 <= arguments.length ? slice.call(arguments, 2) : [];
callback = rest[0];
if (_.isPlainObject(callback)) {
metadata = rest[0], callback = rest[1];
}
if (metadata == null) {
metadata = {};
}
return this._generateAndStoreToken(uuid, params, metadata, callback);
};
MeshbluHttp.prototype._generateAndStoreToken = function(uuid, params, metadata, callback) {
var options;
if (callback == null) {
callback = function() {};
}
options = this._getDefaultRequestOptions();
options.json = params;
options.headers = _.extend({}, this._getMetadataHeaders(metadata), options.headers);
return this.request.post("/devices/" + uuid + "/tokens", options, (function(_this) {
return function(error, response, body) {
debug("generateAndStoreToken", error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.generateKeyPair = function() {
var key;
key = new this.NodeRSA();
key.generateKeyPair();
return {
privateKey: key.exportKey('private'),
publicKey: key.exportKey('public')
};
};
MeshbluHttp.prototype.getServerPublicKey = function(callback) {
var options;
options = this._getDefaultRequestOptions();
return this.request.get('/publickey', options, (function(_this) {
return function(error, response, body) {
body = _.get(body, 'publicKey');
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.healthcheck = function(callback) {
var options;
if (callback == null) {
callback = function() {};
}
options = this._getDefaultRequestOptions();
return this.request.get('/healthcheck', options, (function(_this) {
return function(error, response) {
var healthy;
if (error != null) {
return callback(error);
}
healthy = response.statusCode === 200;
return callback(null, healthy, response.statusCode);
};
})(this));
};
MeshbluHttp.prototype.message = function() {
var callback, message, metadata, rest;
message = arguments[0], rest = 2 <= arguments.length ? slice.call(arguments, 1) : [];
callback = rest[0];
if (_.isPlainObject(callback)) {
metadata = rest[0], callback = rest[1];
}
if (metadata == null) {
metadata = {};
}
return this._message(message, metadata, callback);
};
MeshbluHttp.prototype.mydevices = function(query, callback) {
var options;
if (query == null) {
query = {};
}
if (callback == null) {
callback = function() {};
}
options = this._getDefaultRequestOptions();
options.qs = query;
return this.request.get("/mydevices", options, (function(_this) {
return function(error, response, body) {
debug("mydevices", error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.publicKey = function(deviceUuid, callback) {
var options;
if (callback == null) {
callback = function() {};
}
options = this._getDefaultRequestOptions();
return this.request.get("/devices/" + deviceUuid + "/publickey", options, (function(_this) {
return function(error, response, body) {
debug("publicKey", error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.register = function(device, callback) {
var options;
if (callback == null) {
callback = function() {};
}
options = this._getDefaultRequestOptions();
options.json = device;
return this.request.post("/devices", options, (function(_this) {
return function(error, response, body) {
if (body == null) {
body = {};
}
debug("register", error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.resetToken = function(deviceUuid, callback) {
var options;
if (callback == null) {
callback = function() {};
}
options = this._getDefaultRequestOptions();
url = "/devices/" + deviceUuid + "/token";
return this.request.post(url, options, (function(_this) {
return function(error, response, body) {
debug('resetToken', error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.revokeToken = function(deviceUuid, deviceToken, callback) {
var options;
if (callback == null) {
callback = function() {};
}
options = this._getDefaultRequestOptions();
return this.request["delete"]("/devices/" + deviceUuid + "/tokens/" + deviceToken, options, (function(_this) {
return function(error, response, body) {
if (body == null) {
body = {};
}
debug("revokeToken", error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.revokeTokenByQuery = function(deviceUuid, query, callback) {
var options;
if (callback == null) {
callback = function() {};
}
options = this._getDefaultRequestOptions();
options.qs = query;
return this.request["delete"]("/devices/" + deviceUuid + "/tokens", options, (function(_this) {
return function(error, response, body) {
if (body == null) {
body = {};
}
debug("revokeToken", error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.search = function() {
var callback, metadata, options, query, rest;
query = arguments[0], rest = 2 <= arguments.length ? slice.call(arguments, 1) : [];
callback = rest[0];
if (_.isPlainObject(callback)) {
metadata = rest[0], callback = rest[1];
}
if (metadata == null) {
metadata = {};
}
options = this._getDefaultRequestOptions();
options.headers = _.extend({}, this._getMetadataHeaders(metadata), options.headers);
options.json = query;
return this.request.post("/search/devices", options, (function(_this) {
return function(error, response, body) {
debug('search', error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.searchTokens = function(query, metadata, callback) {
var options;
options = this._getDefaultRequestOptions();
options.headers = _.extend({}, this._getMetadataHeaders(metadata), options.headers);
options.json = query;
return this.request.post("/search/tokens", options, (function(_this) {
return function(error, response, body) {
debug('searchTokens', error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.setPrivateKey = function(privateKey) {
return this.privateKey = new this.NodeRSA(privateKey);
};
MeshbluHttp.prototype.sign = function(data) {
return this.privateKey.sign(stableStringify(data)).toString('base64');
};
MeshbluHttp.prototype.subscriptions = function() {
var callback, metadata, rest, uuid;
uuid = arguments[0], rest = 2 <= arguments.length ? slice.call(arguments, 1) : [];
callback = rest[0];
if (_.isPlainObject(callback)) {
metadata = rest[0], callback = rest[1];
}
if (metadata == null) {
metadata = {};
}
return this._subscriptions(uuid, metadata, callback);
};
MeshbluHttp.prototype.unregister = function(device, callback) {
var options;
if (callback == null) {
callback = function() {};
}
options = this._getDefaultRequestOptions();
return this.request["delete"]("/devices/" + device.uuid, options, (function(_this) {
return function(error, response, body) {
debug("unregister", error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.update = function() {
var callback, metadata, params, rest, uuid;
uuid = arguments[0], params = arguments[1], rest = 3 <= arguments.length ? slice.call(arguments, 2) : [];
callback = rest[0];
if (_.isPlainObject(callback)) {
metadata = rest[0], callback = rest[1];
}
if (metadata == null) {
metadata = {};
}
return this._update(uuid, params, metadata, callback);
};
MeshbluHttp.prototype.updateDangerously = function() {
var callback, metadata, params, rest, uuid;
uuid = arguments[0], params = arguments[1], rest = 3 <= arguments.length ? slice.call(arguments, 2) : [];
callback = rest[0];
if (_.isPlainObject(callback)) {
metadata = rest[0], callback = rest[1];
}
if (metadata == null) {
metadata = {};
}
return this._updateDangerously(uuid, params, metadata, callback);
};
MeshbluHttp.prototype._updateDangerously = function(uuid, params, metadata, callback) {
var options;
if (callback == null) {
callback = function() {};
}
options = this._getDefaultRequestOptions();
options.json = params;
options.headers = _.extend({}, this._getMetadataHeaders(metadata), options.headers);
return this.request.put("/v2/devices/" + uuid, options, (function(_this) {
return function(error, response, body) {
debug("updated", uuid, JSON.stringify(params));
debug("update", error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype.verify = function(message, signature) {
return this.privateKey.verify(stableStringify(message), signature, 'utf8', 'base64');
};
MeshbluHttp.prototype.whoami = function(callback) {
var options;
if (callback == null) {
callback = function() {};
}
options = this._getDefaultRequestOptions();
return this.request.get("/v2/whoami", options, (function(_this) {
return function(error, response, body) {
debug("whoami", error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype._assertNoSrv = function(arg) {
var domain, secure, service;
service = arg.service, domain = arg.domain, secure = arg.secure;
if (domain != null) {
throw new Error('domain property only applies when resolveSrv is true');
}
if (service != null) {
throw new Error('service property only applies when resolveSrv is true');
}
if (secure != null) {
throw new Error('secure property only applies when resolveSrv is true');
}
};
MeshbluHttp.prototype._assertNoUrl = function(arg) {
var hostname, port, protocol;
protocol = arg.protocol, hostname = arg.hostname, port = arg.port;
if (protocol != null) {
throw new Error('protocol property only applies when resolveSrv is false');
}
if (hostname != null) {
throw new Error('hostname property only applies when resolveSrv is false');
}
if (port != null) {
throw new Error('port property only applies when resolveSrv is false');
}
};
MeshbluHttp.prototype._buildRequest = function(arg) {
var auth, domain, hostname, port, protocol, request, resolveSrv, secure, service;
request = arg.request, protocol = arg.protocol, hostname = arg.hostname, port = arg.port, service = arg.service, domain = arg.domain, secure = arg.secure, resolveSrv = arg.resolveSrv, auth = arg.auth;
if (request != null) {
return request;
}
if (resolveSrv) {
return this._buildSrvRequest({
protocol: protocol,
hostname: hostname,
port: port,
service: service,
domain: domain,
secure: secure,
auth: auth
});
}
return this._buildUrlRequest({
protocol: protocol,
hostname: hostname,
port: port,
service: service,
domain: domain,
secure: secure,
auth: auth
});
};
MeshbluHttp.prototype._buildSrvRequest = function(arg) {
var auth, domain, hostname, port, protocol, request, secure, service;
protocol = arg.protocol, hostname = arg.hostname, port = arg.port, service = arg.service, domain = arg.domain, secure = arg.secure, auth = arg.auth;
this._assertNoUrl({
protocol: protocol,
hostname: hostname,
port: port
});
if (service == null) {
service = 'meshblu';
}
if (domain == null) {
domain = 'octoblu.com';
}
if (secure == null) {
secure = true;
}
request = {};
if (auth != null) {
request.auth = auth;
}
if (this.timeout != null) {
request.timeout = this.timeout;
}
return new this.MeshbluRequest({
resolveSrv: true,
service: service,
domain: domain,
secure: secure,
request: request
});
};
MeshbluHttp.prototype._buildUrlRequest = function(arg) {
var auth, domain, hostname, port, protocol, request, secure, service;
protocol = arg.protocol, hostname = arg.hostname, port = arg.port, service = arg.service, domain = arg.domain, secure = arg.secure, auth = arg.auth;
this._assertNoSrv({
service: service,
domain: domain,
secure: secure
});
if (protocol == null) {
protocol = 'https';
}
if (hostname == null) {
hostname = 'meshblu.octoblu.com';
}
if (port == null) {
port = 443;
}
try {
port = parseInt(port);
} catch (error1) {}
request = {};
if (auth != null) {
request.auth = auth;
}
if (this.timeout != null) {
request.timeout = this.timeout;
}
return new this.MeshbluRequest({
resolveSrv: false,
protocol: protocol,
hostname: hostname,
port: port,
request: request
});
};
MeshbluHttp.prototype._devices = function(query, metadata, callback) {
var options;
if (callback == null) {
callback = function() {};
}
options = this._getDefaultRequestOptions();
options.qs = query;
options.headers = _.extend({}, this._getMetadataHeaders(metadata), options.headers);
return this.request.get("/v2/devices", options, (function(_this) {
return function(error, response, body) {
debug("devices", error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype._getDefaultHeaders = function() {
if (this.serviceName == null) {
return;
}
return {
'x-meshblu-service-name': this.serviceName
};
};
MeshbluHttp.prototype._getDefaultRequestOptions = function() {
var headers, options;
headers = this._getDefaultHeaders();
options = {
json: true,
forever: this.keepAlive,
gzip: this.gzip
};
if (headers != null) {
options.headers = headers;
}
return options;
};
MeshbluHttp.prototype._getMetadataHeaders = function(metadata) {
return _.transform(metadata, (function(_this) {
return function(newMetadata, value, key) {
var kebabKey;
kebabKey = _.kebabCase(key);
newMetadata["x-meshblu-" + kebabKey] = _this._possiblySerializeHeaderValue(value);
return true;
};
})(this), {});
};
MeshbluHttp.prototype._getRawRequestOptions = function() {
var headers, ref;
headers = (ref = this._getDefaultHeaders()) != null ? ref : {};
headers['content-type'] = 'application/json';
return {
json: false,
headers: headers
};
};
MeshbluHttp.prototype._handleError = function(arg, callback) {
var code, error, message, response;
message = arg.message, code = arg.code, response = arg.response;
if (message == null) {
message = 'Unknown Error Occurred';
}
response = response != null ? typeof response.toJSON === "function" ? response.toJSON() : void 0 : void 0;
debug('handling error', JSON.stringify({
message: message,
code: code,
response: response
}, null, 2));
error = this._userError(code, message, response);
return callback(error);
};
MeshbluHttp.prototype._handleResponse = function(arg, callback) {
var body, code, error, message, ref, response;
error = arg.error, response = arg.response, body = arg.body;
if (error != null) {
return this._handleError({
message: error.message,
code: error != null ? error.code : void 0,
response: response
}, callback);
}
code = response != null ? response.statusCode : void 0;
if ((response != null ? (ref = response.headers) != null ? ref['x-meshblu-error'] : void 0 : void 0) != null) {
error = JSON.parse(response.headers['x-meshblu-error']);
return this._handleError({
message: error.message,
response: response,
code: code
}, callback);
}
if ((body != null ? body.error : void 0) != null) {
return this._handleError({
message: body.error,
response: response,
code: code
}, callback);
}
if (code >= 400) {
if (_.isString(body)) {
message = body;
}
if (message == null) {
message = "Invalid Response Code " + code;
}
return this._handleError({
message: message,
response: response,
code: code
}, callback);
}
return callback(null, body);
};
MeshbluHttp.prototype._message = function(message, metadata, callback) {
var options;
if (callback == null) {
callback = function() {};
}
if (this.raw) {
options = this._getRawRequestOptions();
options.body = message;
} else {
options = this._getDefaultRequestOptions();
options.json = message;
}
options.headers = _.extend({}, this._getMetadataHeaders(metadata), options.headers);
return this.request.post("/messages", options, (function(_this) {
return function(error, response, body) {
debug("message", error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype._possiblySerializeHeaderValue = function(value) {
if (_.isString(value)) {
return value;
}
if (_.isBoolean(value)) {
return value;
}
if (_.isNumber(value)) {
return value;
}
return JSON.stringify(value);
};
MeshbluHttp.prototype._subscriptions = function(uuid, metadata, callback) {
var options;
if (callback == null) {
callback = function() {};
}
options = this._getDefaultRequestOptions();
options.headers = _.extend({}, this._getMetadataHeaders(metadata), options.headers);
return this.request.get("/v2/devices/" + uuid + "/subscriptions", options, (function(_this) {
return function(error, response, body) {
debug("subscriptions", error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype._subscriptionUrl = function(options) {
var emitterUuid, subscriberUuid, type;
subscriberUuid = options.subscriberUuid, emitterUuid = options.emitterUuid, type = options.type;
return "/v2/devices/" + subscriberUuid + "/subscriptions/" + emitterUuid + "/" + type;
};
MeshbluHttp.prototype._update = function(uuid, params, metadata, callback) {
var options;
if (callback == null) {
callback = function() {};
}
options = this._getDefaultRequestOptions();
options.json = params;
options.headers = _.extend({}, this._getMetadataHeaders(metadata), options.headers);
return this.request.patch("/v2/devices/" + uuid, options, (function(_this) {
return function(error, response, body) {
debug("update", error, JSON.stringify(body));
return _this._handleResponse({
error: error,
response: response,
body: body
}, callback);
};
})(this));
};
MeshbluHttp.prototype._userError = function(code, message, response) {
var error;
error = new Error(message);
error.code = code;
if (response != null) {
error.response = response;
}
return error;
};
return MeshbluHttp;
})();
module.exports = MeshbluHttp;
}).call(this);
//# sourceMappingURL=meshblu-http.js.map