browser-meshblu-http
Version:
Meshblu Client for the Browser
113 lines (99 loc) • 3.91 kB
text/coffeescript
_ = require 'lodash'
request = require 'superagent'
ParseUrl = require 'url-parse'
qs = require 'qs'
class MeshbluHttp
constructor: (meshbluConfig) ->
throw new Error("MeshbluHttp only allows hostname: 'server' is not allowed") if meshbluConfig?.server
throw new Error("MeshbluHttp only allows hostname: 'host' is not allowed") if meshbluConfig?.host
options = _.extend port: 443, hostname: 'meshblu.octoblu.com', meshbluConfig
{, , , , } = options
= null if == 'websocket'
try = parseInt
?= 'https:' if == 443
?= 'http:'
generateAndStoreToken: (uuid, options={}, callback) =>
console.log "/devices/#{uuid}/tokens"
request
.post "/devices/#{uuid}/tokens"
.auth ,
.send options
.end (error, response) =>
return callback new Error 'Invalid Response Code' unless response.ok
return callback new Error 'Invalid Response' if _.isEmpty response.body
callback null, response.body.token
device: (uuid, callback) =>
request
.get "/v2/devices/#{uuid}"
.auth ,
.end (error, response) =>
return callback null if response.notFound
return callback new Error 'Invalid Response Code' unless response.ok
return callback new Error 'Invalid Response' if _.isEmpty response.body
callback null, response.body ? {}
devices: (query, callback) =>
request
.get "/v2/devices"
.auth ,
.query qs.stringify query
.end (error, response) =>
return callback null if response.notFound
return callback new Error 'Invalid Response Code' unless response.ok
callback null, response.body ? []
register: (body, callback) =>
request
.post "/devices"
.auth ,
.send body
.end (error, response) =>
return callback null if response.notFound
return callback new Error 'Invalid Response Code' unless response.ok
return callback new Error 'Invalid Response' if _.isEmpty response.body
callback null, response.body
removeTokenByQuery: (uuid, options={}, callback) =>
request
.del "/devices/#{uuid}/tokens"
.query options
.auth ,
.end (error, response) =>
return callback new Error 'Invalid Response Code' unless response.ok
callback null
unregister: (uuid, callback) =>
request
.del "/devices/#{uuid}"
.auth ,
.end (error, response) =>
return callback new Error 'Invalid Response Code' unless response.ok
callback null, response.body
update: (uuid, body, callback) =>
request
.patch "/v2/devices/#{uuid}"
.auth ,
.send body
.end (error, response) =>
return callback new Error 'Invalid Response Code' unless response.ok
callback null, response.body
updateDangerously: (uuid, body, callback) =>
request
.put "/v2/devices/#{uuid}"
.auth ,
.send body
.end (error, response) =>
return callback new Error 'Invalid Response Code' unless response.ok
callback null, response.body
whoami: (callback) =>
request
.get '/v2/whoami'
.auth ,
.end (error, response) =>
return callback new Error 'Invalid Response Code' unless response.ok
return callback new Error 'Invalid Device' if _.isEmpty response.body
callback null, response.body
_url: (pathname) =>
theUrl = new ParseUrl('')
theUrl.set 'hostname',
theUrl.set 'protocol',
theUrl.set 'port',
theUrl.set 'pathname', pathname
return theUrl.toString()
module.exports = MeshbluHttp