2014-11-18 10:11:07 -04:00
|
|
|
_ = require('lodash')
|
|
|
|
Canvas = require('resin-platform-api')
|
|
|
|
Promise = require('bluebird')
|
2014-12-03 12:03:54 -04:00
|
|
|
settings = require('../settings')
|
2014-11-26 12:15:40 -04:00
|
|
|
server = require('../server/server')
|
|
|
|
promisifiedServerRequest = Promise.promisify(server.request, server)
|
2014-11-18 10:11:07 -04:00
|
|
|
|
|
|
|
class CanvasRequestService extends Canvas(_, Promise)
|
|
|
|
_request: (params) ->
|
2014-11-24 10:40:12 -04:00
|
|
|
params.json = params.data
|
2014-11-18 10:11:07 -04:00
|
|
|
params.gzip ?= true
|
|
|
|
promisifiedServerRequest(params).spread (response, body) ->
|
|
|
|
if 200 <= response.statusCode < 300
|
|
|
|
return body
|
|
|
|
throw new Error(body)
|
|
|
|
|
|
|
|
module.exports = new CanvasRequestService
|
2014-12-05 10:53:59 -04:00
|
|
|
url: settings.get('remoteUrl')
|
|
|
|
apiPrefix: settings.get('apiPrefix')
|
2014-11-18 10:11:07 -04:00
|
|
|
withCredentials: true
|