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

fix(delighted): replace myAxios utility with handleHttpRequest utility #3237

Merged
merged 5 commits into from
Apr 22, 2024
Merged
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
107 changes: 56 additions & 51 deletions src/v0/destinations/delighted/util.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
const {
NetworkInstrumentationError,
InstrumentationError,
NetworkError,
} = require('@rudderstack/integrations-lib');
const myAxios = require('../../../util/myAxios');
const { InstrumentationError, NetworkError } = require('@rudderstack/integrations-lib');
const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils');
const { getValueFromMessage } = require('../../util');
const { ENDPOINT } = 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 Down Expand Up @@ -41,6 +37,30 @@
};
};

/**
* Returns final status
* @param {*} status
* @returns
*/
const getErrorStatus = (status) => {
let errStatus;
switch (status) {
case 422:
case 401:
case 406:
case 403:
errStatus = 400;
break;
case 500:
case 503:
errStatus = 500;
break;

Check warning on line 57 in src/v0/destinations/delighted/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/delighted/util.js#L48-L57

Added lines #L48 - L57 were not covered by tests
default:
errStatus = status;
}
return errStatus;
};

const userValidity = async (channel, Config, userId) => {
const paramsdata = {};
if (channel === 'email') {
Expand All @@ -50,53 +70,38 @@
}

const basicAuth = Buffer.from(Config.apiKey).toString('base64');
let response;
try {
response = await myAxios.get(
`${ENDPOINT}`,
{
headers: {
Authorization: `Basic ${basicAuth}`,
'Content-Type': JSON_MIME_TYPE,
},
params: paramsdata,
const { processedResponse } = await handleHttpRequest(
'get',
`${ENDPOINT}`,
{
headers: {
Authorization: `Basic ${basicAuth}`,
'Content-Type': JSON_MIME_TYPE,
},
{
destType: 'delighted',
feature: 'transformation',
requestMethod: 'GET',
endpointPath: '/people.json',
module: 'router',
},
);
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;
}
}
throw new NetworkError(`Error occurred while checking user : ${errMsg}`, errStatus, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errStatus),
});
params: paramsdata,
},
{
destType: 'delighted',
feature: 'transformation',
requestMethod: 'GET',
endpointPath: '/people.json',
module: 'router',
},
);

if (processedResponse.status === 200 && Array.isArray(processedResponse?.response)) {
return processedResponse.response.length > 0;
}

const errStatus = getErrorStatus(processedResponse.status);
throw new NetworkError(
`Error occurred while checking user: ${JSON.stringify(processedResponse?.response || 'Invalid response')}`,
errStatus,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errStatus),
},
processedResponse,
);
};
const eventValidity = (Config, message) => {
const event = getValueFromMessage(message, 'event');
Expand Down
14 changes: 14 additions & 0 deletions test/integrations/destinations/delighted/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,18 @@ export const networkCallsData = [
status: 200,
},
},
{
httpReq: {
url: 'https://api.delighted.com/v1/people.json',
method: 'GET',
headers: { Authorization: `Basic ZHVtbXlBcGlLZXlmb3JmYWlsdXJl` },
params: {
email: '[email protected]',
},
},
httpRes: {
status: 429,
data: {},
},
},
];
89 changes: 89 additions & 0 deletions test/integrations/destinations/delighted/processor/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -944,4 +944,93 @@ export const data = [
},
},
},
{
name: 'delighted',
description: 'Too many request test',
feature: 'processor',
module: 'destination',
version: 'v0',
input: {
request: {
body: [
{
destination: {
Config: {
apiKey: 'dummyApiKeyforfailure',
channel: 'email',
delay: 0,
eventNamesSettings: [
{
event: 'Product Reviewed',
},
],
},
},
message: {
channel: 'web',
context: {
app: {
build: '1.0.0',
name: 'RudderLabs JavaScript SDK',
namespace: 'com.rudderlabs.javascript',
version: '1.0.0',
},
library: {
name: 'RudderLabs JavaScript SDK',
version: '1.0.0',
},
userAgent:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
locale: 'en-US',
ip: '0.0.0.0',
os: {
name: '',
version: '',
},
screen: {
density: 2,
},
},
messageId: '84e26acc-56a5-4835-8233-591137fca468',
session_id: '3049dc4c-5a95-4ccd-a3e7-d74a7e411f22',
originalTimestamp: '2019-10-14T09:03:17.562Z',
type: 'track',
userId: '[email protected]',
event: 'Product Reviewed',
properties: {
review_id: '12345',
product_id: '123',
rating: 3,
review_body: 'Average product, expected much more.',
},
integrations: {
All: true,
},
sentAt: '2019-10-14T09:03:22.563Z',
},
},
],
},
},
output: {
response: {
status: 200,
body: [
{
error:
'{"message":"Error occurred while checking user: {}","destinationResponse":{"response":{},"status":429}}',
statTags: {
destType: 'DELIGHTED',
errorCategory: 'network',
errorType: 'throttled',
feature: 'processor',
implementation: 'native',
module: 'destination',
},
statusCode: 429,
},
],
},
},
},
];
Loading