UNPKG

node-red-contrib-opentext

Version:

node-red-contrib-opentext - An Opentext Core client

221 lines (185 loc) 6.95 kB
/** * MIT License * * Copyright (c) 2019 Marcos Caputo <caputo.marcos@gmail.com> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software.. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * **/ module.exports = function (RED) { "use strict"; // require any external libraries we may need.... let request = require("request"); var querystring = require("querystring"); // The main node definition - most things happen in here function CaptureNode(captureNode) { // Create a RED node RED.nodes.createNode(this, captureNode); // Store local copies of the node configuration (as defined in the .html) this.name = captureNode.name || ""; this.base_url = captureNode.base_url || ""; this.id = captureNode.id || ""; this.content_type = captureNode.content_type || ""; this.to_name = captureNode.to_name || ""; this.to_email = captureNode.to_email || ""; this.email_subject = captureNode.email_subject || ""; this.email_message = captureNode.email_message || ""; // copy "this" object in case we need it in context of callbacks of other functions. let node = this; let msg = {}; let msg1 = {}; //msg.payload = this.payload; // respond to inputs.... this.on("input", function (msg) { // set an empty form node.status({ fill: "yellow", shape: "dot", text: "preparing....", }); // TODO - ??? =) let Method = "Post"; let Authorization = ""; //let url = ""; let Document = ""; let Body = ""; let Headers =""; let Options = ""; //let Form = {}; let Databuffer =""; if (msg.coreCaptureCaptureRequest){ if (msg.coreCaptureCaptureRequest.url) node.base_url = msg.coreCaptureCaptureRequest.base_url; if (msg.coreCaptureCaptureRequest.email.from_name) node.id = msg.coreCaptureCaptureRequest.id; if (msg.coreCaptureCaptureRequest.email.from_email) node.content_type = msg.coreCaptureCaptureRequest.content_type; if (msg.coreCaptureCaptureRequest.email.to_name) node.to_name = msg.coreCaptureCaptureRequest.email.to_name; if (msg.coreCaptureCaptureRequest.email.to_email) node.to_email = msg.coreCaptureCaptureRequest.email.to_email; if(msg.coreCaptureCaptureRequest.email.subject) node.email_subject = msg.coreCaptureCaptureRequest.email.subject; if (msg.coreCaptureCaptureRequest.email.message) node.email_message = msg.coreCaptureCaptureRequest.email.message; // get accesstoken & content from input } node.accesstoken = msg.payload.access_token; //set token Authorization = "Bearer " + node.accesstoken; let send_url = node.base_url + "/cp-rest/session/services/fullpageocr/"; //sign Document------------------------------------------------------------------------------------------------------ //sign Document // Capture url console.log("payload->"); console.log(node.id); console.log(node.content_type); if (node.id == "") node.id = msg.payload.id; if (content_type == "") node.content_type = msg.payload.content_Type; console.log(node.id); console.log(node.content_type); console.log("->payload"); capture url Document = msg.payload.url; console.log(Document); //clear Body // Body = ""; Headers =""; Options = ""; Body =JSON.stringify({ }); console.log("Body "); console.log(Body); //set Headers Headers = { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(Body), Authorization: Authorization, }; console.log("Headers "); console.log(Headers); // set Option Options = { method: Method , url: send_url, headers: Headers, body: Body, json: false, }; console.log("options ok"); // make a post request request(Options, function (err, response, body) { if (msg.coreCaptureRequest) delete msg.coreCaptureRequest; try { let CaptureBody = JSON.parse(body ? body : JSON.stringify("{}")); if (response && response.statusCode < 299 && response.statusCode > 199) { console.log(response.statusMessage); console.log(response.body); msg.payload = CaptureBody; msg.payload.statusCode = response.statusCode; node.status({ fill: "green", shape: "dot", text: `HTTP ${response.statusCode}, Document has been sent!!`, }); } else if ( response && response.statusCode && response.statusCode !== 200 ) { console.log(response.statusMessage); console.log(response.body); msg.payload = { statusCode: response.statusCode, statusMessage: response.statusMessage, body: CaptureBody, }; node.status({ fill: "red", shape: "dot", text: `HTTP ${response.statusCode}, error sending document!`, }); } if (err && err.code) { msg.err = JSON.parse(JSON.stringify(err)); node.status({ fill: "yellow", shape: "dot", text: `ERR ${err.code}` }); } else if (err && err.message && err.stack) { msg.err = { message: err.message, stack: err.stack }; node.status({ fill: "black", shape: "dot", text: `ERR ${err.message}`, }); } node.send(msg); } catch (err) { var d = new Date(); var n = d.toISOString(); console.log(n + ": " + err.message); console.log(n + ": " + body.replace(/ |\r\n|\n|\r/gm, "")); msg.err = JSON.parse(JSON.stringify(err)); node.status({ fill: "blue", shape: "dot", text: `ERR ${err.code}` }); } }); }); } RED.nodes.registerType("capture", CaptureNode); };