Skip to content

Commit

Permalink
feat: webhook v2 path variables support (#3705)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauravudia authored Sep 6, 2024
1 parent 1777c6c commit f7783d8
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 12 deletions.
16 changes: 13 additions & 3 deletions src/cdk/v2/destinations/webhook_v2/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ const { groupBy } = require('lodash');
const { createHash } = require('crypto');
const { ConfigurationError } = require('@rudderstack/integrations-lib');
const { BatchUtils } = require('@rudderstack/workflow-engine');
const { base64Convertor, applyCustomMappings, isEmptyObject } = require('../../../../v0/util');
const {
base64Convertor,
applyCustomMappings,
isEmptyObject,
applyJSONStringTemplate,
} = require('../../../../v0/util');

const getAuthHeaders = (config) => {
let headers;
Expand Down Expand Up @@ -36,8 +41,13 @@ const getCustomMappings = (message, mapping) => {
}
};

// TODO: write a func to evaluate json path template
const addPathParams = (message, webhookUrl) => webhookUrl;
const addPathParams = (message, webhookUrl) => {
try {
return applyJSONStringTemplate(message, `\`${webhookUrl}\``);
} catch (e) {
throw new ConfigurationError(`[Webhook]:: Error in url template: ${e.message}`);
}
};

const excludeMappedFields = (payload, mapping) => {
const rawPayload = { ...payload };
Expand Down
37 changes: 34 additions & 3 deletions test/integrations/destinations/webhook_v2/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const destinations: Destination[] = [
},
{
Config: {
webhookUrl: 'http://abc.com/contact/$traits.userId',
webhookUrl: 'http://abc.com/contacts',
auth: 'basicAuth',
username: 'test-user',
password: '',
Expand Down Expand Up @@ -92,7 +92,7 @@ const destinations: Destination[] = [
},
{
Config: {
webhookUrl: 'http://abc.com/contacts/$.traits.userId/',
webhookUrl: 'http://abc.com/contacts/{{$.traits.email}}/',
auth: 'apiKeyAuth',
apiKeyName: 'x-api-key',
apiKeyValue: 'test-api-key',
Expand All @@ -114,7 +114,7 @@ const destinations: Destination[] = [
},
{
Config: {
webhookUrl: 'http://abc.com/contacts/$.traits.userId/',
webhookUrl: 'http://abc.com/contacts/{{$.traits.email}}/',
auth: 'apiKeyAuth',
apiKeyName: 'x-api-key',
apiKeyValue: 'test-api-key',
Expand Down Expand Up @@ -247,6 +247,37 @@ const destinations: Destination[] = [
Transformations: [],
WorkspaceID: 'test-workspace-id',
},
{
Config: {
webhookUrl: 'http://abc.com/contacts/{{$.traits.phone}}',
auth: 'noAuth',
method: 'POST',
format: 'JSON',
isBatchingEnabled: true,
maxBatchSize: 4,
headers: [
{
to: "$.'content-type'",
from: "'application/json'",
},
{
to: '$.key',
from: '.traits.key',
},
],
},
DestinationDefinition: {
DisplayName: displayName,
ID: '123',
Name: destTypeInUpperCase,
Config: { cdkV2Enabled: true },
},
Enabled: true,
ID: '123',
Name: destTypeInUpperCase,
Transformations: [],
WorkspaceID: 'test-workspace-id',
},
];

const traits = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const configuration: ProcessorTestData[] = [
output: transformResultBuilder({
method: 'DELETE',
userId: '',
endpoint: 'http://abc.com/contacts/$.traits.userId/',
endpoint: 'http://abc.com/contacts/[email protected]/',
headers: {
'x-api-key': 'test-api-key',
},
Expand Down
146 changes: 141 additions & 5 deletions test/integrations/destinations/webhook_v2/router/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,48 @@ const routerRequest3 = {
destType,
};

// TODO: add failure testcases
const routerRequest4 = {
input: [
{
message: {
type: 'identify',
userId: 'userId1',
traits: { ...traits, key: 'value1' },
},
metadata: generateMetadata(1),
destination: destinations[6],
},
{
message: {
type: 'identify',
userId: 'userId1',
traits: { ...traits, key: 'value1' },
},
metadata: generateMetadata(2),
destination: destinations[6],
},
{
message: {
type: 'identify',
userId: 'userId1',
traits,
},
metadata: generateMetadata(3),
destination: destinations[6],
},
{
message: {
type: 'identify',
userId: 'userId1',
traits: { ...traits, phone: '2234567890' },
},
metadata: generateMetadata(4),
destination: destinations[6],
},
],
destType,
};

export const data = [
{
id: 'webhook_v2-router-test-1',
Expand All @@ -147,7 +188,7 @@ export const data = [
version: '1',
type: 'REST',
method: 'GET',
endpoint: 'http://abc.com/contacts/$.traits.userId/',
endpoint: 'http://abc.com/contacts/[email protected]/',
headers: {
'x-api-key': 'test-api-key',
},
Expand Down Expand Up @@ -196,7 +237,7 @@ export const data = [
version: '1',
type: 'REST',
method: 'GET',
endpoint: 'http://abc.com/contact/$traits.userId',
endpoint: 'http://abc.com/contacts',
headers: {
Authorization: 'Basic dGVzdC11c2VyOg==',
'content-type': 'application/json',
Expand Down Expand Up @@ -228,7 +269,7 @@ export const data = [
version: '1',
type: 'REST',
method: 'GET',
endpoint: 'http://abc.com/contact/$traits.userId',
endpoint: 'http://abc.com/contacts',
headers: {
Authorization: 'Basic dGVzdC11c2VyOg==',
'content-type': 'application/json',
Expand Down Expand Up @@ -260,7 +301,7 @@ export const data = [
version: '1',
type: 'REST',
method: 'GET',
endpoint: 'http://abc.com/contact/$traits.userId',
endpoint: 'http://abc.com/contacts',
headers: {
Authorization: 'Basic dGVzdC11c2VyOg==',
'content-type': 'application/json',
Expand Down Expand Up @@ -347,4 +388,99 @@ export const data = [
},
},
},
{
id: 'webhook_v2-router-test-4',
name: destType,
description: 'Batch multiple requests based on webhook url and headers',
scenario: 'Framework',
successCriteria: 'All events should be transformed successfully and status code should be 200',
feature: 'router',
module: 'destination',
version: 'v0',
input: {
request: {
body: routerRequest4,
method: 'POST',
},
},
output: {
response: {
status: 200,
body: {
output: [
{
batchedRequest: {
version: '1',
type: 'REST',
method: 'POST',
endpoint: 'http://abc.com/contacts/1234567890',
headers: {
'content-type': 'application/json',
key: 'value1',
},
params: {},
body: {
JSON: {},
JSON_ARRAY: { batch: '[]' },
XML: {},
FORM: {},
},
files: {},
},
metadata: [generateMetadata(1), generateMetadata(2)],
batched: true,
statusCode: 200,
destination: destinations[6],
},
{
batchedRequest: {
version: '1',
type: 'REST',
method: 'POST',
endpoint: 'http://abc.com/contacts/1234567890',
headers: {
'content-type': 'application/json',
},
params: {},
body: {
JSON: {},
JSON_ARRAY: { batch: '[]' },
XML: {},
FORM: {},
},
files: {},
},
metadata: [generateMetadata(3)],
batched: true,
statusCode: 200,
destination: destinations[6],
},
{
batchedRequest: {
version: '1',
type: 'REST',
method: 'POST',
endpoint: 'http://abc.com/contacts/2234567890',
headers: {
'content-type': 'application/json',
},
params: {},
body: {
JSON: {},
JSON_ARRAY: { batch: '[]' },
XML: {},
FORM: {},
},
files: {},
},
metadata: [generateMetadata(4)],
batched: true,
statusCode: 200,
destination: destinations[6],
},
],
},
},
},
},
];

0 comments on commit f7783d8

Please sign in to comment.