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';
|
2021-03-23 15:49:08 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to Plivo.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export async function plivoApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2021-03-23 15:49:08 -07:00
|
|
|
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
|
|
|
|
2024-02-14 07:29:09 -08:00
|
|
|
const options: IRequestOptions = {
|
2021-12-15 13:07:25 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|