-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from aydrian/issue-117
Added support for Relay Webhooks
- Loading branch information
Showing
10 changed files
with
417 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.