Skip to content

Commit 85b768a

Browse files
Merge pull request #243 from SparkPost/SE-231
[SE-231] Events api added with tests
2 parents 185814e + e817882 commit 85b768a

File tree

6 files changed

+476
-327
lines changed

6 files changed

+476
-327
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
node_modules
22
xunit.xml
33
test/reports/*
4+
5+
# vscode
6+
.vscode

examples/events/search_message.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
var key = 'YOURAPIKEY'
4+
, SparkPost = require('sparkpost')
5+
, client = new SparkPost(key);
6+
7+
// Returns 1000 message events for the last hour
8+
9+
// Promise
10+
client.events.searchMessage({})
11+
.then(data => {
12+
console.log('Congrats you can use our client library!');
13+
console.log(data);
14+
})
15+
.catch(err => {
16+
console.log('Whoops! Something went wrong');
17+
console.log(err);
18+
});
19+
20+
// Callback
21+
client.events.searchMessage({}, function(err, data) {
22+
if (err) {
23+
console.log('Whoops! Something went wrong');
24+
console.log(err);
25+
} else {
26+
console.log('Congrats you can use our client library!');
27+
console.log(data);
28+
}
29+
});

lib/events.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const api = 'events';
4+
5+
/*
6+
* "Class" declaration, Events API exposes one function:
7+
* - search: retrieves list of message events according to given params
8+
*/
9+
module.exports = function(client) {
10+
return {
11+
/**
12+
* Search for events using given parameters
13+
*
14+
* @param {Object} parameters
15+
* @param {RequestCb} [callback]
16+
* @returns {Promise}
17+
*/
18+
searchMessage: function(parameters, callback) {
19+
const options = {
20+
uri: `${api}/message`
21+
, qs: {}
22+
};
23+
24+
Object.keys(parameters).forEach(function(paramname) {
25+
if (Array.isArray(parameters[paramname])) {
26+
options.qs[paramname] = parameters[paramname].join(',');
27+
} else {
28+
options.qs[paramname] = parameters[paramname];
29+
}
30+
});
31+
return client.get(options, callback);
32+
}
33+
};
34+
};

lib/sparkpost.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const SparkPost = function(apiKey, options) {
7878

7979
this.inboundDomains = require('./inboundDomains')(this);
8080
this.messageEvents = require('./messageEvents')(this);
81+
this.events = require('./events')(this);
8182
this.recipientLists = require('./recipientLists')(this);
8283
this.relayWebhooks = require('./relayWebhooks')(this);
8384
this.sendingDomains = require('./sendingDomains')(this);

0 commit comments

Comments
 (0)