2023-06-16 07:26:35 -07:00
|
|
|
import get from 'lodash/get';
|
2020-04-15 15:42:37 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { parseString } from 'xml2js';
|
2020-04-15 15:42:37 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IDataObject,
|
2020-04-15 15:42:37 -07:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
2023-03-09 09:13:15 -08:00
|
|
|
IHttpRequestOptions,
|
|
|
|
} from 'n8n-workflow';
|
2022-08-01 13:47:55 -07:00
|
|
|
|
|
|
|
export async function awsApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
|
|
|
|
service: string,
|
|
|
|
method: string,
|
|
|
|
path: string,
|
|
|
|
body?: string | Buffer,
|
|
|
|
query: IDataObject = {},
|
|
|
|
headers?: object,
|
|
|
|
option: IDataObject = {},
|
2022-11-08 06:28:21 -08:00
|
|
|
_region?: string,
|
2022-08-01 13:47:55 -07:00
|
|
|
): Promise<any> {
|
2022-08-23 10:02:32 -07:00
|
|
|
const requestOptions = {
|
|
|
|
qs: {
|
2022-08-29 06:28:17 -07:00
|
|
|
...query,
|
2022-08-23 10:02:32 -07:00
|
|
|
service,
|
|
|
|
path,
|
|
|
|
query,
|
|
|
|
},
|
2020-04-15 15:42:37 -07:00
|
|
|
method,
|
2022-09-06 05:33:52 -07:00
|
|
|
body,
|
2022-08-23 10:02:32 -07:00
|
|
|
url: '',
|
|
|
|
headers,
|
|
|
|
//region: credentials?.region as string,
|
|
|
|
} as IHttpRequestOptions;
|
2020-04-20 17:33:38 -07:00
|
|
|
|
2020-04-15 15:42:37 -07:00
|
|
|
if (Object.keys(option).length !== 0) {
|
2022-08-23 10:02:32 -07:00
|
|
|
Object.assign(requestOptions, option);
|
2020-04-15 15:42:37 -07:00
|
|
|
}
|
2022-11-22 08:57:17 -08:00
|
|
|
return this.helpers.requestWithAuthentication.call(this, 'aws', requestOptions);
|
2020-04-15 15:42:37 -07:00
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function awsApiRequestREST(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
service: string,
|
|
|
|
method: string,
|
|
|
|
path: string,
|
|
|
|
body?: string,
|
|
|
|
query: IDataObject = {},
|
|
|
|
headers?: object,
|
|
|
|
options: IDataObject = {},
|
|
|
|
region?: string,
|
|
|
|
): Promise<any> {
|
|
|
|
const response = await awsApiRequest.call(
|
|
|
|
this,
|
|
|
|
service,
|
|
|
|
method,
|
|
|
|
path,
|
|
|
|
body,
|
|
|
|
query,
|
|
|
|
headers,
|
|
|
|
options,
|
|
|
|
region,
|
|
|
|
);
|
2020-04-15 15:42:37 -07:00
|
|
|
try {
|
2023-02-27 19:39:43 -08:00
|
|
|
return JSON.parse(response as string);
|
2021-04-16 09:33:36 -07:00
|
|
|
} catch (error) {
|
2020-04-15 15:42:37 -07:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function awsApiRequestSOAP(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
|
|
|
|
service: string,
|
|
|
|
method: string,
|
|
|
|
path: string,
|
|
|
|
body?: string | Buffer,
|
|
|
|
query: IDataObject = {},
|
|
|
|
headers?: object,
|
|
|
|
option: IDataObject = {},
|
|
|
|
region?: string,
|
|
|
|
): Promise<any> {
|
|
|
|
const response = await awsApiRequest.call(
|
|
|
|
this,
|
|
|
|
service,
|
|
|
|
method,
|
|
|
|
path,
|
|
|
|
body,
|
|
|
|
query,
|
|
|
|
headers,
|
|
|
|
option,
|
|
|
|
region,
|
|
|
|
);
|
2020-04-15 15:42:37 -07:00
|
|
|
try {
|
|
|
|
return await new Promise((resolve, reject) => {
|
2023-02-27 19:39:43 -08:00
|
|
|
parseString(response as string, { explicitArray: false }, (err, data) => {
|
2020-04-15 15:42:37 -07:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
resolve(data);
|
|
|
|
});
|
|
|
|
});
|
2021-04-16 09:33:36 -07:00
|
|
|
} catch (error) {
|
|
|
|
return error;
|
2020-04-15 15:42:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function awsApiRequestSOAPAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
|
|
|
|
propertyName: string,
|
|
|
|
service: string,
|
|
|
|
method: string,
|
|
|
|
path: string,
|
|
|
|
body?: string,
|
|
|
|
query: IDataObject = {},
|
|
|
|
headers: IDataObject = {},
|
|
|
|
option: IDataObject = {},
|
|
|
|
region?: string,
|
|
|
|
): Promise<any> {
|
2020-04-15 15:42:37 -07:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
do {
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await awsApiRequestSOAP.call(
|
|
|
|
this,
|
|
|
|
service,
|
|
|
|
method,
|
|
|
|
path,
|
|
|
|
body,
|
|
|
|
query,
|
|
|
|
headers,
|
|
|
|
option,
|
|
|
|
region,
|
|
|
|
);
|
2020-04-15 15:42:37 -07:00
|
|
|
|
|
|
|
//https://forums.aws.amazon.com/thread.jspa?threadID=55746
|
|
|
|
if (get(responseData, `${propertyName.split('.')[0]}.NextContinuationToken`)) {
|
2022-08-01 13:47:55 -07:00
|
|
|
query['continuation-token'] = get(
|
|
|
|
responseData,
|
|
|
|
`${propertyName.split('.')[0]}.NextContinuationToken`,
|
|
|
|
);
|
2020-04-15 15:42:37 -07:00
|
|
|
}
|
|
|
|
if (get(responseData, propertyName)) {
|
|
|
|
if (Array.isArray(get(responseData, propertyName))) {
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, get(responseData, propertyName) as IDataObject[]);
|
2020-04-15 15:42:37 -07:00
|
|
|
} else {
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push(get(responseData, propertyName) as IDataObject);
|
2020-04-15 15:42:37 -07:00
|
|
|
}
|
|
|
|
}
|
2023-03-30 04:59:59 -07:00
|
|
|
const limit = query.limit as number | undefined;
|
|
|
|
if (limit && limit <= returnData.length) {
|
2020-04-15 15:42:37 -07:00
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
} while (
|
|
|
|
get(responseData, `${propertyName.split('.')[0]}.IsTruncated`) !== undefined &&
|
|
|
|
get(responseData, `${propertyName.split('.')[0]}.IsTruncated`) !== 'false'
|
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|