diff --git a/src/bin/barracks b/src/bin/barracks index 3375155..d2b74b9 100755 --- a/src/bin/barracks +++ b/src/bin/barracks @@ -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') diff --git a/src/bin/barracks-hook b/src/bin/barracks-hook new file mode 100644 index 0000000..84dd66b --- /dev/null +++ b/src/bin/barracks-hook @@ -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(); +}); diff --git a/src/bin/barracks-hook-create b/src/bin/barracks-hook-create new file mode 100644 index 0000000..9a3a605 --- /dev/null +++ b/src/bin/barracks-hook-create @@ -0,0 +1,4 @@ +#!/usr/bin/env node + +const CreateHook = require('../commands/hook/CreateHook'); +new CreateHook().render(); \ No newline at end of file diff --git a/src/clients/BarracksClient.js b/src/clients/BarracksClient.js index 83e4412..5500cd2 100644 --- a/src/clients/BarracksClient.js +++ b/src/clients/BarracksClient.js @@ -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'); @@ -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); @@ -111,6 +117,7 @@ class BarracksClient { mergeSegmentClient(this); mergeTokenClient(this); mergeUpdateClient(this); + mergeHookClient(this); mergeSDKProxy(this); mergeMessagingSDKProxy(this); } diff --git a/src/clients/HookClient.js b/src/clients/HookClient.js new file mode 100644 index 0000000..661bb4d --- /dev/null +++ b/src/clients/HookClient.js @@ -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; \ No newline at end of file diff --git a/src/commands/hook/CreateHookCommand.js b/src/commands/hook/CreateHookCommand.js new file mode 100644 index 0000000..74f2b0d --- /dev/null +++ b/src/commands/hook/CreateHookCommand.js @@ -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;