// Download sensors from RPI Data Service
// Outputs each sensor in list to a file in output directory.

var fs = require('fs');
var request = require('request');
var qs = require('querystring');

var argv = require('yargs')
  .usage('Usage: $0 <RPI HOST URL> <User UID> <-o directory>')
  .demand(2)
  .demand(['o'])
  .argv;

var url = argv._[0] + "/v01/users/" + argv._[1] + "/sensors";

request({
  url: url,
  qs: {
    detail: 'true'
  }
}, function (error, response, body) {
  outputStations(JSON.parse(body));
});

var outputStations = function (stations) {
  stations.forEach(function(item) {
    var filename = argv.o + "/" + encodeURIComponent(item.title) + ".json";
    fs.writeFile(filename, JSON.stringify(item, null, "  "), function (err) {
      if (err) {
        console.error("error!", err);
      }
    });
  });
};

@@ -96,16 +96,139 @@
 rectory.
+%0A// If Api Key is specified, then it will be added to each object. Api Key is%0A// for access through the Data Service Proxy.
 %0A%0Avar fs
@@ -386,16 +386,26 @@
 ser UID%3E
+ %5BAPI key%5D
  %3C-o dir
@@ -803,16 +803,81 @@
 %22.json%22;
+%0A%0A    if (argv._%5B2%5D) %7B%0A      item.api_key = argv._%5B2%5D;%0A    %7D%0A    
 %0A    fs.

Add API key option for sensor downloader