2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-06-11 15:57:32 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions, IHookFunctions } from 'n8n-core';
|
2020-06-11 17:40:58 -07:00
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
import type { IDataObject, JsonObject } from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { 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
|
|
|
): 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) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2020-06-11 15:57:32 -07:00
|
|
|
}
|
|
|
|
}
|