2022-08-01 13:47:55 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2021-03-11 04:36:55 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { IExecuteFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2021-03-11 04:36:55 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { IDataObject, IHookFunctions, IWebhookFunctions, NodeApiError } from 'n8n-workflow';
|
2021-03-11 04:36:55 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function autopilotApiRequest(
|
|
|
|
this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
|
|
|
const credentials = (await this.getCredentials('autopilotApi')) as IDataObject;
|
2021-03-11 04:36:55 -08:00
|
|
|
|
|
|
|
const apiKey = `${credentials.apiKey}`;
|
|
|
|
|
|
|
|
const endpoint = 'https://api2.autopilothq.com/v1';
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
autopilotapikey: apiKey,
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs: query,
|
|
|
|
uri: uri || `${endpoint}${resource}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
if (!Object.keys(body).length) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
if (!Object.keys(query).length) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2021-03-11 04:36:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function autopilotApiRequestAllItems(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
propertyName: string,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2021-03-11 04:36:55 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0, false) as boolean;
|
|
|
|
|
|
|
|
const base = endpoint;
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
do {
|
|
|
|
responseData = await autopilotApiRequest.call(this, method, endpoint, body, query);
|
|
|
|
endpoint = `${base}/${responseData.bookmark}`;
|
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
|
|
if (query.limit && returnData.length >= query.limit && returnAll === false) {
|
|
|
|
return returnData;
|
|
|
|
}
|
2022-08-01 13:47:55 -07:00
|
|
|
} while (responseData.bookmark !== undefined);
|
2021-03-11 04:36:55 -08:00
|
|
|
return returnData;
|
|
|
|
}
|