-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from prey/api-refactor
Client API refactor
- Loading branch information
Showing
39 changed files
with
1,114 additions
and
518 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,54 @@ | ||
var needle = require('needle'), | ||
common = require('./../../common'), | ||
providers = require('./../../providers'), | ||
keys = require('./../../keys'), | ||
var common = require('./../../common'), | ||
api = common.api, | ||
config = common.config, | ||
providers = require('./../../providers'), | ||
logger = common.logger.prefix('control-panel'); | ||
|
||
var request_format = '.json'; | ||
|
||
var has_files = function(data) { | ||
for (var key in data) { | ||
if (data[key] && data[key].file && data[key].content_type) | ||
return true; | ||
} | ||
}; | ||
|
||
var get_url = function(what) { | ||
var host = config.get('protocol') + '://' + config.get('host'), | ||
path = '/api/v2/devices/' + config.get('device_key'); | ||
|
||
return host + path + '/' + what + request_format; | ||
}; | ||
|
||
var get_status_info = function(cb) { | ||
var headers = {}; | ||
|
||
providers.get('status', function(err, data){ | ||
if (err) return cb(err); | ||
|
||
headers['X-Prey-Status'] = JSON.stringify(data); | ||
cb(null, headers); | ||
}) | ||
providers.get('status', cb) | ||
} | ||
|
||
var make_request = function(what, data, opts, callback) { | ||
|
||
var url = get_url(what), | ||
opts = opts || {}; | ||
var opts = opts || {}, | ||
what = what.replace(/s$/, ''); | ||
|
||
var request_opts = { | ||
username : config.get('api_key'), | ||
password : 'x', | ||
multipart : has_files(data), | ||
timeout : 20000, | ||
proxy : opts.proxy | ||
}; | ||
|
||
if (opts.headers) | ||
request_opts.headers = opts.headers; | ||
|
||
var host = url.replace(/.*\/\/([^\/]+).*/, '$1'), | ||
msg = 'Posting data to ' + host; | ||
opts.multipart = has_files(data); | ||
|
||
var msg = 'Posting ' + what; | ||
if (opts.proxy) msg += ' using proxy: ' + opts.proxy; | ||
logger.info(msg); | ||
|
||
needle.post(url, data, request_opts, function(err, resp, body){ | ||
api.push[what](data, opts, function(err, resp, body){ | ||
|
||
// if there was an error, lets try connecting via a proxy if possible | ||
if (err && !opts.proxy && config.get('try_proxy') && config.get('try_proxy').match('.')) { | ||
return make_request(what, data, { proxy: config.get('try_proxy') }, callback); | ||
opts.proxy = config.get('try_proxy'); | ||
return make_request(what, data, opts, callback); | ||
} | ||
|
||
if (resp) resp.body = body; | ||
callback(err, resp); | ||
}); | ||
|
||
}; | ||
|
||
exports.init = function(cb) { | ||
keys.verify(cb); | ||
cb(); | ||
} | ||
|
||
exports.send = function(what, data, opts, callback) { | ||
if (!config.get('send_status_info') || what == 'response') | ||
return make_request(what, data, opts, callback); | ||
|
||
get_status_info(function(err, headers){ | ||
if (!err && headers) | ||
opts.headers = headers; | ||
|
||
get_status_info(function(err, status) { | ||
opts.status = status; | ||
make_request(what, data, opts, callback); | ||
}) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.