2023-03-09 09:13:15 -08:00
|
|
|
import type { IExecuteFunctions, IHookFunctions, IDataObject, JsonObject } from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2021-03-23 15:49:08 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2021-12-15 13:07:25 -08:00
|
|
|
|
2021-03-23 15:49:08 -07:00
|
|
|
/**
|
|
|
|
* Make an API request to Plivo.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export async function plivoApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const credentials = (await this.getCredentials('plivoApi')) as {
|
|
|
|
authId: string;
|
|
|
|
authToken: string;
|
|
|
|
};
|
2021-03-23 15:49:08 -07:00
|
|
|
|
2021-12-15 13:07:25 -08:00
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'user-agent': 'plivo-n8n',
|
|
|
|
},
|
2021-03-23 15:49:08 -07:00
|
|
|
method,
|
|
|
|
form: body,
|
|
|
|
qs,
|
|
|
|
uri: `https://api.plivo.com/v1/Account/${credentials.authId}${endpoint}/`,
|
|
|
|
auth: {
|
|
|
|
user: credentials.authId,
|
|
|
|
pass: credentials.authToken,
|
|
|
|
},
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await this.helpers.request(options);
|
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2021-03-23 15:49:08 -07:00
|
|
|
}
|
|
|
|
}
|