2021-04-16 09:33:36 -07:00
|
|
|
import { parseString as parseXml } from 'xml2js';
|
2019-10-16 02:18:39 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2019-10-05 06:27:19 -07:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
2020-02-08 14:19:00 -08:00
|
|
|
IWebhookFunctions,
|
2019-10-05 06:27:19 -07:00
|
|
|
} from 'n8n-core';
|
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
import type { IHttpRequestOptions, JsonObject } from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2019-10-05 13:17:23 -07:00
|
|
|
|
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,
|
|
|
|
headers?: object,
|
|
|
|
): Promise<any> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('aws');
|
2022-08-23 10:02:32 -07:00
|
|
|
const requestOptions = {
|
|
|
|
qs: {
|
|
|
|
service,
|
|
|
|
path,
|
|
|
|
},
|
2019-10-16 02:18:39 -07:00
|
|
|
method,
|
2022-09-06 01:01:48 -07:00
|
|
|
body: service === 'lambda' ? body : JSON.stringify(body),
|
2022-08-23 10:02:32 -07:00
|
|
|
url: '',
|
|
|
|
headers,
|
|
|
|
region: credentials?.region as string,
|
|
|
|
} as IHttpRequestOptions;
|
2020-04-20 11:44:22 -07:00
|
|
|
|
2019-10-05 13:17:23 -07:00
|
|
|
try {
|
2022-09-06 01:01:48 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'aws', requestOptions);
|
2019-10-05 13:17:23 -07:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject, { parseXml: true });
|
2019-10-05 13:17:23 -07:00
|
|
|
}
|
2019-10-15 10:21:48 -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,
|
|
|
|
headers?: object,
|
|
|
|
): Promise<any> {
|
2019-10-15 10:21:48 -07:00
|
|
|
const response = await awsApiRequest.call(this, service, method, path, body, headers);
|
2019-10-05 13:17:23 -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) {
|
2019-10-16 02:18:39 -07:00
|
|
|
return response;
|
2019-10-05 13:17:23 -07:00
|
|
|
}
|
|
|
|
}
|
2019-10-15 10:21:48 -07:00
|
|
|
|
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,
|
|
|
|
headers?: object,
|
|
|
|
): Promise<any> {
|
2019-10-15 10:21:48 -07: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
|
|
|
parseXml(response as string, { explicitArray: false }, (err, data) => {
|
2019-10-15 10:21:48 -07:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
resolve(data);
|
|
|
|
});
|
|
|
|
});
|
2021-04-16 09:33:36 -07:00
|
|
|
} catch (error) {
|
2019-10-16 02:18:39 -07:00
|
|
|
return response;
|
2019-10-15 10:21:48 -07:00
|
|
|
}
|
|
|
|
}
|