Skip to content

Commit

Permalink
updates to baseUrl setter
Browse files Browse the repository at this point in the history
  • Loading branch information
shrutiburman committed Nov 23, 2023
1 parent a21b169 commit b356716
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
8 changes: 6 additions & 2 deletions packages/client/src/classes/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Client {
constructor() {
this.auth = '';
this.impersonateSubuser = '';
this.sendgrid_region = '';

this.defaultHeaders = {
Accept: 'application/json',
Expand All @@ -43,8 +44,10 @@ class Client {

setApiKey(apiKey) {
this.auth = 'Bearer ' + apiKey;
this.setDefaultRequest('baseUrl', SENDGRID_BASE_URL);

// this means that region was never set before
if (this.sendgrid_region == '') {
this.setDefaultRequest('baseUrl', SENDGRID_BASE_URL);
}
if (!this.isValidApiKey(apiKey)) {
console.warn(`API key does not start with "${API_KEY_PREFIX}".`);
}
Expand Down Expand Up @@ -108,6 +111,7 @@ class Client {
if (!REGION_HOST_MAP.hasOwnProperty(region)) {
console.warn('Region can only be "global" or "eu".');
} else {
this.sendgrid_region = region;
this.setDefaultRequest('baseUrl', REGION_HOST_MAP[region]);
}
return this;
Expand Down
23 changes: 16 additions & 7 deletions packages/client/src/client.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const nock = require('nock');
const sgClient = require('./client');
const testClient = require('./client');
let testClient = require('./client');
const testRequest = (request, statusCode) => {
const sgClient = require('./client');
sgClient.setApiKey('SG.API Key');
Expand Down Expand Up @@ -3094,23 +3094,26 @@ describe('test_whitelabel_links__link_id__subuser_post', () => {
});

describe('setDataResidency', () => {
const testClient = require('./client');
let consoleWarnSpy;

beforeEach(() => {
testClient = require('./client');
consoleWarnSpy = sinon.spy(console, 'warn');
});
afterEach(() => {
console.warn.restore();
});

it('should have default value of hostname as https://api.sendgrid.com/', () => {
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/');
expect(testClient.sendgrid_region).to.equal('');
});
it('should send to host EU', () => {
testClient.setDataResidency('eu');
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.eu.sendgrid.com/');
});
it('should send to host Global/default', () => {
testClient.setDataResidency('global');
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/');
expect(testClient.sendgrid_region).to.equal('global');
});
it('should override the existing set hostname, if data residency setter is called after', () => {
testClient.setApiKey('SG.1234567890');
Expand All @@ -3125,13 +3128,19 @@ describe('setDataResidency', () => {
testClient.setDataResidency(null);
expect(consoleWarnSpy.calledOnce).to.equal(true);
});
it('should give precedence to the order of execution', () => {
it('setting the API Key wont reset the region set', () => {
testClient.setDataResidency('eu');
testClient.setApiKey('SG.1234567890');
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/');
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.eu.sendgrid.com/');
expect(testClient.sendgrid_region).to.equal('eu');
});
it('should have default value of hostname as https://api.sendgrid.com/', () => {
it('should send to host global and then call setApiKey', () => {
testClient.setDataResidency('global');
testClient.setApiKey('SG.1234567890');
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/');
expect(testClient.sendgrid_region).to.equal('global');


});
});

0 comments on commit b356716

Please sign in to comment.