UNPKG

n8n-nodes-databricks-api

Version:
252 lines 11.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Databricks = void 0; const n8n_workflow_1 = require("n8n-workflow"); const resources_1 = require("./resources"); class Databricks { constructor() { this.description = { displayName: 'Databricks', name: 'databricks', icon: 'file:databricks.svg', group: ['transform'], version: 1, usableAsTool: true, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Interact with Databricks API', defaults: { name: 'Databricks', }, inputs: [n8n_workflow_1.NodeConnectionTypes.Main], outputs: [n8n_workflow_1.NodeConnectionTypes.Main], credentials: [ { name: 'databricks', required: true, }, ], requestDefaults: { baseURL: '={{$credentials.host}}', headers: { Authorization: '=Bearer {{$credentials.token}}', }, }, properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'Genie', value: 'genie' }, { name: 'Databricks SQL', value: 'databricksSql' }, { name: 'Unity Catalog', value: 'unityCatalog' }, { name: 'Model Serving', value: 'modelServing' }, { name: 'Files', value: 'files' }, { name: 'Vector Search', value: 'vectorSearch' }, ], default: 'databricksSql', }, resources_1.filesOperations, resources_1.genieOperations, resources_1.unityCatalogOperations, resources_1.databricksSqlOperations, resources_1.modelServingOperations, resources_1.vectorSearchOperations, ...resources_1.filesParameters, ...resources_1.genieParameters, ...resources_1.unityCatalogParameters, ...resources_1.databricksSqlParameters, ...resources_1.modelServingParameters, ...resources_1.vectorSearchParameters, ], }; } async execute() { var _a, _b; const items = this.getInputData(); const returnData = []; this.logger.debug(`Starting execution with ${items.length} items`); for (let i = 0; i < items.length; i++) { try { this.logger.debug(`Processing item ${i + 1}/${items.length}`); const resource = this.getNodeParameter('resource', i); const operation = this.getNodeParameter('operation', i); this.logger.debug('Node parameters', { resource, operation, itemIndex: i }); if (resource === 'files' && operation === 'uploadFile') { const dataFieldName = this.getNodeParameter('dataFieldName', i); const catalog = this.getNodeParameter('catalog', i); const schema = this.getNodeParameter('schema', i); const volume = this.getNodeParameter('volume', i); const path = this.getNodeParameter('path', i); this.logger.debug('File upload parameters', { dataFieldName, catalog, schema, volume, path }); const credentials = (await this.getCredentials('databricks')); const host = credentials.host; const binaryData = await this.helpers.getBinaryDataBuffer(i, dataFieldName); this.logger.debug('Starting file upload', { host, path, dataSize: binaryData.length }); await this.helpers.httpRequest({ method: 'PUT', url: `${host}/api/2.0/fs/files/Volumes/${catalog}/${schema}/${volume}/${path}`, body: binaryData, headers: { Authorization: `Bearer ${credentials.token}`, 'Content-Type': ((_b = (_a = items[i].binary) === null || _a === void 0 ? void 0 : _a[dataFieldName]) === null || _b === void 0 ? void 0 : _b.mimeType) || 'application/octet-stream', }, encoding: 'arraybuffer', }); this.logger.debug('File upload successful', { path }); returnData.push({ json: { success: true, message: `File uploaded successfully to ${path}`, }, }); } else if (resource === 'genie') { const credentials = (await this.getCredentials('databricks')); const host = credentials.host; let url; let method; let body; switch (operation) { case 'createMessage': url = `${host}/api/2.0/genie/spaces/${this.getNodeParameter('spaceId', i)}/conversations/${this.getNodeParameter('conversationId', i)}/messages`; method = 'POST'; body = { content: this.getNodeParameter('message', i), }; break; case 'getMessage': url = `${host}/api/2.0/genie/spaces/${this.getNodeParameter('spaceId', i)}/conversations/${this.getNodeParameter('conversationId', i)}/messages/${this.getNodeParameter('messageId', i)}`; method = 'GET'; break; case 'getQueryResults': url = `${host}/api/2.0/genie/spaces/${this.getNodeParameter('spaceId', i)}/conversations/${this.getNodeParameter('conversationId', i)}/messages/${this.getNodeParameter('messageId', i)}/attachments/${this.getNodeParameter('attachmentId', i)}/query-result`; method = 'GET'; break; case 'executeMessageQuery': url = `${host}/api/2.0/genie/spaces/${this.getNodeParameter('spaceId', i)}/conversations/${this.getNodeParameter('conversationId', i)}/messages/${this.getNodeParameter('messageId', i)}/attachments/${this.getNodeParameter('attachmentId', i)}/execute-query`; method = 'POST'; break; case 'getSpace': url = `${host}/api/2.0/genie/spaces/${this.getNodeParameter('spaceId', i)}`; method = 'GET'; break; case 'startConversation': const spaceId = this.getNodeParameter('spaceId', i); url = `${host}/api/2.0/genie/spaces/${spaceId}/start-conversation`; method = 'POST'; body = { content: this.getNodeParameter('initialMessage', i), }; break; default: throw new Error(`Unsupported Genie operation: ${operation}`); } this.logger.debug('Making Genie API request', { url, method, body: JSON.stringify(body, null, 2) }); const response = await this.helpers.httpRequest({ method, url, body, headers: { Authorization: `Bearer ${credentials.token}`, 'Content-Type': 'application/json', }, json: true, }); this.logger.debug('Genie API response received', { statusCode: response.statusCode, response: JSON.stringify(response, null, 2) }); returnData.push({ json: response }); } else { this.logger.debug('Passing through unhandled resource', { resource }); returnData.push({ json: items[i].json, }); } } catch (error) { const currentResource = this.getNodeParameter('resource', i); const currentOperation = this.getNodeParameter('operation', i); this.logger.error(`Error processing item ${i + 1}`, { error: error.message, stack: error.stack, itemIndex: i, resource: currentResource, operation: currentOperation }); if (error.response) { this.logger.error('API Error', { status: error.response.status, statusText: error.response.statusText, data: error.response.data }); if (this.continueOnFail()) { returnData.push({ json: { error: `API Error: ${error.response.status} ${error.response.statusText}`, details: error.response.data } }); } else { throw new Error(`API Error: ${error.response.status} ${error.response.statusText}`); } } else if (error.request) { this.logger.error('Network Error', { request: error.request }); if (this.continueOnFail()) { returnData.push({ json: { error: 'Network Error: No response received from server', details: error.message } }); } else { throw new Error('Network Error: No response received from server'); } } else { if (this.continueOnFail()) { returnData.push({ json: { error: error.message, details: error.stack } }); } else { throw error; } } } } this.logger.debug('Execution completed successfully'); return [returnData]; } } exports.Databricks = Databricks; //# sourceMappingURL=Databricks.node.js.map