diff --git a/sdk/agent.js b/sdk/agent.js deleted file mode 100644 index 48578f8..0000000 --- a/sdk/agent.js +++ /dev/null @@ -1,249 +0,0 @@ -/* - IBM Confidential - OCO Source Materials - © Copyright IBM Corp. 2017 - */ -/* -const express = require('express'); -var bodyParser = require('body-parser'); -var request = require('request'); -const app = express(); -require('dotenv').config(); -app.use(bodyParser.json()); - -const uuidv1 = require('uuid/v1'); -var myapp = require('../app'); -var cache = require('./storage-memory'); -const url = require('url'); - - -var api_key = process.env.API_KEY; -var agent_url = process.env.HUB_URL; - -var agentHost = process.env.AGENT_HOST; - -var actions = {}; - -var conditions = {}; - -function Agent(event, condition_method, action_method, request_key) { - this.event = event; - this.condition_method = condition_method; - this.action_method = action_method; - this.request_key = request_key; -} -*/ -/* - Agent subscription method. - Event is a string and is one of: "object-create", "object-update", "object-delete", "relation-create", "relation-delete" - condition_method is a function that returns a string 'true' or 'false' depending on whether the action should be called - action_method is the main function of the agent - */ -/* -Agent.prototype.subscribe = function () { - var post_data = { - 'event': this.event, - 'action': agentHost + "/action" - }; - post_data.action = post_data.action.replace("//action", "/action"); - - if (this.condition_method) { - //post_data['condition'] = agentHost + "/condition"; - post_data['condition'] = ""; - } - - - var headers = { - 'Content-type': 'application/json', - 'api_key': app.get("api_key") || api_key - }; - - var options = { - url: (app.get("hub_url") || agent_url) + 'agent/register_eca_agent', - method: 'POST', - headers: headers, - body: JSON.stringify(post_data) - }; - - var self = this; - return new Promise(function (res, rej) { - request(options, function (err, response, body) { - if (err || response.statusCode !== 200) { - console.log('Error subscribing: ' + err + ' ' + body); - rej(body); - } else { - var id = JSON.parse(body)['id']; - actions[id] = self.action_method; - actions["request_key"] = self.request_key; - if (self.condition_method) { - conditions[id] = self.condition_method; - } - console.log('Created subscription to event', self.event, 'and received ID', id); - - cache.read("data", self.request_key).then(function (result) { - - if (result) { - - result.subscriptionid = id; - cache.write("data", self.request_key, result); - } else { - - var result = {}; - result.subscriptionid = id; - cache.write("data", self.request_key, result); - } - }, function (err) { - console.log("err ", err); - }); - self.id = id; - res(body); - } - }); - }); -}; -*/ -/* Agent deletion method - - */ -/* -Agent.prototype.delete = function () { - if (!this.id) { - throw 'No id -- did you subscribe yet?'; - } - - var headers = { - 'Content-type': 'application/json', - 'api_key': app.get("api_key") || api_key - }; - - var options = { - //url: (app.get("hub_url") || agent_url) + 'agent/subscription/' + this.id + '/' + this.event, - url: (app.get("hub_url") || agent_url) + 'agent/subscription/' + this.id, - method: 'DELETE', - headers: headers, - }; - - var self = this; - return new Promise(function (res, rej) { - request(options, function (err, response, body) { - if (err || response.statusCode !== 200) { - console.log('Error subscribing: ' + err + ' ' + body); - rej(body); - } else { - console.log('Deleted agent subscription', self.id); - res(body); - } - }); - }); -}; - -// This is the route that will handle all the callbacks from the message broker -app.post('/condition/:var', function (req, res) { - var subId = req.params['var']; - if (!(subId in conditions)) { - res.status(400); - res.send('Bad subscription ID'); - } else { - res.status(200); - var response = conditions[subId](req.body.results); - - Promise.resolve(response).then((response) => { - res.send(response); - }); - } -}); - -app.post('/action/:var', function (req, res) { - var subId = req.params['var']; - if (!(subId in actions)) { - res.status(400); - res.send('Bad subscription ID'); - } else { - res.status(200); - var key = actions["request_key"]; - var response = actions[subId](req.body.results, key); - Promise.resolve(response).then((response) => { - res.send(response); - }); - } -}); - - -app.get('/execute', function (req, res) { - var api_key = req.query["api_key"]; - var hub_url = req.query["hub_url"]; - app.set("api_key", api_key); - console.log("===>>hub_url ", hub_url) - if (hub_url) { - hub_url = url.parse(hub_url).href; - app.set("hub_url", hub_url); - } - console.log("===>>parsed hub_url ", hub_url) - - var key = uuidv1(); - var trans_id = myapp.execute(key); - var obj = {}; - obj.message = "In Progress"; - cache.write("data", key, obj); - res.status(200); - res.send(key); -}); - -app.get('/execute/:trans_id', function (req, res) { - var trans_id = req.params['trans_id']; - console.log("trans_id >>", trans_id); - cache.read("data", trans_id).then(function (result) { - res.status(200); - res.send(result); - }, function (err) { - console.log("err ", err); - res.status(400); - res.send(err); - }); - -}); - - -app.get('/execute', function (req, res) { - var api_key = req.query["api_key"]; - var hub_url = req.query["hub_url"]; - app.set("api_key", api_key); - console.log("===>>hub_url ", hub_url) - if (hub_url) { - hub_url = url.parse(hub_url).href; - app.set("hub_url", hub_url); - } - console.log("===>>parsed hub_url ", hub_url) - - var key = uuidv1(); - var trans_id = myapp.execute(key); - var obj = {}; - obj.message = "In Progress"; - cache.write("data", key, obj); - res.status(200); - res.send(key); -}); - -app.get('/execute/:trans_id', function (req, res) { - var trans_id = req.params['trans_id']; - console.log("trans_id >>", trans_id); - cache.read("data", trans_id).then(function (result) { - res.status(200); - res.send(result); - }, function (err) { - console.log("err ", err); - res.status(400); - res.send(err); - }); - -}); - -var port = process.env.PORT || process.env.AGENT_PORT || 8080; - -app.listen(port, function () { - console.log('Agent REST service is alive! Listening on port', process.env.AGENT_PORT, '\n\n'); -}); - -module.exports.Agent = Agent; -module.exports.App = app; -*/ \ No newline at end of file diff --git a/sdk/config.json b/sdk/config.json deleted file mode 100644 index 5c1da0f..0000000 --- a/sdk/config.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "MESSAGE_HUB": { - "kafka_brokers_sasl": [ - "REPLACE", - "REPLACE" - ], - "user": "REPLACE", - "password": "REPLACE" - } -} diff --git a/sdk/messages.js b/sdk/messages.js index e622e1a..ffdef71 100644 --- a/sdk/messages.js +++ b/sdk/messages.js @@ -12,7 +12,6 @@ app.use(bodyParser.json()); require('dotenv').config(); var Kafka = require('node-rdkafka'); -var config = require('./config.json'); var api_key = process.env.API_KEY; var knowledge_url = process.env.HUB_URL; @@ -22,35 +21,6 @@ class Agent { this.event = event; this.conditionMethod = conditionMethod; this.actionMethod = actionMethod; - - // optional - save the message hub credentials in the configuration file - - /* - // Config common options - var driver_options = { - //'debug': 'all', - 'metadata.broker.list': config.MESSAGE_HUB.kafka_brokers_sasl, - 'security.protocol': 'sasl_ssl', - //'ssl.ca.location': '/etc/ssl/certs', // this is the default location in bluemix - 'sasl.mechanisms': 'PLAIN', - 'sasl.username': config.MESSAGE_HUB.user, - 'sasl.password': config.MESSAGE_HUB.password, - 'api.version.request': true, - 'broker.version.fallback': '0.10.2.1' - }; - - var consumer_opts = { - 'client.id': 'kafka-nodejs-console-sample-consumer', - 'group.id': 'kafka-nodejs-console-sample-group' - }; - - // Add the common options to client - for (var key in driver_options) { - consumer_opts[key] = driver_options[key]; - } - - this.connection_options = consumer_opts; - */ } connect() { @@ -68,7 +38,7 @@ class Agent { return new Promise(function (res, rej) { request(options, function (err, response, body) { - if (err || response.statusCode !== 200) { + if (err || (response && response.statusCode !== 200)) { console.log("error:" + body); rej(body); } else { diff --git a/sdk/object.js b/sdk/object.js index 259c484..abc9fbf 100644 --- a/sdk/object.js +++ b/sdk/object.js @@ -40,7 +40,7 @@ class KnowledgeObject { return new Promise((resolve, reject) => { request(options, (err, response, body) => { - if (err || response.statusCode !== 200) { + if (err || (response && response.statusCode !== 200)) { return reject(body); } @@ -73,7 +73,7 @@ class KnowledgeObject { return new Promise((resolve, reject) => { request(options, (err, response, body) => { - if (err || response.statusCode !== 200) { + if (err || (response && response.statusCode !== 200)) { return reject(body); } diff --git a/sdk/relation.js b/sdk/relation.js index 5e6e4b1..b789767 100644 --- a/sdk/relation.js +++ b/sdk/relation.js @@ -53,7 +53,7 @@ class KnowledgeRelation { }; return new Promise((resolve, reject) => { request(options, (err, response, body) => { - if (err || response.statusCode !== 200) { + if (err || (response && response.statusCode !== 200)) { console.log(`Error creating relation: ${err} ${body}`); return reject(body); }