Skip to content

Instantly share code, notes, and snippets.

@jmervine
Created October 22, 2014 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmervine/8e111f249523975bb2a0 to your computer and use it in GitHub Desktop.
Save jmervine/8e111f249523975bb2a0 to your computer and use it in GitHub Desktop.
Work in progress, not currently working.
var configurator = require('configurator');
var Base64 = require('urlsafe-base64');
var request = require('request');
var host = 'https://io.catchpoint.com';
var prefix = '/ui/api';
var version = 1;
var config;
function _init(opts) {
if (!opts || !opts.endpoint)
throw new Error('Missing required endpoint.');
if (opts.file) {
config = configurator(opts.file);
} else {
config = configurator();
}
}
function fetchToken(callback) {
var tokenData = {
grant_type: 'client_credentials',
client_id: config.key,
client_secret: config.secret
};
var tokenOpts = {
url: host + prefix + '/token',
headers: { 'Accept': 'application/json' },
form: tokenData
};
request.post(tokenOpts, function(e, _, b) {
callback(e, Base64.encode(new Buffer(JSON.parse(b).access_token)));
});
}
module.exports = function(opts, callback) {
_init(opts);
fetchToken(function(err, token) {
var req = {
url: host + prefix + '/v' + version.toString() + opts.endpoint,
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ' + token
}
};
request.get(req, function(e, r, b) {
if (e) {
return callback(e);
}
if (r.statusCode < 200 || r.statusCode > 299) {
return callback(new Error('Status Code ' + r.statusCode));
}
try {
callback(e, JSON.parse(b));
} catch(e) {
callback(e);
}
});
});
};
var yaml = require('js-yaml');
var fs = require('fs');
var defaultConfig = process.env.HOME + '/.catchpoint';
/****
* option: default :: description
* -------------------------------------
* file: undefined :: config file to be used, default is
* '$HOME/.catchpoint'
* [opt]: n/a :: set or overide [opt]
*
* If callback is undefined, 'fs.readFileSync' method is used and result
* is returned. Additionally errors are thrown.
*
* Otherwise, result is passed via callback along with any errors.
*
* YAML parsing is always snyc.
*
* callback(err, result)
*/
module.exports = function configurator(opts, callback) {
// allow for configurator(function(...) { ... })`
if (typeof opts === 'function' || typeof opts === 'undefined') {
callback = opts;
opts = {};
}
if (typeof opts === 'string') {
opts = { file: opts };
}
var applyOverides = function applyOverides(res) {
Object.keys(opts).forEach(function(key) {
if (key !== 'file') {
res[key] = opts[key];
}
});
return res;
};
var file = opts.file || defaultConfig;
if (typeof callback === 'undefined') {
return applyOverides(yaml.safeLoad(fs.readFileSync(file, 'utf8')));
}
fs.readFile(file, function (err, raw) {
if (err) return callback(err);
try {
return callback(null, applyOverides(yaml.safeLoad(raw)));
} catch (e) {
return callback(e);
}
});
};
#!/usr/bin/env node
var catchpoint = require('catchpoint');
var opts = {
endpoint: '/items'
};
catchpoint(opts, function(e, b) {
if (e) console.trace(e);
console.dir(b);
});
{
"dependencies": {
"js-yaml": "^3.2.2",
"request": "^2.45.0",
"urlsafe-base64": "0.0.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment