From 90f194d29802d3f6863e9d2d7bb9b3b67d9da983 Mon Sep 17 00:00:00 2001 From: ItsSudip Date: Fri, 14 Jun 2024 09:34:48 +0530 Subject: [PATCH] chore: move mailchimp from myaxios to httpget --- src/v0/destinations/mailchimp/utils.js | 80 +++++++++---------- .../destinations/mailchimp/network.ts | 38 +++++++++ 2 files changed, 78 insertions(+), 40 deletions(-) diff --git a/src/v0/destinations/mailchimp/utils.js b/src/v0/destinations/mailchimp/utils.js index a726f23a39..8cc42cc177 100644 --- a/src/v0/destinations/mailchimp/utils.js +++ b/src/v0/destinations/mailchimp/utils.js @@ -5,7 +5,6 @@ const { NetworkError, structuredLogger: logger, } = require('@rudderstack/integrations-lib'); -const myAxios = require('../../../util/myAxios'); const { MappedToDestinationKey } = require('../../../constants'); const { isDefinedAndNotNull, @@ -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']; @@ -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; }; @@ -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; }; /** diff --git a/test/integrations/destinations/mailchimp/network.ts b/test/integrations/destinations/mailchimp/network.ts index b036bf566c..a29712ce9a 100644 --- a/test/integrations/destinations/mailchimp/network.ts +++ b/test/integrations/destinations/mailchimp/network.ts @@ -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, + }, + }, ];