Skip to content

Commit

Permalink
feat: migrated myaxios calls
Browse files Browse the repository at this point in the history
  • Loading branch information
aashishmalik committed Nov 13, 2023
1 parent 3891b1f commit 8d7a136
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 165 deletions.
62 changes: 31 additions & 31 deletions src/v0/destinations/delighted/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const {
InstrumentationError,
NetworkError,
} = require('@rudderstack/integrations-lib');
const myAxios = require('../../../util/myAxios');
const { handleHttpRequest } = require('../../../adapters/network');
const { isHttpStatusSuccess } = require('../../util');
const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils');
const { getValueFromMessage } = require('../../util');
const { ENDPOINT } = require('./config');
Expand Down Expand Up @@ -48,11 +49,9 @@ const userValidity = async (channel, Config, userId) => {
} else if (channel === 'sms') {
paramsdata.phone_number = userId;
}

const basicAuth = Buffer.from(Config.apiKey).toString('base64');
let response;
try {
response = await myAxios.get(
const { processedResponse: processedResponseDl } = await handleHttpRequest(
'get',
`${ENDPOINT}`,
{
headers: {
Expand All @@ -63,34 +62,35 @@ const userValidity = async (channel, Config, userId) => {
},
{ destType: 'delighted', feature: 'transformation' },
);
if (response && response.data && response.status === 200 && Array.isArray(response.data)) {
return response.data.length > 0;
}
throw new NetworkInstrumentationError('Invalid response');
} catch (error) {
let errMsg = '';
let errStatus = 400;
if (error.response && error.response.data) {
errMsg = JSON.stringify(error.response.data);
switch (error.response.status) {
case 422:
case 401:
case 406:
case 403:
errStatus = 400;
break;
case 500:
case 503:
errStatus = 500;
break;
default:
errStatus = 400;

if (!isHttpStatusSuccess(processedResponseDl.status)) {
let errMsg = '';
let errStatus = 400;
if (processedResponseDl.response) {
errMsg = JSON.stringify(processedResponseDl.response);
switch (processedResponseDl.status) {
case 422:
case 401:
case 406:
case 403:
errStatus = 400;
break;
case 500:
case 503:
errStatus = 500;
break;
default:
errStatus = 400;
}
}
throw new NetworkError(`Error occurred while checking user : ${errMsg}`, errStatus, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errStatus),
});
}
throw new NetworkError(`Error occurred while checking user : ${errMsg}`, errStatus, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errStatus),
});
}
if (processedResponseDl.status === 200 && Array.isArray(processedResponseDl.response)) {
return processedResponseDl.response.length > 0;
}
throw new NetworkInstrumentationError('Invalid response');
};
const eventValidity = (Config, message) => {
const event = getValueFromMessage(message, 'event');
Expand Down
49 changes: 26 additions & 23 deletions src/v0/destinations/drip/util.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { NetworkError, AbortedError } = require('@rudderstack/integrations-lib');
const myAxios = require('../../../util/myAxios');
const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils');
const logger = require('../../../logger');
const { constructPayload, isDefinedAndNotNull } = require('../../util');
const { constructPayload, isDefinedAndNotNull, isHttpStatusSuccess } = require('../../util');
const { ENDPOINT, productMapping } = require('./config');
const tags = require('../../util/tags');
const { JSON_MIME_TYPE } = require('../../util/constant');
const { handleHttpRequest } = require('../../../adapters/network');

const isValidEmail = (email) => {
const re =
Expand All @@ -21,9 +21,9 @@ const isValidTimestamp = (timestamp) => {

const userExists = async (Config, id) => {
const basicAuth = Buffer.from(Config.apiKey).toString('base64');
let response;
try {
response = await myAxios.get(
const { processedResponse: processedResponseDrip } = await handleHttpRequest(
'get',
`${ENDPOINT}/v2/${Config.accountId}/subscribers/${id}`,
{
headers: {
Expand All @@ -33,17 +33,20 @@ const userExists = async (Config, id) => {
},
{ destType: 'drip', feature: 'transformation' },
);
if (response && response.status) {
return response.status === 200;

if (!isHttpStatusSuccess(processedResponseDrip.status)) {
throw new NetworkError(
'Invalid response.',
processedResponseDrip.status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponseDrip.status),
},
processedResponseDrip.response,
);
}
if (processedResponseDrip?.status) {
return processedResponseDrip.status === 200;
}
throw new NetworkError(
'Invalid response.',
response?.status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(response?.status),
},
response,
);
} catch (error) {
let errMsg = '';
let errStatus = 400;
Expand All @@ -57,11 +60,12 @@ const userExists = async (Config, id) => {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errStatus),
});
}
return false;
};

const createUpdateUser = async (finalpayload, Config, basicAuth) => {
try {
const response = await myAxios.post(
const { processedResponse: processedResponseDrip } = await handleHttpRequest(
'post',
`${ENDPOINT}/v2/${Config.accountId}/subscribers`,
finalpayload,
{
Expand All @@ -72,17 +76,16 @@ const createUpdateUser = async (finalpayload, Config, basicAuth) => {
},
{ destType: 'drip', feature: 'transformation' },
);
if (response) {
return response.status === 200 || response.status === 201;

if (processedResponseDrip) {
return processedResponseDrip.status === 200 || processedResponseDrip.status === 201;
}
throw new AbortedError('Invalid response.');
} catch (error) {

let errMsg = '';
if (error.response && error.response.data) {
errMsg = JSON.stringify(error.response.data);
if (processedResponseDrip.response) {
errMsg = JSON.stringify(processedResponseDrip.response);
}
throw new AbortedError(`Error occurred while creating or updating user : ${errMsg}`);
}
};

const createList = (productList) => {
Expand Down
2 changes: 1 addition & 1 deletion src/v0/destinations/gainsight/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const groupResponseBuilder = async (message, { Config }) => {
payload = removeUndefinedAndNullValues(payload);

let groupGsid;
if (resp.data.data.records.length === 0) {
if (resp?.data?.records?.length === 0) {
groupGsid = await createGroup(payload, Config);
} else {
groupGsid = await updateGroup(payload, Config);
Expand Down
99 changes: 53 additions & 46 deletions src/v0/destinations/gainsight/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ const {
RetryableError,
NetworkError,
} = require('@rudderstack/integrations-lib');
const myAxios = require('../../../util/myAxios');
const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils');
const logger = require('../../../logger');
const { ENDPOINTS, getLookupPayload } = require('./config');
const tags = require('../../util/tags');
const { JSON_MIME_TYPE } = require('../../util/constant');
const { handleHttpRequest } = require('../../../adapters/network');
const { isHttpStatusSuccess } = require('../../util');

const searchGroup = async (groupName, Config) => {
let resp;
try {
resp = await myAxios.post(
const { processedResponse: processedResponseGs } = await handleHttpRequest(
'post',
`${ENDPOINTS.groupSearchEndpoint(Config.domain)}`,
getLookupPayload(groupName),
{
Expand All @@ -24,28 +24,30 @@ const searchGroup = async (groupName, Config) => {
},
{ destType: 'gainsight', feature: 'transformation' },
);
} catch (error) {
let errMessage = '';
let errorStatus = 500;
if (error.response && error.response.data) {
errMessage = error.response.data.errorDesc;
errorStatus = error.response.status;

if (!isHttpStatusSuccess(processedResponseGs.status) && processedResponseGs >= 400 && processedResponseGs < 500) {
let errMessage = '';
let errorStatus = 500;
if (processedResponseGs.response) {
errMessage = processedResponseGs.response.errorDesc;
errorStatus = processedResponseGs.status;
}
throw new NetworkError(`failed to search group ${errMessage}`, errorStatus, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errorStatus),
}, processedResponseGs.response);
}
throw new NetworkError(`failed to search group ${errMessage}`, errorStatus, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errorStatus),
});
}

if (!resp || !resp.data || resp.status !== 200) {
throw new RetryableError('failed to search group');
if (processedResponseGs.status !== 200) {
throw new RetryableError('failed to search group', processedResponseGs.status, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponseGs.status),
}, processedResponseGs.response);
}
return resp;
return processedResponseGs.response;
};

const createGroup = async (payload, Config) => {
let resp;
try {
resp = await myAxios.post(
const { processedResponse: processedResponseGs } = await handleHttpRequest(
'post',
`${ENDPOINTS.groupCreateEndpoint(Config.domain)}`,
{
records: [payload],
Expand All @@ -58,28 +60,30 @@ const createGroup = async (payload, Config) => {
},
{ destType: 'gainsight', feature: 'transformation' },
);
} catch (error) {

if (!isHttpStatusSuccess(processedResponseGs.status) && processedResponseGs >= 400 && processedResponseGs < 500) {
let errMessage = '';
let errorStatus = 500;
if (error.response && error.response.data) {
errMessage = error.response.data.errorDesc;
errorStatus = error.response.status;
if (processedResponseGs.response) {
errMessage = processedResponseGs.response.errorDesc;
errorStatus = processedResponseGs.status;
}
throw new NetworkError(`failed to create group ${errMessage}`, errorStatus, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errorStatus),
});
}, processedResponseGs.response);
}

if (!resp || !resp.data || resp.status !== 200) {
throw new RetryableError('failed to create group');
if (processedResponseGs.status !== 200) {
throw new RetryableError('failed to create group', processedResponseGs.status, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponseGs.status),
}, processedResponseGs.response);
}
return resp.data.data.records[0].Gsid;
return processedResponseGs.response?.data?.records[0].Gsid;
};

const updateGroup = async (payload, Config) => {
let resp;
try {
resp = await myAxios.put(
const { processedResponse: processedResponseGs } = await handleHttpRequest(
'put',
`${ENDPOINTS.groupUpdateEndpoint(Config.domain)}`,
{
records: [payload],
Expand All @@ -95,22 +99,25 @@ const updateGroup = async (payload, Config) => {
},
{ destType: 'gainsight', feature: 'transformation' },
);
} catch (error) {
let errMessage = '';
let errorStatus = 500;
if (error.response && error.response.data) {
errMessage = error.response.data.errorDesc;
errorStatus = error.response.status;
}
throw new NetworkError(`failed to update group ${errMessage}`, errorStatus, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errorStatus),
});
}

if (!resp || !resp.data || resp.status !== 200) {
throw new RetryableError('failed to update group');
}
return resp.data.data.records[0].Gsid;
if (!isHttpStatusSuccess(processedResponseGs.status) && processedResponseGs >= 400 && processedResponseGs < 500) {
let errMessage = '';
let errorStatus = 500;
if (processedResponseGs.response) {
errMessage = processedResponseGs.response.errorDesc;
errorStatus = processedResponseGs.status;
}
throw new NetworkError(`failed to create group ${errMessage}`, errorStatus, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errorStatus),
}, processedResponseGs.response);
}

if (processedResponseGs.status !== 200) {
throw new RetryableError('failed to create group', processedResponseGs.status, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponseGs.status),
}, processedResponseGs.response);
}
return processedResponseGs.response?.data?.records[0].Gsid;
};

/**
Expand Down
Loading

0 comments on commit 8d7a136

Please sign in to comment.