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

chore: move trengo,drip from myaxios to httpget #3454

Closed
wants to merge 5 commits into from
Closed
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
14 changes: 7 additions & 7 deletions src/v0/destinations/drip/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const {
} = require('./util');
const { JSON_MIME_TYPE } = require('../../util/constant');

const identifyResponseBuilder = async (message, { Config }) => {
const identifyResponseBuilder = async (message, { Config }, metadata) => {
const id = getDestinationExternalID(message, 'dripId');

let email = getFieldValueFromMessage(message, 'email');
Expand Down Expand Up @@ -117,7 +117,7 @@ const identifyResponseBuilder = async (message, { Config }) => {
response.method = defaultPostRequestConfig.requestMethod;
const campaignId = getDestinationExternalID(message, 'dripCampaignId') || Config.campaignId;
if (campaignId && email) {
const check = await createUpdateUser(finalpayload, Config, basicAuth);
const check = await createUpdateUser(finalpayload, Config, basicAuth, metadata);
if (!check) {
throw new NetworkInstrumentationError('Unable to create/update user.');
}
Expand All @@ -139,7 +139,7 @@ const identifyResponseBuilder = async (message, { Config }) => {
return response;
};

const trackResponseBuilder = async (message, { Config }) => {
const trackResponseBuilder = async (message, { Config }, metadata) => {
const id = getDestinationExternalID(message, 'dripId');

let email = getValueFromMessage(message, [
Expand All @@ -162,7 +162,7 @@ const trackResponseBuilder = async (message, { Config }) => {
event = event.trim().toLowerCase();

if (!Config.enableUserCreation && !id) {
const check = await userExists(Config, email);
const check = await userExists(Config, email, metadata);
if (!check) {
throw new NetworkInstrumentationError(
'User creation mode is disabled and user does not exist. Track call aborted.',
Expand Down Expand Up @@ -239,7 +239,7 @@ const trackResponseBuilder = async (message, { Config }) => {
};

const process = async (event) => {
const { message, destination } = event;
const { message, destination, metadata } = event;
if (!message.type) {
throw new InstrumentationError('Message Type is not present. Aborting message.');
}
Expand All @@ -255,10 +255,10 @@ const process = async (event) => {
let response;
switch (messageType) {
case EventType.IDENTIFY:
response = await identifyResponseBuilder(message, destination);
response = await identifyResponseBuilder(message, destination, metadata);
break;
case EventType.TRACK:
response = await trackResponseBuilder(message, destination);
response = await trackResponseBuilder(message, destination, metadata);
break;
default:
throw new InstrumentationError(`Message type ${messageType} not supported`);
Expand Down
24 changes: 16 additions & 8 deletions src/v0/destinations/drip/util.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const { NetworkError, AbortedError } = require('@rudderstack/integrations-lib');
const myAxios = require('../../../util/myAxios');
// const myAxios = require('../../../util/myAxios');
const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils');
const logger = require('../../../logger');
const { constructPayload, isDefinedAndNotNull } = 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 @@ -19,11 +20,11 @@
return re.test(String(timestamp));
};

const userExists = async (Config, id) => {
const userExists = async (Config, id, metadata) => {
const basicAuth = Buffer.from(Config.apiKey).toString('base64');
let response;
try {
response = await myAxios.get(
const { httpResponse } = await handleHttpRequest(
'get',
`${ENDPOINT}/v2/${Config.accountId}/subscribers/${id}`,
{
headers: {
Expand All @@ -32,13 +33,15 @@
},
},
{
metadata,
destType: 'drip',
feature: 'transformation',
requestMethod: 'GET',
endpointPath: '/subscribers/id',
module: 'router',
},
);
const { response } = httpResponse;
if (response && response.status) {
return response.status === 200;
}
Expand All @@ -50,7 +53,8 @@
},
response,
);
} catch (error) {
} catch ({ destinationResponse }) {
const error = destinationResponse;
let errMsg = '';
let errStatus = 400;
if (error.response) {
Expand All @@ -65,9 +69,10 @@
}
};

const createUpdateUser = async (finalpayload, Config, basicAuth) => {
const createUpdateUser = async (finalpayload, Config, basicAuth, metadata) => {
try {
const response = await myAxios.post(
const { httpResponse } = await handleHttpRequest(
'post',
`${ENDPOINT}/v2/${Config.accountId}/subscribers`,
finalpayload,
{
Expand All @@ -77,18 +82,21 @@
},
},
{
metadata,
destType: 'drip',
feature: 'transformation',
requestMethod: 'POST',
endpointPath: '/subscribers',
module: 'router',
},
);
const { response } = httpResponse;
if (response) {
return response.status === 200 || response.status === 201;
}
throw new AbortedError('Invalid response.');
} catch (error) {
} catch ({ destinationResponse }) {
const error = destinationResponse;

Check warning on line 99 in src/v0/destinations/drip/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/drip/util.js#L99

Added line #L99 was not covered by tests
let errMsg = '';
if (error.response && error.response.data) {
errMsg = JSON.stringify(error.response.data);
Expand Down
27 changes: 17 additions & 10 deletions src/v0/destinations/trengo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const {
InstrumentationError,
NetworkInstrumentationError,
} = require('@rudderstack/integrations-lib');
const myAxios = require('../../../util/myAxios');
const { EventType } = require('../../../constants');
const { EndPoints, BASE_URL } = require('./config');
const {
Expand All @@ -27,6 +26,7 @@ const {
const tags = require('../../util/tags');
const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils');
const { JSON_MIME_TYPE } = require('../../util/constant');
const { httpGET } = require('../../../adapters/network');

/**
*
Expand Down Expand Up @@ -80,24 +80,26 @@ const validate = (email, phone, channelIdentifier) => {
*
* In case no contact is founf for a particular identifer it returns -1
*/
const lookupContact = async (term, destination) => {
const lookupContact = async (term, destination, metadata) => {
let res;
try {
res = await myAxios.get(
res = await httpGET(
`${BASE_URL}/contacts?page=1&term=${term}`,
{
headers: {
Authorization: `Bearer ${destination.Config.apiToken}`,
},
},
{
metadata,
destType: 'trengo',
feature: 'transformation',
endpointPath: '/contacts',
requestMethod: 'GET',
module: 'router',
},
);
res = res.response;
} catch (err) {
// check if exists err.response && err.response.status else 500
const status = err.response?.status || 400;
Expand Down Expand Up @@ -141,6 +143,7 @@ const contactBuilderTrengo = async (
destination,
identifier,
extIds,
metadata,
createScope = true,
) => {
let result;
Expand Down Expand Up @@ -175,7 +178,7 @@ const contactBuilderTrengo = async (
let contactId = get(extIds, 'contactId');
if (!contactId) {
// If we alrady dont have contactId in our message we do lookup
contactId = await lookupContact(identifier, destination);
contactId = await lookupContact(identifier, destination, metadata);
if (!contactId) {
// In case contactId is returned null we throw error (This indicates and search API issue in trengo end)
throw new NetworkInstrumentationError(
Expand All @@ -202,13 +205,13 @@ const contactBuilderTrengo = async (
return result;
};

const ticketBuilderTrengo = async (message, destination, identifer, extIds) => {
const ticketBuilderTrengo = async (message, destination, identifer, extIds, metadata) => {
let subjectLine;
const template = getTemplate(message, destination);
const externalId = get(extIds, 'externalId');
let contactId = get(extIds, 'contactId');
if (!contactId) {
contactId = await lookupContact(identifer, destination);
contactId = await lookupContact(identifer, destination, metadata);
if (!contactId) {
throw new InstrumentationError(
`LookupContact failed for term:${identifer} track event failed`,
Expand Down Expand Up @@ -263,7 +266,7 @@ const ticketBuilderTrengo = async (message, destination, identifer, extIds) => {
* based on type of event and the destination configurations the
* payloads are generated.
*/
const responseBuilderSimple = async (message, messageType, destination) => {
const responseBuilderSimple = async (message, messageType, destination, metadata) => {
let trengoPayload;
// ChannelId is a mandatory field if it is not present in destination config
// we will abort events.
Expand Down Expand Up @@ -293,6 +296,7 @@ const responseBuilderSimple = async (message, messageType, destination) => {
destination,
channelIdentifier === 'email' ? email : phone,
extIds,
metadata,
false,
);
if (trengoPayload === -1) {
Expand All @@ -306,6 +310,7 @@ const responseBuilderSimple = async (message, messageType, destination) => {
destination,
channelIdentifier === 'email' ? email : phone,
extIds,
metadata,
true,
);
}
Expand All @@ -320,6 +325,7 @@ const responseBuilderSimple = async (message, messageType, destination) => {
destination,
channelIdentifier === 'email' ? email : phone,
extIds,
metadata,
true,
);
}
Expand All @@ -331,6 +337,7 @@ const responseBuilderSimple = async (message, messageType, destination) => {
destination,
channelIdentifier === 'email' ? email : phone,
extIds,
metadata,
);
}
// Wrapped payload with structure
Expand Down Expand Up @@ -370,20 +377,20 @@ const responseBuilderSimple = async (message, messageType, destination) => {
* If event type is not identify or track it will discard
* the event
*/
const processEvent = async (message, destination) => {
const processEvent = async (message, destination, metadata) => {
if (!message.type) {
throw new InstrumentationError('Event type is required');
}
const messageType = message.type.toLowerCase();
if (messageType !== EventType.IDENTIFY && messageType !== EventType.TRACK) {
throw new InstrumentationError(`Event type ${messageType} is not supported`);
}
const resp = await responseBuilderSimple(message, messageType, destination);
const resp = await responseBuilderSimple(message, messageType, destination, metadata);
return resp;
};

const process = async (event) => {
const response = await processEvent(event.message, event.destination);
const response = await processEvent(event.message, event.destination, event.metadata);
return response;
};

Expand Down
Loading