2020-05-22 06:47:47 -07:00
|
|
|
import {
|
2020-06-04 06:54:39 -07:00
|
|
|
OptionsWithUrl,
|
2020-05-22 06:47:47 -07:00
|
|
|
} from 'request';
|
2019-11-12 12:48:45 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
2020-05-22 06:47:47 -07:00
|
|
|
IExecuteSingleFunctions,
|
2019-11-12 12:48:45 -08:00
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2020-05-22 06:47:47 -07:00
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
export async function mailchimpApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, endpoint: string, method: string, body: any = {}, qs: IDataObject = {} ,headers?: object): Promise<any> { // tslint:disable-line:no-any
|
2020-06-04 06:54:39 -07:00
|
|
|
const authenticationMethod = this.getNodeParameter('authentication', 0) as string;
|
2019-11-15 12:44:46 -08:00
|
|
|
|
|
|
|
const host = 'api.mailchimp.com/3.0';
|
2019-11-12 12:48:45 -08:00
|
|
|
|
2020-06-04 06:54:39 -07:00
|
|
|
const options: OptionsWithUrl = {
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json'
|
|
|
|
},
|
2019-11-12 12:48:45 -08:00
|
|
|
method,
|
2020-05-22 06:47:47 -07:00
|
|
|
qs,
|
2020-06-04 06:54:39 -07:00
|
|
|
body,
|
|
|
|
url: ``,
|
2019-11-15 12:44:46 -08:00
|
|
|
json: true,
|
2019-11-12 12:48:45 -08:00
|
|
|
};
|
|
|
|
|
2020-06-04 06:54:39 -07:00
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
2019-11-12 12:48:45 -08:00
|
|
|
}
|
2020-06-04 06:54:39 -07:00
|
|
|
|
2019-11-12 12:48:45 -08:00
|
|
|
try {
|
2020-06-04 06:54:39 -07:00
|
|
|
if (authenticationMethod === 'accessToken') {
|
|
|
|
const credentials = this.getCredentials('mailchimpApi');
|
|
|
|
|
|
|
|
if (credentials === undefined) {
|
|
|
|
throw new Error('No credentials got returned!');
|
|
|
|
}
|
|
|
|
|
|
|
|
options.headers = Object.assign({}, headers, { Authorization: `apikey ${credentials.apiKey}` });
|
|
|
|
|
|
|
|
if (!(credentials.apiKey as string).includes('-')) {
|
|
|
|
throw new Error('The API key is not valid!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const datacenter = (credentials.apiKey as string).split('-').pop();
|
|
|
|
options.url = `https://${datacenter}.${host}${endpoint}`;
|
|
|
|
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} else {
|
|
|
|
const credentials = this.getCredentials('mailchimpOAuth2Api');
|
|
|
|
const datacenter = credentials!.dataCenter;
|
|
|
|
|
|
|
|
options.url = `https://${datacenter}.${host}${endpoint}`;
|
|
|
|
//@ts-ignore
|
|
|
|
return await this.helpers.requestOAuth2!.call(this, 'mailchimpOAuth2Api', options, 'bearer');
|
|
|
|
}
|
2019-11-12 12:48:45 -08:00
|
|
|
} catch (error) {
|
2020-05-22 06:47:47 -07:00
|
|
|
if (error.response.body && error.response.body.detail) {
|
2020-05-22 12:44:39 -07:00
|
|
|
throw new Error(`Mailchimp Error response [${error.statusCode}]: ${error.response.body.detail}`);
|
2019-11-12 12:48:45 -08:00
|
|
|
}
|
2020-05-22 12:44:39 -07:00
|
|
|
throw error;
|
2019-11-12 12:48:45 -08:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 15:44:07 -08:00
|
|
|
|
2020-05-22 06:47:47 -07:00
|
|
|
export async function mailchimpApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, endpoint: string, method: string, propertyName: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
query.offset = 0;
|
|
|
|
query.count = 500;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await mailchimpApiRequest.call(this, endpoint, method, body, query);
|
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
|
|
query.offset += query.count;
|
|
|
|
} while (
|
|
|
|
responseData[propertyName] && responseData[propertyName].length !== 0
|
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2019-11-14 15:44:07 -08:00
|
|
|
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(json!);
|
|
|
|
} catch (exception) {
|
|
|
|
result = '';
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|