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

n8n-3699 support for customer.io tracking api endpoint region selection #3378

Merged
merged 11 commits into from
Jul 10, 2022
37 changes: 37 additions & 0 deletions packages/nodes-base/credentials/CustomerIoApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
ICredentialDataDecryptedObject,
ICredentialType,
IHttpRequestOptions,
INodeProperties,
} from 'n8n-workflow';

Expand All @@ -17,6 +19,25 @@ export class CustomerIoApi implements ICredentialType {
description: 'Required for tracking API',
required: true,
},
{
displayName: 'Region',
name: 'region',
type: 'options',
options: [
{
name: 'EU region',
value: 'track-eu.customer.io',
},
{
name: 'Global region',
value: 'track.customer.io',
},
],
default: 'track.customer.io',
description: 'Should be set based on your account region',
hint: 'The region will be omited when being used with the HTTP node',
required: true,
},
{
displayName: 'Tracking Site ID',
name: 'trackingSiteId',
Expand All @@ -32,4 +53,20 @@ export class CustomerIoApi implements ICredentialType {
description: 'Required for App API',
},
];
async authenticate(credentials: ICredentialDataDecryptedObject, requestOptions: IHttpRequestOptions): Promise<IHttpRequestOptions> {
// @ts-ignore
const url = requestOptions.url ? requestOptions.url : requestOptions.uri;
if (url.includes('track') || url.includes('api.customer.io')) {
const basicAuthKey = Buffer.from(`${credentials.trackingSiteId}:${credentials.trackingApiKey}`).toString('base64');
// @ts-ignore
Object.assign(requestOptions.headers, { 'Authorization': `Basic ${basicAuthKey}` });
} else if (url.includes('beta-api.customer.io')) {
// @ts-ignore
Object.assign(requestOptions.headers, { 'Authorization': `Bearer ${credentials.appApiKey as string}` });
} else {
throw new Error('Unknown way of authenticating');
}

return requestOptions;
}
}
24 changes: 9 additions & 15 deletions packages/nodes-base/nodes/CustomerIo/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from 'request';

import {
IDataObject, NodeApiError, NodeOperationError,
IDataObject, IHttpRequestMethods, IHttpRequestOptions, NodeApiError, NodeOperationError,
} from 'n8n-workflow';

import {
Expand All @@ -18,34 +18,28 @@ import {

export async function customerIoApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: object, baseApi?: string, query?: IDataObject): Promise<any> { // tslint:disable-line:no-any
const credentials = await this.getCredentials('customerIoApi');

query = query || {};

const options: OptionsWithUri = {
const options: IHttpRequestOptions = {
headers: {
'Content-Type': 'application/json',
},
method,
method: method as IHttpRequestMethods,
body,
uri: '',
url: '',
json: true,
};

if (baseApi === 'tracking') {
options.uri = `https://track.customer.io/api/v1${endpoint}`;
const basicAuthKey = Buffer.from(`${credentials.trackingSiteId}:${credentials.trackingApiKey}`).toString('base64');
Object.assign(options.headers, { 'Authorization': `Basic ${basicAuthKey}` });
const region = credentials.region;
options.url = `https://${region}/api/v1${endpoint}`;
} else if (baseApi === 'api') {
options.uri = `https://api.customer.io/v1/api${endpoint}`;
const basicAuthKey = Buffer.from(`${credentials.trackingSiteId}:${credentials.trackingApiKey}`).toString('base64');
Object.assign(options.headers, { 'Authorization': `Basic ${basicAuthKey}` });
options.url = `https://api.customer.io/v1/api${endpoint}`;
} else if (baseApi === 'beta') {
options.uri = `https://beta-api.customer.io/v1/api${endpoint}`;
Object.assign(options.headers, { 'Authorization': `Bearer ${credentials.appApiKey as string}` });
options.url = `https://beta-api.customer.io/v1/api${endpoint}`;
}

try {
return await this.helpers.request!(options);
return await this.helpers.requestWithAuthentication.call(this, 'customerIoApi' ,options );
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
Expand Down