2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IDataObject,
|
|
|
|
JsonObject,
|
2024-02-14 07:29:09 -08:00
|
|
|
IHttpRequestMethods,
|
2024-11-07 03:42:47 -08:00
|
|
|
IHttpRequestOptions,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2020-11-10 10:15:56 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function gotifyApiRequest(
|
2024-11-07 03:42:47 -08:00
|
|
|
context: IExecuteFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-17 08:50:24 -07:00
|
|
|
path: string,
|
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string | undefined,
|
2022-11-08 06:28:21 -08:00
|
|
|
_option = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
): Promise<any> {
|
2024-11-07 03:42:47 -08:00
|
|
|
const credentials = await context.getCredentials('gotifyApi');
|
2020-11-10 10:15:56 -08:00
|
|
|
|
2024-11-07 03:42:47 -08:00
|
|
|
const options: IHttpRequestOptions = {
|
2020-11-10 10:15:56 -08:00
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
2024-11-07 03:42:47 -08:00
|
|
|
url: uri ?? `${credentials.url}${path}`,
|
2020-11-10 10:15:56 -08:00
|
|
|
json: true,
|
2024-11-07 03:42:47 -08:00
|
|
|
skipSslCertificateValidation: credentials.ignoreSSLIssues as boolean,
|
2020-11-10 10:15:56 -08:00
|
|
|
};
|
|
|
|
try {
|
2023-02-27 19:39:43 -08:00
|
|
|
if (Object.keys(body as IDataObject).length === 0) {
|
2020-11-10 10:15:56 -08:00
|
|
|
delete options.body;
|
|
|
|
}
|
2024-11-07 03:42:47 -08:00
|
|
|
return await context.helpers.httpRequestWithAuthentication.call(context, 'gotifyApi', options);
|
2020-11-10 10:15:56 -08:00
|
|
|
} catch (error) {
|
2024-11-07 03:42:47 -08:00
|
|
|
throw new NodeApiError(context.getNode(), error as JsonObject);
|
2020-11-10 10:15:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function gotifyApiRequestAllItems(
|
2024-11-07 03:42:47 -08:00
|
|
|
context: IExecuteFunctions,
|
2022-08-17 08:50:24 -07:00
|
|
|
propertyName: string,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-17 08:50:24 -07:00
|
|
|
endpoint: string,
|
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2020-11-10 10:15:56 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
let uri: string | undefined;
|
|
|
|
query.limit = 100;
|
|
|
|
do {
|
2024-11-07 03:42:47 -08:00
|
|
|
responseData = await gotifyApiRequest(context, method, endpoint, body, query, uri);
|
2020-11-10 10:15:56 -08:00
|
|
|
if (responseData.paging.next) {
|
|
|
|
uri = responseData.paging.next;
|
|
|
|
}
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (responseData.paging.next);
|
2020-11-10 10:15:56 -08:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|