2024-02-14 07:29:09 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
IDataObject,
|
|
|
|
JsonObject,
|
|
|
|
IHttpRequestMethods,
|
|
|
|
IRequestOptions,
|
|
|
|
} 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,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2020-06-11 15:57:32 -07:00
|
|
|
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
|
|
|
|
2024-02-14 07:29:09 -08:00
|
|
|
const options: IRequestOptions = {
|
2020-06-11 15:57:32 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|