2022-08-01 13:47:55 -07:00
|
|
|
import { parseString } from 'xml2js';
|
2021-02-07 14:42:59 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2021-02-07 14:42:59 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
2023-03-09 09:13:15 -08:00
|
|
|
IHttpRequestOptions,
|
2024-02-14 07:29:09 -08:00
|
|
|
IHttpRequestMethods,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2021-02-07 14:42:59 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function awsApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
|
|
|
|
service: string,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-01 13:47:55 -07:00
|
|
|
path: string,
|
|
|
|
body?: string,
|
|
|
|
headers?: object,
|
|
|
|
): Promise<any> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('aws');
|
2021-02-07 14:42:59 -08:00
|
|
|
|
2022-08-23 10:02:32 -07:00
|
|
|
const requestOptions = {
|
|
|
|
qs: {
|
|
|
|
service,
|
|
|
|
path,
|
|
|
|
},
|
2021-02-07 14:42:59 -08:00
|
|
|
method,
|
2022-08-23 10:02:32 -07:00
|
|
|
body,
|
|
|
|
url: '',
|
|
|
|
headers,
|
|
|
|
region: credentials?.region as string,
|
|
|
|
} as IHttpRequestOptions;
|
2024-01-17 07:08:50 -08:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'aws', requestOptions);
|
2021-02-07 14:42:59 -08:00
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function awsApiRequestREST(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
service: string,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-01 13:47:55 -07:00
|
|
|
path: string,
|
|
|
|
body?: string,
|
|
|
|
headers?: object,
|
|
|
|
): Promise<any> {
|
2021-02-07 14:42:59 -08:00
|
|
|
const response = await awsApiRequest.call(this, service, method, path, body, headers);
|
|
|
|
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) {
|
2021-02-07 14:42:59 -08:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function awsApiRequestSOAP(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
|
|
|
|
service: string,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-01 13:47:55 -07:00
|
|
|
path: string,
|
|
|
|
body?: string,
|
|
|
|
headers?: object,
|
|
|
|
): Promise<any> {
|
2021-02-07 14:42:59 -08:00
|
|
|
const response = await awsApiRequest.call(this, service, method, path, body, headers);
|
|
|
|
try {
|
|
|
|
return await new Promise((resolve, reject) => {
|
2023-02-27 19:39:43 -08:00
|
|
|
parseString(response as string, { explicitArray: false }, (err, data) => {
|
2021-02-07 14:42:59 -08:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
resolve(data);
|
|
|
|
});
|
|
|
|
});
|
2021-04-16 09:33:36 -07:00
|
|
|
} catch (error) {
|
2021-02-07 14:42:59 -08:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|