n8n/packages/nodes-base/nodes/HomeAssistant/GenericFunctions.ts
Ricardo Espinoza 477b3598ea
Add Home AssistantIO node (#1974)
*  Add Home Assistant io node

* Implement continueOnFail

* Add Camera Proxy resource

* Clean up

* Minor improvements

* Remove 'Io' from the node name & code

* Fix generic functions naming

*  Improvements

* Apply review changes & fix minor bugs

* Reduce nesting for additional attributes

* Minor changes

*  Minor improvements and deactivate "Event" resource

Co-authored-by: dali <servfrdali@yahoo.fr>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2021-07-10 13:44:23 +02:00

43 lines
1 KiB
TypeScript

import {
OptionsWithUri
} from 'request';
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';
export async function homeAssistantApiRequest(this: IExecuteFunctions, method: string, resource: string, body: IDataObject = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}) {
const credentials = this.getCredentials('homeAssistantApi');
if (credentials === undefined) {
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
}
let options: OptionsWithUri = {
headers: {
Authorization: `Bearer ${credentials.accessToken}`,
},
method,
qs,
body,
uri: uri ?? `${credentials.ssl === true ? 'https' : 'http'}://${credentials.host}:${credentials.port}/api${resource}`,
json: true,
};
options = Object.assign({}, options, option);
if (Object.keys(options.body).length === 0) {
delete options.body;
}
try {
return await this.helpers.request(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
}