Skip to content

Commit

Permalink
chore: improve iterable network handler (#3918)
Browse files Browse the repository at this point in the history
* chore: improve iterable network handler

* chore: add comment in principal strategy class

* chore: rename from PrincipalStrategy to BaseStrategy

* chore: update expect-error comment

---------

Co-authored-by: Sai Sankeerth <[email protected]>
  • Loading branch information
sanpj2292 and Sai Sankeerth authored Dec 7, 2024
1 parent 97d531f commit 3789652
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 90 deletions.
12 changes: 6 additions & 6 deletions src/v1/destinations/iterable/networkHandler.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { prepareProxyRequest, proxyRequest } from '../../../adapters/network';
import { processAxiosResponse } from '../../../adapters/utils/networkUtils';
import { BULK_ENDPOINTS } from '../../../v0/destinations/iterable/config';
import { CommonStrategy } from './commonStrategy';
import { TrackIdentifyStrategy } from './trackIdentifyStrategy';
import { GenericStrategy } from './strategies/generic';
import { TrackIdentifyStrategy } from './strategies/track-identify';

interface ResponseParams {
type ResponseParams = {
destinationRequest: {
endpoint: string;
};
}
};

const strategyRegistry: { [key: string]: any } = {
[TrackIdentifyStrategy.name]: new TrackIdentifyStrategy(),
[CommonStrategy.name]: new CommonStrategy(),
[GenericStrategy.name]: new GenericStrategy(),
};

const getResponseStrategy = (endpoint: string) => {
if (BULK_ENDPOINTS.some((path) => endpoint.includes(path))) {
return strategyRegistry[TrackIdentifyStrategy.name];
}
return strategyRegistry[CommonStrategy.name];
return strategyRegistry[GenericStrategy.name];
};

const responseHandler = (responseParams: ResponseParams) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { CommonResponse } from './type';

const { isHttpStatusSuccess } = require('../../../v0/util/index');
const { TransformerProxyError } = require('../../../v0/util/errorTypes');
const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils');
const tags = require('../../../v0/util/tags');

class ResponseStrategy {
handleResponse(responseParams: {
destinationResponse: { status: number; response: CommonResponse };
}): void {
import { TAG_NAMES } from '@rudderstack/integrations-lib';
import { getDynamicErrorType } from '../../../../adapters/utils/networkUtils';
import { isHttpStatusSuccess } from '../../../../v0/util';
import { TransformerProxyError } from '../../../../v0/util/errorTypes';
import { DestinationResponse, ResponseParams } from '../types';

// Base strategy is the base class for all strategies in Iterable destination
class BaseStrategy {
handleResponse(responseParams: { destinationResponse: DestinationResponse }): void {
const { destinationResponse } = responseParams;
const { status } = destinationResponse;

Expand All @@ -22,14 +20,12 @@ class ResponseStrategy {
return this.handleSuccess(responseParams);
}

handleError(responseParams): void {
handleError(responseParams: ResponseParams): void {
const { destinationResponse, rudderJobMetadata } = responseParams;
const { response, status } = destinationResponse;
const errorMessage =
JSON.stringify(response.params) ||
JSON.stringify(response.msg) ||
JSON.stringify(response.message) ||
'unknown error format';
// @ts-expect-error: not sure if `response.message` is correct or needed
const responseMessage = response.params || response.msg || response.message;
const errorMessage = JSON.stringify(responseMessage) || 'unknown error format';

const responseWithIndividualEvents = rudderJobMetadata.map((metadata) => ({
statusCode: status,
Expand All @@ -40,7 +36,7 @@ class ResponseStrategy {
throw new TransformerProxyError(
`ITERABLE: Error transformer proxy during ITERABLE response transformation. ${errorMessage}`,
status,
{ [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status) },
{ [TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status) },
destinationResponse,
'',
responseWithIndividualEvents,
Expand All @@ -52,4 +48,4 @@ class ResponseStrategy {
}
}

export { ResponseStrategy };
export { BaseStrategy };
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { CommonResponse } from './type';
import { BaseStrategy } from './base';
import { DestinationResponse, SuccessResponse } from '../types';

const { ResponseStrategy } = require('./responseStrategy');

class CommonStrategy extends ResponseStrategy {
class GenericStrategy extends BaseStrategy {
handleSuccess(responseParams: {
destinationResponse: { status: number; response: CommonResponse };
destinationResponse: DestinationResponse;
rudderJobMetadata: any[];
}): {
status: number;
message: string;
destinationResponse: { status: number; response: CommonResponse };
response: { statusCode: number; metadata: any; error: string }[];
} {
}): SuccessResponse {
const { destinationResponse, rudderJobMetadata } = responseParams;
const { status } = destinationResponse;

Expand All @@ -30,4 +24,4 @@ class CommonStrategy extends ResponseStrategy {
}
}

export { CommonStrategy };
export { GenericStrategy };
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
import { ResponseStrategy } from './responseStrategy';
import { checkIfEventIsAbortableAndExtractErrorMessage } from '../../../v0/destinations/iterable/util';
import { CommonResponse } from './type';
import { BaseStrategy } from './base';
import { DestinationResponse, ResponseParams, Response } from '../types';
import { checkIfEventIsAbortableAndExtractErrorMessage } from '../../../../v0/destinations/iterable/util';

interface ResponseParams {
destinationResponse: { status: number; response: CommonResponse };
rudderJobMetadata: any[];
destinationRequest: {
body: {
JSON: {
events?: any[];
users?: any[];
};
};
};
}

class TrackIdentifyStrategy extends ResponseStrategy {
class TrackIdentifyStrategy extends BaseStrategy {
handleSuccess(responseParams: ResponseParams): {
status: number;
message: string;
destinationResponse: { status: number; response: CommonResponse };
response: Array<{ statusCode: number; metadata: any; error: string }>;
destinationResponse: DestinationResponse;
response: Response[];
} {
const { destinationResponse, rudderJobMetadata, destinationRequest } = responseParams;
const { status } = destinationResponse;
const responseWithIndividualEvents: Array<{
statusCode: number;
metadata: any;
error: string;
}> = [];
const responseWithIndividualEvents: Response[] = [];

const { events, users } = destinationRequest.body.JSON;
const { events, users } = destinationRequest?.body.JSON || {};
const finalData = events || users;

if (finalData) {
Expand Down
26 changes: 0 additions & 26 deletions src/v1/destinations/iterable/type.ts

This file was deleted.

57 changes: 57 additions & 0 deletions src/v1/destinations/iterable/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
type FailedUpdates = {
invalidEmails?: string[];
invalidUserIds?: string[];
notFoundEmails?: string[];
notFoundUserIds?: string[];
invalidDataEmails?: string[];
invalidDataUserIds?: string[];
conflictEmails?: string[];
conflictUserIds?: string[];
forgottenEmails?: string[];
forgottenUserIds?: string[];
};

export type GeneralApiResponse = {
msg?: string;
code?: string;
params?: Record<string, unknown>;
successCount?: number;
failCount?: number;
invalidEmails?: string[];
invalidUserIds?: string[];
filteredOutFields?: string[];
createdFields?: string[];
disallowedEventNames?: string[];
failedUpdates?: FailedUpdates;
};

export type DestinationResponse = {
status: number;
response: GeneralApiResponse;
};

export type ResponseParams = {
destinationResponse: DestinationResponse;
rudderJobMetadata: any[];
destinationRequest?: {
body: {
JSON: {
events?: any[];
users?: any[];
};
};
};
};

export type Response = {
statusCode: number;
metadata: any;
error: string;
};

export type SuccessResponse = {
status: number;
message: string;
destinationResponse: DestinationResponse;
response: Response[];
};
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ export const testScenariosForV1API: ProxyV1TestData[] = [
status: 400,
statTags,
message:
'ITERABLE: Error transformer proxy during ITERABLE response transformation. null',
'ITERABLE: Error transformer proxy during ITERABLE response transformation. "Invalid currentEmail sayan"',
response: [
{
statusCode: 400,
Expand Down Expand Up @@ -596,7 +596,7 @@ export const testScenariosForV1API: ProxyV1TestData[] = [
status: 400,
statTags,
message:
'ITERABLE: Error transformer proxy during ITERABLE response transformation. null',
'ITERABLE: Error transformer proxy during ITERABLE response transformation. "Invalid email: sayan"',
response: [
{
statusCode: 400,
Expand Down

0 comments on commit 3789652

Please sign in to comment.