2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IDataObject,
|
|
|
|
IExecuteFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
JsonObject,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { jsonParse, NodeApiError } from 'n8n-workflow';
|
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-04-04 07:04:25 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function cockpitApiRequest(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
2022-08-01 13:47:55 -07:00
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
body: any = {},
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('cockpitApi');
|
2020-04-04 07:04:25 -07:00
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs: {
|
2022-12-02 12:54:28 -08:00
|
|
|
token: credentials.accessToken,
|
2020-04-04 07:04:25 -07:00
|
|
|
},
|
|
|
|
body,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `${credentials.url}/api${resource}`,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2020-04-04 07:04:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
if (Object.keys(options.body as IDataObject).length === 0) {
|
2020-04-04 07:04:25 -07:00
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.request(options);
|
2020-04-04 07:04:25 -07:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2020-04-04 07:04:25 -07:00
|
|
|
}
|
|
|
|
}
|
2020-04-11 11:25:20 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export function createDataFromParameters(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
2022-08-01 13:47:55 -07:00
|
|
|
itemIndex: number,
|
|
|
|
): IDataObject {
|
2020-04-11 11:25:20 -07:00
|
|
|
const dataFieldsAreJson = this.getNodeParameter('jsonDataFields', itemIndex) as boolean;
|
|
|
|
|
|
|
|
if (dataFieldsAreJson) {
|
|
|
|
// Parameters are defined as JSON
|
2022-10-21 11:52:43 -07:00
|
|
|
return jsonParse(this.getNodeParameter('dataFieldsJson', itemIndex, '{}') as string);
|
2020-04-11 11:25:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parameters are defined in UI
|
2020-04-11 11:30:28 -07:00
|
|
|
const uiDataFields = this.getNodeParameter('dataFieldsUi', itemIndex, {}) as IDataObject;
|
2020-04-11 11:25:20 -07:00
|
|
|
const unpacked: IDataObject = {};
|
|
|
|
|
|
|
|
if (uiDataFields.field === undefined) {
|
|
|
|
return unpacked;
|
|
|
|
}
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
for (const field of uiDataFields.field as IDataObject[]) {
|
|
|
|
unpacked[field.name as string] = field.value;
|
2020-04-11 11:25:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return unpacked;
|
|
|
|
}
|