🔀 Merge branch 'RicardoE105-feature/extended-mailchimp'

This commit is contained in:
Jan Oberhauser 2020-05-22 21:45:16 +02:00
commit 4241cd2ed6
2 changed files with 1128 additions and 115 deletions

View file

@ -1,13 +1,19 @@
import { OptionsWithUri } from 'request';
import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IExecuteSingleFunctions
} from 'n8n-core';
export async function mailchimpApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, endpoint: string, method: string, body: any = {}, headers?: object): Promise<any> { // tslint:disable-line:no-any
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
const credentials = this.getCredentials('mailchimpApi');
if (credentials === undefined) {
@ -27,6 +33,7 @@ export async function mailchimpApiRequest(this: IHookFunctions | IExecuteFunctio
const options: OptionsWithUri = {
headers: headerWithAuthentication,
method,
qs,
uri: `https://${datacenter}.${host}${endpoint}`,
json: true,
};
@ -34,19 +41,36 @@ export async function mailchimpApiRequest(this: IHookFunctions | IExecuteFunctio
if (Object.keys(body).length !== 0) {
options.body = body;
}
try {
return await this.helpers.request!(options);
} catch (error) {
const errorMessage = error.response.body.message || error.response.body.Message;
if (errorMessage !== undefined) {
throw errorMessage;
if (error.response.body && error.response.body.detail) {
throw new Error(`Mailchimp Error response [${error.statusCode}]: ${error.response.body.detail}`);
}
throw error.response.body;
throw error;
}
}
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;
}
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
let result;
try {

File diff suppressed because it is too large Load diff