Skip to content

Commit

Permalink
Updated subaccounts doc and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
aydrian committed Aug 31, 2016
1 parent 443a776 commit 25b8ec9
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 41 deletions.
55 changes: 31 additions & 24 deletions docs/resources/subaccounts.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,53 @@
# Subaccounts

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

## Methods
* **all(callback)**
* **all([callback]) &rarr; `{Promise}`**<br />
List a summary of all subaccounts.
* `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
* **find(subaccountId, callback)**
* **find(id[, callback]) &rarr; `{Promise}`**<br />
Retrieve details about a specified subaccount by its id
* `subaccountId` - the id of the subaccount you want to look up **required**
* `id` - the id of the subaccount you want to look up **required**
* `callback` - see all function
* **create(options, callback)**
* **create(subaccount[, callback]) &rarr; `{Promise}`**<br />
Create a new subaccount
* `options.name` - user-friendly name **required**
* `options.keyLabel` - user-friendly identifier for subaccount API key **required**
* `options.keyGrants` - list of grants to give the subaccount API key **required**
* `options.keyValidIps` - list of IPs the subaccount may be used from
* `options.ipPool` - id of the default IP pool assigned to subaccount's transmissions
* `subaccount` - an object of [subaccount attributes](https://developers.sparkpost.com/api/subaccounts#header-request-body-attributes) **required**
* `callback` - see all function
* **update(options, callback)**
* **update(subaccount[, callback]) &rarr; `{Promise}`**<br />
Updates an existing subaccount
* `options.subaccountId` - the id of the subaccount you want to update **required**
* `options.name` - user-friendly name
* `options.status` - status of the subaccount
* `options.ipPool` - id of the default IP pool assigned to subaccount's transmissions
* `callback` - see all function
* `subaccount` - an object of [updatable subaccount attributes](https://developers.sparkpost.com/api/subaccounts#header-request-body-attributes-1) **required**
* `subaccount.id` - the id of the subaccount you want to update **required**

*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.subaccounts.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.subaccounts.all(function(err, data) {
if(err) {
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);
});
```
28 changes: 20 additions & 8 deletions examples/subaccounts/create_subaccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@
var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key)
, options = {
name: 'Test Subaccount'
, keyLabel: 'Test Subaccount key'
, keyGrants: [
'smtp/inject'
, 'transmissions/modify'
, subaccount = {
name: 'Test Subaccount',
key_label: 'Test Subaccount key',
key_grants: [
'smtp/inject',
'transmissions/modify'
]
};

client.subaccounts.create(options, function(err, data) {
client.subaccounts.create(subaccount)
.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.subaccounts.create(subaccount, 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/subaccounts/get_all_subaccounts.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.subaccounts.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.subaccounts.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);
}
});
16 changes: 14 additions & 2 deletions examples/subaccounts/get_subaccount.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.subaccounts.find(123, function(err, data) {
client.subaccounts.find('123')
.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.subaccounts.find('123', 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);
}
});
24 changes: 18 additions & 6 deletions examples/subaccounts/update_subaccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@
var key = 'YOURAPIKEY'
, SparkPost = require('sparkpost')
, client = new SparkPost(key)
, options = {
subaccountId: 123
, name: 'Test Subaccount'
, status: 'suspended'
, subaccount = {
id: 123,
name: 'Test Subaccount',
status: 'suspended'
};

client.subaccounts.update(options, function(err, data) {
client.subaccounts.update(subaccount)
.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.subaccounts.update(subaccount, 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);
}
});

0 comments on commit 25b8ec9

Please sign in to comment.