Skip to content

Commit

Permalink
[SE-231] Events api added with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sstaley-sparkpost committed Sep 20, 2019
1 parent 185814e commit 6a8bc2e
Show file tree
Hide file tree
Showing 6 changed files with 562 additions and 327 deletions.
29 changes: 29 additions & 0 deletions examples/events/search_ingest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

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

// Returns 1000 ingest events for the last hour

// Promise
client.events.ingest.search({})
.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);
});

// Callback
client.events.ingest.search({}, 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);
}
});
29 changes: 29 additions & 0 deletions examples/events/search_message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

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

// Returns 1000 message events for the last hour

// Promise
client.events.message.search({})
.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);
});

// Callback
client.events.message.search({}, 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);
}
});
41 changes: 41 additions & 0 deletions lib/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

let api = 'events';

/*
* "Class" declaration, Events API exposes one function:
* - search: retrieves list of events according to given params
*/
module.exports = function(client) {

function search(parameters, callback) {
const options = {
uri: api
, qs: {}
};

Object.keys(parameters).forEach(function(paramname) {
if (Array.isArray(parameters[paramname])) {
options.qs[paramname] = parameters[paramname].join(',');
} else {
options.qs[paramname] = parameters[paramname];
}
});
return client.get(options, callback);
}

return {
message: {
search: function(parameters, callback) {
api += '/message';
return search(parameters, callback);
}
},
ingest: {
search: function(parameters, callback) {
api += '/ingest';
return search(parameters, callback);
},
}
};
};
1 change: 1 addition & 0 deletions lib/sparkpost.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const SparkPost = function(apiKey, options) {

this.inboundDomains = require('./inboundDomains')(this);
this.messageEvents = require('./messageEvents')(this);
this.events = require('./events')(this);
this.recipientLists = require('./recipientLists')(this);
this.relayWebhooks = require('./relayWebhooks')(this);
this.sendingDomains = require('./sendingDomains')(this);
Expand Down
Loading

0 comments on commit 6a8bc2e

Please sign in to comment.