Skip to content

Commit

Permalink
Merge pull request #121 from aydrian/issue-117
Browse files Browse the repository at this point in the history
Added support for Relay Webhooks
  • Loading branch information
aydrian committed Mar 28, 2016
2 parents d6f81e7 + 57ab0e2 commit 4f4bff2
Show file tree
Hide file tree
Showing 10 changed files with 417 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ sp.transmissions.send({
## SparkPost API Resources Supported in Node Client Library
Click on the desired API to see usage and more information

* [Inbound Domains](/docs/resources/inboundDomains.md) - `client.inboundDomains` ([examples](/examples/inboundDomains))
* [Message Events](/docs/resources/messageEvents.md) - `client.messageEvents` ([examples](/examples/messageEvents))
* [Recipient Lists](/docs/resources/recipientLists.md) - `client.recipientLists` ([examples](/examples/recipientLists))
* [Relay Webhooks](/docs/resources/relayWebhooks.md) - `client.relayWebhooks` ([examples](/examples/relayWebhooks))
* [Sending Domains](/docs/resources/sendingDomains.md) - `client.sendingDomains` ([examples](/examples/sendingDomains))
* [Suppression List](/docs/resources/suppressionList.md) - `client.suppressionList` ([examples](/examples/suppressionList))
* [Templates](/docs/resources/templates.md) - `client.templates` ([examples](/examples/templates))
Expand Down
55 changes: 55 additions & 0 deletions docs/resources/relayWebhooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Relay Webhooks

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

## Methods
* **all(options, callback)**
List all relay webhooks.
* `callback` - executed after task is completed. **required**
* standard `callback(err, data)`
* `err` - any error that occurred
* `data` - full response from request client
* **find(webhookId, callback)**
Retrieve details about a specified relay webhook by its id
* `webhookId` - the id of the relay webhook you want to look up **required**
* `callback` - see all function
* **create(options, callback)**
Create a new recipient list
* `options.target` - url of the target to which to POST relay batches **required**
* `options.domain` - inbound domain associated with this webhook **required**
* `options.name` - user-friendly name
* `options.authToken` - authentication token to present in the X-MessageSystems-Webhook-Token header of POST requests to target
* `options.protocol` - inbound messaging protocol associated with this webhook
* `callback` - see all function
* **update(options, callback)**
Update an existing relay webhook
* `options.webhookId` - the id of the relay webhook you want to update **required**
* `options.target` - url of the target to which to POST relay batches
* `options.domain` - inbound domain associated with this webhook
* `options.name` - user-friendly name
* `options.authToken` - authentication token to present in the X-MessageSystems-Webhook-Token header of POST requests to target
* `options.protocol` - inbound messaging protocol associated with this webhook
* `callback` - see all function
* **delete(webhookId, callback)**
Delete an existing relay webhook
* `webhookId` - the id of the webhook you want to delete **required**
* `callback` - see all function

## Examples

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

client.relayWebhooks.all(function(err, data) {
if(err) {
console.log(err);
return;
}

console.log(data.body);
});

```

Check out all the examples provided [here](/examples/relayWebhooks).
19 changes: 19 additions & 0 deletions examples/relayWebhooks/create_relayWebhook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key)
, options = {
name: 'Test Relay Webhook'
, target: 'http://client.test.com/test-webhook'
, domain: 'inbound.example.com'
};

client.relayWebhooks.create(options, function(err, res) {
if (err) {
console.log(err);
} else {
console.log(res.body);
console.log('Congrats you can use our client library!');
}
});
15 changes: 15 additions & 0 deletions examples/relayWebhooks/delete_relayWebhook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key)
, relayWebhookId = '123456789';

client.relayWebhooks.delete(relayWebhookId, function(err, res) {
if (err) {
console.log(err);
} else {
console.log(res.body);
console.log('Congrats you can use our client library!');
}
});
15 changes: 15 additions & 0 deletions examples/relayWebhooks/find_relayWebhook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key)
, relayWebhookId = '123456789';

client.relayWebhooks.find(relayWebhookId, function(err, res) {
if (err) {
console.log(err);
} else {
console.log(res.body);
console.log('Congrats you can use our client library!');
}
});
14 changes: 14 additions & 0 deletions examples/relayWebhooks/get_all_relayWebhooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key);

client.relayWebhooks.all(function(err, res) {
if (err) {
console.log(err);
} else {
console.log(res.body);
console.log('Congrats you can use our client library!');
}
});
18 changes: 18 additions & 0 deletions examples/relayWebhooks/update_relayWebhook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key)
, options = {
relayWebhookId: '123456789'
, target: 'http://client.test.com/test-webhook'
};

client.relayWebhooks.update(options, function(err, res) {
if (err) {
console.log(err);
} else {
console.log(res.body);
console.log('Congrats you can use our client library!');
}
});
114 changes: 114 additions & 0 deletions lib/relayWebhooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
'use strict';

var api = 'relay-webhooks';

var toApiFormat = function(input) {
var model = {
match: {}
};

model.target = input.target;
model.match.domain = input.domain;

model.name = input.name;
model.auth_token = input.authToken;
model.match.protocol = input.protocol;

return model;
};

module.exports = function(client) {
var relayWebhooks = {
all: function(callback) {
var reqOpts = {
uri: api
};
client.get(reqOpts, callback);
},
find: function(relayWebhookId, callback) {
if(typeof relayWebhookId === 'function') {
callback = relayWebhookId;
relayWebhookId = null;
}

if(!relayWebhookId) {
callback(new Error('relayWebhookId is required'));
return;
}

var options = {
uri: api + '/' + relayWebhookId
};
client.get(options, callback);
},
create: function(options, callback) {
if(typeof options === 'function') {
callback = options;
options = null;
}

if(!options) {
callback(new Error('options are required'));
return;
}

if(!options.target) {
callback(new Error('target is required in options'));
return;
}

if(!options.domain) {
callback(new Error('domain is required in options'));
return;
}

var reqOpts = {
uri: api
, json: toApiFormat(options)
};
client.post(reqOpts, callback);
},
update: function(options, callback) {
if(typeof options === 'function') {
callback = options;
options = null;
}

if(!options) {
callback(new Error('options are required'));
return;
}

if(!options.relayWebhookId) {
callback(new Error('relayWebhookId is required in options'));
return;
}

var relayWebhookId = options.relayWebhookId;
var reqOpts = {
uri: api + '/' + relayWebhookId
, json: toApiFormat(options)
};
client.put(reqOpts, callback);
},
delete: function(relayWebhookId, callback) {
if (typeof relayWebhookId === 'function') {
callback = relayWebhookId;
relayWebhookId = null;
}

if (!relayWebhookId) {
callback(new Error('relayWebhookId is required'));
return;
}

var options = {
uri: api + '/' + relayWebhookId
};

client.delete(options, callback);
}
};

return relayWebhooks;
};
3 changes: 2 additions & 1 deletion lib/sparkpost.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ var SparkPost = function(apiKey, options) {
this.apiVersion = options.apiVersion || defaults.apiVersion;

this.inboundDomains = require('./inboundDomains')(this);
this.messageEvents = require('./messageEvents')(this);
this.recipientLists = require('./recipientLists')(this);
this.relayWebhooks = require('./relayWebhooks')(this);
this.sendingDomains = require('./sendingDomains')(this);
this.suppressionList = require('./suppressionList')(this);
this.templates = require('./templates')(this);
this.transmissions = require('./transmissions')(this);
this.webhooks = require('./webhooks')(this);
this.messageEvents = require('./messageEvents')(this);
};

SparkPost.prototype.request = function( options, callback ) {
Expand Down
Loading

0 comments on commit 4f4bff2

Please sign in to comment.