2022-11-08 06:28:21 -08:00
|
|
|
import { get } from 'lodash';
|
2022-10-07 06:07:38 -07:00
|
|
|
|
2022-11-08 06:28:21 -08:00
|
|
|
import { parseString } from 'xml2js';
|
2022-10-07 06:07:38 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2022-11-08 06:28:21 -08:00
|
|
|
import { IDataObject, IHttpRequestOptions, JsonObject, NodeApiError } from 'n8n-workflow';
|
|
|
|
|
|
|
|
export async function awsApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
|
|
|
|
service: string,
|
|
|
|
method: string,
|
|
|
|
path: string,
|
|
|
|
body?: string | Buffer,
|
|
|
|
query: IDataObject = {},
|
|
|
|
headers?: object,
|
|
|
|
_option: IDataObject = {},
|
|
|
|
_region?: string,
|
|
|
|
) {
|
2022-10-07 06:07:38 -07:00
|
|
|
const credentials = await this.getCredentials('aws');
|
|
|
|
|
|
|
|
const requestOptions = {
|
|
|
|
qs: {
|
|
|
|
...query,
|
|
|
|
service,
|
|
|
|
path,
|
|
|
|
},
|
|
|
|
headers,
|
|
|
|
method,
|
|
|
|
url: '',
|
|
|
|
body,
|
|
|
|
region: credentials?.region as string,
|
|
|
|
} as IHttpRequestOptions;
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'aws', requestOptions);
|
|
|
|
} catch (error) {
|
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 06:28:21 -08: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,
|
|
|
|
) {
|
|
|
|
const response = await awsApiRequest.call(
|
|
|
|
this,
|
|
|
|
service,
|
|
|
|
method,
|
|
|
|
path,
|
|
|
|
body,
|
|
|
|
query,
|
|
|
|
headers,
|
|
|
|
options,
|
|
|
|
region,
|
|
|
|
);
|
2022-10-07 06:07:38 -07:00
|
|
|
try {
|
|
|
|
return JSON.parse(response);
|
|
|
|
} catch (e) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 06:28:21 -08: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,
|
|
|
|
) {
|
|
|
|
const response = await awsApiRequest.call(
|
|
|
|
this,
|
|
|
|
service,
|
|
|
|
method,
|
|
|
|
path,
|
|
|
|
body,
|
|
|
|
query,
|
|
|
|
headers,
|
|
|
|
option,
|
|
|
|
region,
|
|
|
|
);
|
2022-10-07 06:07:38 -07:00
|
|
|
try {
|
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
parseString(response, { explicitArray: false }, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
resolve(data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 06:28:21 -08: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,
|
|
|
|
) {
|
2022-10-07 06:07:38 -07:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
const propertyNameArray = propertyName.split('.');
|
|
|
|
|
|
|
|
do {
|
2022-11-08 06:28:21 -08:00
|
|
|
responseData = await awsApiRequestSOAP.call(
|
|
|
|
this,
|
|
|
|
service,
|
|
|
|
method,
|
|
|
|
path,
|
|
|
|
body,
|
|
|
|
query,
|
|
|
|
headers,
|
|
|
|
option,
|
|
|
|
region,
|
|
|
|
);
|
2022-10-07 06:07:38 -07:00
|
|
|
|
|
|
|
if (get(responseData, `${propertyNameArray[0]}.${propertyNameArray[1]}.NextMarker`)) {
|
2022-11-08 06:28:21 -08:00
|
|
|
query['Marker'] = get(
|
|
|
|
responseData,
|
|
|
|
`${propertyNameArray[0]}.${propertyNameArray[1]}.NextMarker`,
|
|
|
|
);
|
2022-10-07 06:07:38 -07:00
|
|
|
}
|
|
|
|
if (get(responseData, propertyName)) {
|
|
|
|
if (Array.isArray(get(responseData, propertyName))) {
|
|
|
|
returnData.push.apply(returnData, get(responseData, propertyName));
|
|
|
|
} else {
|
|
|
|
returnData.push(get(responseData, propertyName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (
|
|
|
|
get(responseData, `${propertyNameArray[0]}.${propertyNameArray[1]}.NextMarker`) !== undefined
|
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|