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

feat: do away myaxios #3222

Merged
merged 7 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
73 changes: 29 additions & 44 deletions src/v0/destinations/sfmc/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const {
isDefinedAndNotNull,
isEmpty,
} = require('@rudderstack/integrations-lib');
const myAxios = require('../../../util/myAxios');
const { EventType } = require('../../../constants');
const { handleHttpRequest } = require('../../../adapters/network');
const { CONFIG_CATEGORIES, MAPPING_CONFIG, ENDPOINTS } = require('./config');
const {
removeUndefinedAndNullValues,
Expand All @@ -22,10 +22,8 @@ const {
getHashFromArray,
simpleProcessRouterDest,
} = require('../../util');
const {
getDynamicErrorType,
nodeSysErrorToStatus,
} = require('../../../adapters/utils/networkUtils');
const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils');
const { isHttpStatusSuccess } = require('../../util');
const tags = require('../../util/tags');
const { JSON_MIME_TYPE } = require('../../util/constant');

Expand All @@ -34,51 +32,38 @@ const CONTACT_KEY_KEY = 'Contact Key';
// DOC: https://developer.salesforce.com/docs/atlas.en-us.mc-app-development.meta/mc-app-development/access-token-s2s.htm

const getToken = async (clientId, clientSecret, subdomain) => {
try {
const resp = await myAxios.post(
`https://${subdomain}.${ENDPOINTS.GET_TOKEN}`,
{
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
},
{
'Content-Type': JSON_MIME_TYPE,
},
{
destType: 'sfmc',
feature: 'transformation',
endpointPath: '/token',
requestMethod: 'POST',
module: 'router',
},
);
if (resp && resp.data) {
return resp.data.access_token;
}
const status = resp.status || 400;
const { processedResponse: processedResponseSfmc } = await handleHttpRequest(
'post',
`https://${subdomain}.${ENDPOINTS.GET_TOKEN}`,
{
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
},
{
'Content-Type': JSON_MIME_TYPE,
},
{
destType: 'sfmc',
feature: 'transformation',
endpointPath: '/token',
requestMethod: 'POST',
module: 'router',
},
);

if (!isHttpStatusSuccess(processedResponseSfmc.status)) {
throw new NetworkError(
'Could not retrieve access token',
status,
processedResponseSfmc.status || 400,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status),
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponseSfmc.status || 400),
},
resp,
JSON.stringify(processedResponseSfmc),
sanpj2292 marked this conversation as resolved.
Show resolved Hide resolved
);
} catch (error) {
if (!isEmpty(error.response)) {
const status = error.status || 400;
throw new NetworkError(`Authorization Failed ${error.response.statusText}`, status, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status),
});
} else {
const httpError = nodeSysErrorToStatus(error.code);
const status = httpError.status || 400;
throw new NetworkError(`Authorization Failed ${httpError.message}`, status, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status),
});
}
}

return processedResponseSfmc.response.access_token;
};

// DOC : https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-apis.meta/mc-apis/createContacts.htm
Expand Down
50 changes: 50 additions & 0 deletions test/integrations/destinations/sfmc/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,54 @@ export const networkCallsData = [
},
},
},
{
httpReq: {
url: 'https://testHandleHttpRequest401.auth.marketingcloudapis.com/v2/token',
method: 'POST',
},
httpRes: {
status: 401,
data: {
error: 'invalid_client',
error_description:
'Invalid client ID. Use the client ID in Marketing Cloud Installed Packages.',
error_uri: 'https://developer.salesforce.com/docs',
},
},
},
{
httpReq: {
url: 'https://testHandleHttpRequest429.auth.marketingcloudapis.com/v2/token',
method: 'POST',
},
httpRes: {
status: 429,
data: {
message: 'Your requests are temporarily blocked.',
errorcode: 50200,
documentation:
'https://developer.salesforce.com/docs/atlas.en-us.mc-apis.meta/mc-apis/error-handling.htm',
},
},
},
{
httpReq: {
url: 'https://testHandleHttpRequest-dns.auth.marketingcloudapis.com/v2/token',
method: 'POST',
},
httpRes: {
data: {},
status: 400,
},
},
{
httpReq: {
url: 'https://testHandleHttpRequest-null.auth.marketingcloudapis.com/v2/token',
method: 'POST',
},
httpRes: {
data: null,
status: 500,
},
},
];
Loading
Loading