Skip to content

Commit

Permalink
Add command to create webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi committed Jul 17, 2017
1 parent 9483e06 commit c8bfd53
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/bin/barracks
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ if (config.v2Enabled) {
if (config.experimental) {
program.command('integration [cmd]', 'Manage third-party integrations');
program.command('message [cmd]', 'Send messages to devices');
program.command('hook [cmd]', 'Manage hooks');
}
} else {
program.command('login', 'Authenticate to Barracks')
Expand Down
15 changes: 15 additions & 0 deletions src/bin/barracks-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env node

const program = require('commander');
const config = require('../config.js');
const pjson = require('../../package.json');

const barracks = program
.version(pjson.version)
.command('create [cmd]', 'Manage hooks creation');

barracks.parse(process.argv);

process.on('SIGINT', () => {
process.exit();
});
4 changes: 4 additions & 0 deletions src/bin/barracks-hook-create
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node

const CreateHook = require('../commands/hook/CreateHook');
new CreateHook().render();
7 changes: 7 additions & 0 deletions src/clients/BarracksClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const PackageClient = require('./PackageClient');
const SegmentClient = require('./SegmentClient');
const TokenClient = require('./TokenClient');
const UpdateClient = require('./UpdateClient');
const HookClient = require('./HookClient');
const BarracksSDKProxy = require('../utils/BarracksSDKProxy');
const BarracksSDK2Proxy = require('../utils/BarracksSDK2Proxy');
const BarracksMessagingSDKProxy = require('../utils/BarracksMessagingSDKProxy');
Expand Down Expand Up @@ -87,6 +88,11 @@ function mergeUpdateClient(barracksClient) {
barracksClient.scheduleUpdate = updateClient.scheduleUpdate.bind(updateClient);
}

function mergeHookClient(barracksClient) {
const hookClient = new HookClient();
barracksClient.createHook = hookClient.createHook.bind(hookClient);
}

function mergeSDKProxy(barracksClient) {
const proxyV1 = new BarracksSDKProxy();
barracksClient.checkUpdate = proxyV1.checkUpdate.bind(proxyV1);
Expand All @@ -111,6 +117,7 @@ class BarracksClient {
mergeSegmentClient(this);
mergeTokenClient(this);
mergeUpdateClient(this);
mergeHookClient(this);
mergeSDKProxy(this);
mergeMessagingSDKProxy(this);
}
Expand Down
35 changes: 35 additions & 0 deletions src/clients/HookClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const HTTPClient = require('./HTTPClient');
const logger = require('../utils/logger');

const endpoints = {
createHook: {
method: 'POST',
path: '/api/dispatcher/hooks'
}
};

class HookClient {

constructor() {
this.httpClient = new HTTPClient();
}

createSegment(token, hook) {
return new Promise((resolve, reject) => {
this.httpClient.sendEndpointRequest(
endpoints.createSegment,
{
headers: {
'x-auth-token': token
},
body: hook
}
).then(response => {
resolve(response.body);
}).catch(err => {
reject(err.message);
});
});
}

module.exports = HookClient;
32 changes: 32 additions & 0 deletions src/commands/hook/CreateHookCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const BarracksCommand = require('../BarracksCommand');

class CreateHookCommand extends BarracksCommand {

configureCommand(program) {
return program
.option('--web', 'To create a web hook')
.option('--name [value]', 'The unique name of the webhook')
.option('--url [value]', 'The URL for this webhook');
}

validateCommand(program) {
return !!(
program.web &&
program.url && program.url !== true &&
program.name && program.name !== true &&
(!program.description || (program.description && program.description !== true))
);
}

execute(program) {
return this.getAuthenticationToken().then(token => {
return this.barracks.createHook(token, {
type: program.web,
name: program.name,
url: program.url
});
});
}
}

module.exports = CreateHookCommand;

0 comments on commit c8bfd53

Please sign in to comment.