Skip to content

Commit

Permalink
chore: move mailchimp from myaxios to httpget
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsSudip committed Jun 14, 2024
1 parent a96d40b commit 90f194d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 40 deletions.
80 changes: 40 additions & 40 deletions src/v0/destinations/mailchimp/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const {
NetworkError,
structuredLogger: logger,
} = require('@rudderstack/integrations-lib');
const myAxios = require('../../../util/myAxios');
const { MappedToDestinationKey } = require('../../../constants');
const {
isDefinedAndNotNull,
Expand All @@ -23,6 +22,7 @@ const { MERGE_CONFIG, MERGE_ADDRESS, SUBSCRIPTION_STATUS, VALID_STATUSES } = req
const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils');
const tags = require('../../util/tags');
const { JSON_MIME_TYPE } = require('../../util/constant');
const { httpGET } = require('../../../adapters/network');

const ADDRESS_MANDATORY_FIELDS = ['addr1', 'city', 'state', 'zip'];

Expand Down Expand Up @@ -158,28 +158,28 @@ const checkIfMailExists = async (apiKey, datacenterId, audienceId, email) => {
};
const url = `${mailChimpSubscriptionEndpoint(datacenterId, audienceId, email)}`;
const basicAuth = Buffer.from(`apiKey:${apiKey}`).toString('base64');
try {
const response = await myAxios.get(
url,
{
headers: {
Authorization: `Basic ${basicAuth}`,
},
},
{
destType: 'mailchimp',
feature: 'transformation',
endpointPath: '/lists/audienceId/members/email',
requestMethod: 'GET',
module: 'router',

const res = await httpGET(
url,
{
headers: {
Authorization: `Basic ${basicAuth}`,
},
);
if (response?.data?.contact_id) {
userStatus.exists = true;
userStatus.subscriptionStatus = response.data.status;
}
} catch (error) {
logger.info(`[Mailchimp] :: Email does not exists, Error: ${error.message}`);
},
{
destType: 'mailchimp',
feature: 'transformation',
endpointPath: '/lists/audienceId/members/email',
requestMethod: 'GET',
module: 'router',
},
);
if (res.response?.data?.contact_id) {
userStatus.exists = true;
userStatus.subscriptionStatus = res.response.data.status;
}
if (!res.status) {
logger.info(`[Mailchimp] :: Email does not exists, Error: ${res.response.message}`);
}
return userStatus;
};
Expand All @@ -192,32 +192,32 @@ const checkIfMailExists = async (apiKey, datacenterId, audienceId, email) => {
* @returns
*/
const checkIfDoubleOptIn = async (apiKey, datacenterId, audienceId) => {
let response;
const url = `${getMailChimpBaseEndpoint(datacenterId, audienceId)}`;
const basicAuth = Buffer.from(`apiKey:${apiKey}`).toString('base64');
try {
response = await myAxios.get(
url,
{
headers: {
Authorization: `Basic ${basicAuth}`,
},
},
{
destType: 'mailchimp',
feature: 'transformation',
endpointPath: '/lists/audienceId',
requestMethod: 'GET',
module: 'router',
const res = await httpGET(
url,
{
headers: {
Authorization: `Basic ${basicAuth}`,
},
);
} catch (error) {
},
{
destType: 'mailchimp',
feature: 'transformation',
endpointPath: '/lists/audienceId',
requestMethod: 'GET',
module: 'router',
},
);
if (!res.success) {
const error = res.response;
const status = error.status || 400;
throw new NetworkError('User does not have access to the requested operation', status, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status),
});
}
return !!response.data.double_optin;

return !!res.response?.data?.double_optin;
};

/**
Expand Down
38 changes: 38 additions & 0 deletions test/integrations/destinations/mailchimp/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,42 @@ export const networkCallsData = [
status: 200,
},
},
{
httpReq: {
url: 'https://usXX.api.mailchimp.com/3.0/lists/aud111/members/5587981bdf09024971ff9ddfb2590a6d',
method: 'GET',
headers: {
Authorization: 'Basic YXBpS2V5OmFwaUtleS1kdW1teUFwaUtleQ==',
},
},
httpRes: {
data: {
type: 'https://mailchimp.com/developer/marketing/docs/errors/',
title: 'API Key Invalid',
status: 401,
detail: "Your API key may be invalid, or you've attempted to access the wrong datacenter.",
instance: 'd2e09e5b-7c28-8585-68db-8feaf57ee0f7',
},
status: 401,
},
},
{
httpReq: {
url: 'https://usXX.api.mailchimp.com/3.0/lists/aud111',
method: 'GET',
headers: {
Authorization: 'Basic YXBpS2V5OmFwaUtleS1kdW1teUFwaUtleQ==',
},
},
httpRes: {
data: {
type: 'https://mailchimp.com/developer/marketing/docs/errors/',
title: 'API Key Invalid',
status: 401,
detail: "Your API key may be invalid, or you've attempted to access the wrong datacenter.",
instance: 'd2e09e5b-7c28-8585-68db-8feaf57ee0f7',
},
status: 401,
},
},
];

0 comments on commit 90f194d

Please sign in to comment.