briareus
Version:
Briareus assists with Feature Branch deploys to ECS
76 lines (60 loc) • 1.85 kB
JavaScript
const _ = require('lodash');
const request = require('request');
const variant = require('./variant');
const secrets = require('./secrets');
const deploy = require('./deploy');
const destroy = require('./destroy');
const errors = require('./errors');
class Client {
constructor(options) {
if (!options.endpoint) {
throw new Error('"endpoint" option required for Briareus API client');
}
if (!options.authtoken) {
throw new Error('"authtoken" option required for Briareus API client');
}
if (!options.logger) {
throw new Error('"logger" option required for Briareus API client');
}
this.endpoint = options.endpoint;
this.authtoken = options.authtoken;
this.log = options.logger;
this.variant = this.wrapApiFns(variant);
this.secrets = this.wrapApiFns(secrets);
this.deploy = deploy(this);
this.destroy = destroy(this);
}
wrapApiFns(fns) {
return _.zipObject(_.keys(fns), _.map(_.values(fns), (n) => {
if (typeof n === 'function') return n(this);
return this.wrapApiFns(n);
}));
}
request(params, expectedStatusCode, cb) {
_.defaults(params, {
baseUrl: this.endpoint,
auth: {
user: 'briareus',
pass: this.authtoken
},
json: true
});
this.log.debug(params, 'Client API Request');
request(params, (err, res, body) => {
if (err) return cb(err);
this.log.debug({
statusCode: res.statusCode,
body
}, 'Client API Response');
if (res.statusCode !== expectedStatusCode) {
let error = new errors.Api(`API responded ${res.statusCode}, expecting ${expectedStatusCode}.`);
error.code = res.statusCode;
error.body = body;
return cb(error);
}
cb(null, body);
})
}
}
module.exports = Client;