balena-cli/lib/server/server.coffee

39 lines
1004 B
CoffeeScript
Raw Normal View History

2014-10-31 15:59:23 +00:00
request = require('request')
urljoin = require('url-join')
config = require('../config')
exports.request = (method = 'GET', uri, json, callback) ->
method = method.toUpperCase()
uri = urljoin(config.baseUrl, uri)
request {
uri
method
json
}, (error, response, body) ->
try
response.body = JSON.parse(response.body)
if response?.statusCode >= 400
error = new Error(response.body)
2014-10-31 15:59:23 +00:00
return callback?.call(null, error, response, body)
exports.get = (uri, callback) ->
return exports.request('GET', uri, null, callback)
exports.head = (uri, callback) ->
return exports.request('HEAD', uri, null, callback)
exports.delete = (uri, callback) ->
return exports.request('DELETE', uri, null, callback)
exports.post = (uri, json, callback) ->
return exports.request('POST', uri, json, callback)
exports.put = (uri, json, callback) ->
return exports.request('PUT', uri, json, callback)
exports.patch = (uri, json, callback) ->
return exports.request('PATCH', uri, json, callback)