Skip to content

Commit

Permalink
Refactored webhooks library (#170)
Browse files Browse the repository at this point in the history
* Updated webhooks lib and tests

* Updated webhooks doc and examples

* updated tests for payloads and cloneDeep
  • Loading branch information
aydrian authored Oct 24, 2016
1 parent 1840444 commit b55f9c4
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 235 deletions.
52 changes: 33 additions & 19 deletions docs/resources/webhooks.md
Original file line number Diff line number Diff line change
@@ -1,63 +1,77 @@
# Webhooks

This library provides easy access to the [Webhooks](https://www.sparkpost.com/api#/reference/webhooks/) Resource.
This library provides easy access to the [Webhooks](https://developers.sparkpost.com/api/webhooks) Resource.

## Methods
* **all(options, callback)**
* **all([options, callback]) &rarr; `{Promise}`**<br />
List currently extant webhooks.
* `options.timezone` - `String` Standard timezone identification Default: `UTC`
* `callback` - executed after task is completed. **required**
* `callback` - executed after task is completed if provided*
* standard `callback(err, data)`
* `err` - any error that occurred
* `data` - full response from request client
* **describe(options, callback)**
* **describe(options[, callback]) &rarr; `{Promise}`**<br />
Retrieve details about a specified webhook by its id
* `options.id` - the id of the webhook you want to describe **required**
* `options.timezone` - `String` Standard timezone identification Default: `UTC`
* `callback` - see all function
* **create(webhook, callback)**
* **create(webhook[, callback]) &rarr; `{Promise}`**<br />
Create a new webhook
* `webhook` - a webhook object **required**
* `webhook` - an object of [webhook attributes](https://developers.sparkpost.com/api/webhooks#header-webhooks-object-properties) **required**
* `callback` - see all function
* **update(webhook, callback)**
* **update(webhook[, callback]) &rarr; `{Promise}`**<br />
Update an existing webhook
* `webhook` - a webhook object **required**
* `webhook` - an object of [webhook attributes](https://developers.sparkpost.com/api/webhooks#header-webhooks-object-properties) **required**
* `callback` - see all function
* **delete(id, callback)**
* **delete(id[, callback]) &rarr; `{Promise}`**<br />
Delete an existing webhook
* `id` - the id of the webhook you want to delete **required**
* `callback` - see all function
* **validate(options, callback)**
* **validate(options[, callback]) &rarr; `{Promise}`**<br />
Sends an example message event batch from the Webhook API to the target URL
* `options.id` - the id of the webhook you want to validate **required**
* `options.message` - sample object to send to the target URL **required**
* `callback` - see all function
* **getBatchStatus(options, callback)**
* **getBatchStatus(options[, callback]) &rarr; `{Promise}`**<br />
Sends an example message event batch from the Webhook API to the target URL
* `options.id` - the id of the webhook you want to get status on **required**
* `options.limit` - `number` maximum number of results to return Default: `1000`
* `callback` - see all function
* **getDocumentation(callback)**
* **getDocumentation([callback]) &rarr; `{Promise}`**<br />
Lists descriptions of the events, event types, and event fields that could be included in a Webhooks post to your target URL.
* `callback` - see all function
* **getSamples(options, callback)**
* **getSamples(options[, callback]) &rarr; `{Promise}`**<br />
List an example of the event data that will be posted by a Webhook for the specified events.
* `options.events` - `String` event types for which to get a sample payload Defaults to all event types
* `callback` - see all function

*callback is optional because all methods return a Promise.

## Examples

```js
var SparkPost = require('sparkpost');
var client = new SparkPost('YOUR_API_KEY');
```javascript
var SparkPost = require('sparkpost')
, client = new SparkPost('YOUR_API_KEY');

client.webhooks.all()
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Using a callback
client.webhooks.all(function(err, data) {
if(err) {
console.log('Whoops! Something went wrong');
console.log(err);
return;
} else {
console.log('Congrats you can use our client library!');
console.log(data);
}

console.log(data);
});

```
Expand Down
22 changes: 17 additions & 5 deletions examples/webhooks/create_webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@ var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key)
, webhook = {
name: 'Test webhook'
, target: 'http://client.test.com/test-webhook'
, auth_token: 'AUTH_TOKEN'
, events: [
name: 'Test Webhook',
target: 'http://client.test.com/test-webhook',
auth_token: 'AUTH_TOKEN',
events: [
'delivery',
'injection',
'open',
'click'
]
};

client.webhooks.create(webhook)
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Using a callback
client.webhooks.create(webhook, function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log(data);
console.log('Congrats you can use our client library!');
console.log(data);
}
});
16 changes: 14 additions & 2 deletions examples/webhooks/delete_webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key);

client.webhooks['delete']('TEST_WEBHOOK_UUID', function(err, data) {
client.webhooks.delete('TEST_WEBHOOK_UUID')
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Using a callback
client.webhooks.delete('TEST_WEBHOOK_UUID', function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log(data);
console.log('Congrats you can use our client library!');
console.log(data);
}
});
18 changes: 15 additions & 3 deletions examples/webhooks/describe_webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@ var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key)
, options = {
id: 'TEST_WEBHOOK_UUID'
, timezone: 'America/New_York'
id: 'TEST_WEBHOOK_UUID',
timezone: 'America/New_York'
};

client.webhooks.describe(options)
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Using a callback
client.webhooks.describe(options, function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log(data);
console.log('Congrats you can use our client library!');
console.log(data);
}
});
18 changes: 15 additions & 3 deletions examples/webhooks/getBatchStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@ var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key)
, options = {
id: 'TEST_WEBHOOK_UUID'
, limit: 1000
id: 'TEST_WEBHOOK_UUID',
limit: 1000
};

client.webhooks.getBatchStatus(options)
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Using a callback
client.webhooks.getBatchStatus(options, function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log(data);
console.log('Congrats you can use our client library!');
console.log(data);
}
});
14 changes: 13 additions & 1 deletion examples/webhooks/getDocumentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key);

client.webhooks.getDocumentation()
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Using a callback
client.webhooks.getDocumentation(function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log(data);
console.log('Congrats you can use our client library!');
console.log(data);
}
});
14 changes: 13 additions & 1 deletion examples/webhooks/getSamples.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@ var key = 'YOURAPIKEY'
events: 'bounce'
};

client.webhooks.getSamples(options)
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Using a callback
client.webhooks.getSamples(options, function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log(data);
console.log('Congrats you can use our client library!');
console.log(data);
}
});
14 changes: 13 additions & 1 deletion examples/webhooks/get_all_webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key);

client.webhooks.all()
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Using a callback
client.webhooks.all(function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log(data);
console.log('Congrats you can use our client library!');
console.log(data);
}
});
20 changes: 16 additions & 4 deletions examples/webhooks/update_webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,31 @@ var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key)
, webhook = {
id: 'TEST_WEBHOOK_UUID'
, name: 'Renamed Test webhook'
, events: [
id: 'TEST_WEBHOOK_UUID',
name: 'Renamed Test Webhook',
events: [
'policy_rejection',
'delay'
]
};

client.webhooks.update(webhook)
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Using a callback
client.webhooks.update(webhook, function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log(data);
console.log('Congrats you can use our client library!');
console.log(data);
}
});
18 changes: 15 additions & 3 deletions examples/webhooks/validate_webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,29 @@ var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key)
, options = {
id: 'TEST_WEBHOOK_UUID'
, message: {
id: 'TEST_WEBHOOK_UUID',
message: {
msys: {}
}
};

client.webhooks.validate(options)
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
})
.catch(err => {
console.log('Whoops! Something went wrong');
console.log(err);
});

// Using a callback
client.webhooks.validate(options, function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log(data);
console.log('Congrats you can use our client library!');
console.log(data);
}
});
Loading

0 comments on commit b55f9c4

Please sign in to comment.