Skip to content

Commit

Permalink
Updated docs and examples for suppression, added async doc
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonrhodes committed Sep 6, 2016
1 parent 2acd01e commit 7d279f3
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 58 deletions.
40 changes: 40 additions & 0 deletions docs/async.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Async Handling

Callbacks and promises living together... MASS HYSTERIA. Or not! This library handles both of your favorite async strategies.

### Promises

All our library methods return promises, so if you're a promise-fan, just do what you normally do.

```javascript
let client = new SparkPost(key);

client.templates.get(id)
.then((data) => {
// this is either:
// a) the `results` key of the API response body, or
// b) the full API response body
})
.catch((err) => {
// handle the sad error
});
```

### Callbacks

If you're more of a callbacker, that works too. Pass a callback as the last argument and it'll be handled like a regular, error-first callback.

```javascript
let client = new SparkPost(key);

client.templates.get(id, (err, data) => {
if (err) {
// handle the sad error
return;
}

// this is either:
// a) the `results` key of the API response body, or
// b) the full API response body
});
```
59 changes: 10 additions & 49 deletions docs/resources/suppressionList.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,22 @@

This library provides easy access to the [Suppression List](https://developers.sparkpost.com/api/suppression-list) Resource.

*Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).*

## Methods
* **search(parameters[, callback]) &rarr; `{Promise}`**<br />
Perform a filtered search for entries in your suppression list.
* `parameters` - an object of [search parameters](https://developers.sparkpost.com/api/suppression-list#suppression-list-search-get)
* `callback` - executed after task is completed if provided*
* standard `callback(err, data)`
* `err` - any error that occurred
* `data` - full response from request client
* **getEntry(email[, callback]) &rarr; `{Promise}`**<br />
* **list([parameters])**<br />
List all entries in your suppression list, filtered by an optional set of search parameters.
* `parameters` - an object of [search parameters](https://developers.sparkpost.com/api/suppression-list#suppression-list-search-get)
* **get(email)**<br />
Retrieve an entry by recipient email.
* `email` - `String` email address to check **required**
* `callback` - see search function
* **deleteEntry(email[, callback]) &rarr; `{Promise}`**<br />
Remove an entry by recipient email.
* `email` - `String` email address to remove **required**
* `callback` - see search function
* **upsert(listEntries[, callback]) &rarr; `{Promise}`**<br />
* **upsert(listEntries)**<br />
Insert or update one or many entries.
* `listEntries` - an object [entry list attributes](https://developers.sparkpost.com/api/suppression-list#header-list-entry-attributes) or `Array` of entry list attribute objects
* `callback` - see search function

*callback is optional because all methods return a Promise.
* **delete(email)**<br />
Remove an entry by recipient email.
* `email` - `String` email address to remove **required**

## Examples

```javascript
var SparkPost = require('sparkpost')
, client = new SparkPost('YOUR_API_KEY')
, parameters = {
from: '2015-05-07T00:00:00+0000',
to: '2015-05-07T23:59:59+0000',
limit: 5
};

client.suppressionList.search(parameters)
.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.suppressionList.search(parameters, function(err, data) {
if(err) {
console.log('Whoops! Something went wrong');
console.log(err);
} else {
console.log('Congrats you can use our client library!');
console.log(data);
}
});

```

Check out all the examples provided [here](/examples/suppressionList).
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key);

client.suppressionList.getEntry('test@test.com')
// Promise
client.suppressionList.delete('test@test.com')
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
Expand All @@ -14,8 +15,8 @@ client.suppressionList.getEntry('test@test.com')
console.log(err);
});

// Using a callback
client.suppressionList.getEntry('test@test.com', function(err, data) {
// Callback
client.suppressionList.delete('test@test.com', function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key);

client.suppressionList.deleteEntry('test@test.com')
// Promise
client.suppressionList.get('test@test.com')
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
Expand All @@ -14,7 +15,8 @@ client.suppressionList.deleteEntry('test@test.com')
console.log(err);
});

client.suppressionList.deleteEntry('test@test.com', function(err, data) {
// Callback
client.suppressionList.get('test@test.com', function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ var key = 'YOURAPIKEY'
limit: 5
};

client.suppressionList.search(parameters)
// Promise
client.suppressionList.list(parameters)
.then(data => {
console.log('Congrats you can use our client library!');
console.log(data);
Expand All @@ -19,8 +20,8 @@ client.suppressionList.search(parameters)
console.log(err);
});

// Using a callback
client.suppressionList.search(parameters, function(err, data) {
// Callback
client.suppressionList.list(parameters, function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
console.log(err);
Expand Down
2 changes: 2 additions & 0 deletions examples/suppressionList/upsert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var key = 'YOURAPIKEY'
description: 'Test description 1'
};

// Promise
client.suppressionList.upsert(listEntry)
.then(data => {
console.log('Congrats you can use our client library!');
Expand All @@ -20,6 +21,7 @@ client.suppressionList.upsert(listEntry)
console.log(err);
});

// Callback
client.suppressionList.upsert(listEntry, function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
Expand Down
3 changes: 2 additions & 1 deletion examples/suppressionList/upsert_bulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var key = 'YOURAPIKEY'
}
];

// Promise
client.suppressionList.upsert(listEntries)
.then(data => {
console.log('Congrats you can use our client library!');
Expand All @@ -34,7 +35,7 @@ client.suppressionList.upsert(listEntries)
console.log(err);
});

// Using a callback
// Callback
client.suppressionList.upsert(listEntries, function(err, data) {
if (err) {
console.log('Whoops! Something went wrong');
Expand Down

0 comments on commit 7d279f3

Please sign in to comment.