// run a report on line item #1234567

var Dfp = require('../lib/Dfp');
var dfpConfig = require('/dfpCredentials');

var dfpUser = new Dfp.User(dfpConfig.networkCore, dfpConfig.applicationName);
dfpUser.setSettings(dfpConfig);

dfpUser.getService('ReportService', function (reportService) {

  var intervalId = null;
  var results = null;
  var args = {
    reportJob: {
      reportQuery: {
        dimensions: ['DATE'],
        columns: [ 'AD_SERVER_CLICKS', 'AD_SERVER_IMPRESSIONS' ],
        dimensionAttributes : [],
        startDate: { year: 2014, month: 5, day: 22 },
        endDate: { year: 2014, month: 6, day: 21 },
        dimensionFilters : [],
        statement: { query : 'WHERE LINE_ITEM_ID = 123456789'}
      }
    }
  };

  function download_report () {

    var reportId = results.rval.id;
    console.log('Trying to get report #' + reportId);

    reportService.getReportJob({reportJobId : reportId}, function (err, data) {

      if (err) {
        return console.log('ERROR', err);
      }
      console.log('Report Job #' + reportId + ' returned ' + data.rval.reportJobStatus);

      if (data.rval.reportJobStatus === 'COMPLETED') {

        var download_args = {
          reportJobId           : reportId,
          reportDownloadOptions : {
            exportFormat            : 'CSV_EXCEL',
            includeReportProperties : false,
            includeTotalsRow        : false,
            useGzipCompression      : true
          }
        };

        reportService.getReportDownloadUrlWithOptions(download_args, function (err, data) {
          if (err) {
            return console.log('ERROR', err.body);
          }

          console.log(data);
        });
      }

      if (data.rval.reportJobStatus === 'FAILED') {
        console.log('Report Failed!');
      }

      if (data.rval.reportJobStatus === 'IN_PROGRESS') {
        setTimeout(download_report, 100);
      }

    });
  }

  reportService.runReportJob(args, function (err, jobStatus) {
    if (err) {
      return console.log('ERROR', err.body);
    }

    results = jobStatus;
    setTimeout(download_report, 100);

  });

});

@@ -746,31 +746,34 @@
 unction 
-download
+check
 _report
+_ready
  () %7B%0A%0A 
@@ -1673,20 +1673,117 @@
 ole.log(
-data
+%22Downloading report from %22 + data.rval);%0A          download_report(data.rval, 'downloaded_report.csv'
 );%0A     
@@ -1978,39 +1978,380 @@
 out(
-download_report, 100);%0A      %7D%0A
+check_report_ready, 100);%0A      %7D%0A%0A    %7D);%0A  %7D;%0A%0A  function download_report (download_url, local_filename) %7B%0A    var https = require('https');%0A    var fs = require('fs');%0A    var zlib = require('zlib');%0A%0A    var file = fs.createWriteStream(local_filename);%0A    var request = https.get(download_url, function(response) %7B%0A      response.pipe(zlib.createGunzip()).pipe(file);
 %0A   
@@ -2358,16 +2358,17 @@
  %7D);%0A  %7D
+;
 %0A%0A  repo
@@ -2531,31 +2531,34 @@
 Timeout(
-download
+check
 _report
+_ready
 , 100);%0A

IMPROVE runReport.js