'use strict'; var rp = require('request-promise'); var logger = require('log4js').getLogger(); var util = require('util'); var request = require('request'); var btoa = require('btoa'); var buildAuthentication = function() { var user = 'a-qz0da4-dfwwdkmkzr'; // Your actual username var pass = '9txJEf3Cjy7hkSOvkv'; // Your actual password var token = user.concat(':', pass); return 'Basic '.concat(btoa(token)); }; function getData(data, prev) { return new Promise(function(resolve, reject) { logger.debug('Working...'); var range = ''; if (data.hasOwnProperty('start')) { range = ['?start=', data.start, '&end=', data.end].join(''); } if (data.hasOwnProperty('bookmark')) { range = range.concat(range.length > 0 ? '&' : '?'); range = range.concat('_bookmark=',encodeURIComponent(data.bookmark)); } var options = { uri: 'https://qz0da4.internetofthings.ibmcloud.com/api/v0002/historian/types/mDot/devices/' + data.id + range, headers: { Authorization: buildAuthentication() }, json: true }; logger.debug(options); // This is put together to get around the Paging that bluemix uses. // They limit the number of results to 100 per query // Kind of sloppy using the promises like this I think. rp(options) .then(function(d) { prev.events.concat(d.events); Array.prototype.push.apply(prev.events, d.events); if (d.hasOwnProperty('bookmark')) { data.bookmark = d.bookmark; getData(data, prev).then(function(d) { resolve(d); }); } else { logger.debug('Final resolve...'); resolve(prev); } }) .catch(function(err) { logger.error(err); reject(err); }); }); } var self = module.exports = { doGet: function(data) { return new Promise(function(resolve, reject) { logger.debug('Inside doGet Promise'); logger.debug('About to query'); getData(data,{events: []}).then(function(d) { resolve(d); }).catch(function(e) { logger.error(e); reject(e); }); }); } };