2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-06-11 15:57:32 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions, IHookFunctions } from 'n8n-core';
|
2020-06-11 17:40:58 -07:00
|
|
|
|
2022-11-08 06:28:21 -08:00
|
|
|
import { IDataObject, NodeApiError } from 'n8n-workflow';
|
2020-06-11 15:57:32 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to Message Bird
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export async function messageBirdApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
body: IDataObject,
|
2020-10-22 09:00:28 -07:00
|
|
|
query: IDataObject = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('messageBirdApi');
|
2020-06-11 15:57:32 -07:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
2020-06-11 17:40:58 -07:00
|
|
|
Authorization: `AccessKey ${credentials.accessKey}`,
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs: query,
|
|
|
|
body,
|
|
|
|
uri: `https://rest.messagebird.com${resource}`,
|
2020-06-12 00:39:56 -07:00
|
|
|
json: true,
|
2020-06-11 15:57:32 -07:00
|
|
|
};
|
|
|
|
try {
|
2020-12-22 23:51:07 -08:00
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
2020-06-11 15:57:32 -07:00
|
|
|
return await this.helpers.request(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-06-11 15:57:32 -07:00
|
|
|
}
|
|
|
|
}
|