/** * Query for 5 documents using the Watson Discovery Service * @param {string} topic - The topic to query for * @returns {promise} */ function query(topic) { var promise = new Promise(function(resolve, reject) { discovery.query({ environment_id : config.DISCOVERY.env_id, collection_id : 'news-en', query : topic, count : NUM_DOCS_TO_QUERY }, function(error, data) { if (error) { reject(error); } else { resolve(data); } }); }); return promise; }
// NOTE, due to the use of async/await, this file is not intended to be imported by the front-end // TODO investigate escaping queries before sending to the discovery service // This wrapper sets the discover service up and connects to the proper discovery environment and collection before making a query. // // All the params are passed straight through. function makeQuery(params) { const discovery = new DiscoveryV1({ version: '2017-08-01' }) // Using a Promise to allow the use of async/await in other code, this allows calls without callback hell which is important in // finding the correct collection, it requires both the environment and a discovery instance. return new Promise(async (resolve, reject) => { // Watson News now uses pre-defined env and coll id values params.environment_id = 'system' params.collection_id = 'news' // The SDK isn't setup for promises, wrapping in one so that we may use async/await with other calls to these functions discovery.query(params, (error, data) => { if (error) { reject(error) } else { resolve(data) } }) }) }