Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[contact-importer] Update to support @sendgrid/client v6.X #770

Merged
merged 2 commits into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/contact-importer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

**This package is part of a monorepo, please see [this README](https://github.com/sendgrid/sendgrid-nodejs/blob/master/README.md) for details.**

This package has yet to be updated to support v6.0.0 of the `sendgrid-nodejs` SDK. Currently, this package is a placeholder. Please use [this package](https://www.npmjs.com/package/sendgrid) until the upgrade. The related issue is located [here](https://github.com/sendgrid/sendgrid-nodejs/issues/427).

To be notified when this package is updated, please subscribe to email [notifications](https://dx.sendgrid.com/newsletter/nodejs) for releases and breaking changes.

<a name="contribute"></a>
Expand Down
6 changes: 3 additions & 3 deletions packages/contact-importer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
"access": "public"
},
"dependencies": {
"@sendgrid/client": "^6.3.0",
"async.ensureasync": "^0.5.2",
"async.queue": "^0.5.2",
"bottleneck": "^1.12.0",
"debug": "^2.2.0",
"lodash.chunk": "^4.2.0",
"sendgrid": "^5.2.3"
"debug": "^4.0.1",
"lodash.chunk": "^4.2.0"
},
"tags": [
"sendgrid"
Expand Down
29 changes: 16 additions & 13 deletions packages/contact-importer/src/importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,23 @@ class ContactImporter extends EventEmitter {
* @param {Object} task Task to be processed (data in 'data' property)
* @param {Function} callback Callback function.
*/
_worker (task, callback) {
_worker(task, callback) {
const context = task.owner;
debug('processing batch (%s items)', task.data.length);
context.throttle.submit(context._sendBatch, context, task.data, callback);
}

_sendBatch (context, data, callback) {
_sendBatch(context, data, callback) {
debug('sending batch (%s items)', data.length);

const request = context.sg.emptyRequest();
request.method = 'POST';
request.path = '/v3/contactdb/recipients';
request.body = data;
const request = {
method: 'POST',
path: '/v3/contactdb/recipients',
body: data,
};

context.sg.API(request)
.then((response) => {
context.sg.request(request)
.then(([response]) => {
debug('got response: %o', response);
setTimeout(() => {
context.throttle.incrementReservoir(1);
Expand All @@ -106,23 +107,25 @@ class ContactImporter extends EventEmitter {
* @param {Object} error
* @param {Object} result
*/
_notify (error, result, batch) {
_notify(error, result, batch) {
if (error) {
return this.emit('error', error, batch);
}
return this.emit('success', result, batch);
};
}

/**
* Sets up the queue object on this instance of ContactImporter
*/
_setupQueue () {
_setupQueue() {
// Create a queue that wil be used to send batches to the throttler.
this.queue = queue(ensureAsync(this._worker));

// When the last batch is removed from the queue, add any incomplete batches.
this.queue.empty = () => {
if (!this.pendingItems.length) return;
if (!this.pendingItems.length) {
return;
}

debug('adding %s items from deferrd queue for processing', this.pendingItems.length);

Expand All @@ -141,7 +144,7 @@ class ContactImporter extends EventEmitter {
*
* @param {Array} batch A batch to send to the queue.
*/
_pushToQueue (batch) {
_pushToQueue(batch) {
this.queue.push({
data: batch,
owner: this,
Expand Down
6 changes: 3 additions & 3 deletions packages/contact-importer/src/importer.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const sendgrid = require('sendgrid');
const sendgrid = require('@sendgrid/client');
const ContactImporter = require('./importer');

describe('test_contact_importer', function() {
beforeEach(function() {
// Create a new SendGrid instance.
const API_KEY = process.env.API_KEY;
const sg = sendgrid(API_KEY);
sendgrid.setApiKey(API_KEY);

// Create a new importer with a batch size of 2.
this.contactImporter = new ContactImporter(sg, {
this.contactImporter = new ContactImporter(sendgrid, {
batchSize: 2,
});
// this.spy = sinon.spy(ContactImporter.prototype, '_sendBatch')
Expand Down
Loading