Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 57 additions & 7 deletions adwords/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

const request = require('request');
const util = require('util');
const fs = require('fs');
const AdwordsConstants = require('./constants');
const AdwordsReportBuilder = require('./report-builder');
const AdwordsAuth = require('./auth');
const https = require('https');
const querystring = require('querystring');

class AdwordsReport {

Expand Down Expand Up @@ -43,12 +44,9 @@ class AdwordsReport {
return callback(error);
}

request({
uri: 'https://adwords.google.com/api/adwords/reportdownload/' + apiVersion,
method: 'POST',
headers: headers,
form: this.buildReportBody(report)
}, (error, response, body) => {
let params = this.buildParams(apiVersion, headers, report);

request(params, (error, response, body) => {
if (error || this.reportBodyContainsError(report, body)) {
error = error || body;
if (-1 !== error.toString().indexOf(AdwordsConstants.OAUTH_ERROR) && retryRequest) {
Expand All @@ -62,6 +60,58 @@ class AdwordsReport {
});
}

/**
* Builds parameters.
*
* @param {string} apiVersion The api version
* @param {Object} headers The headers
* @param {Object} report The report
* @return {Object} The parameters.
*/
buildParams(apiVersion, headers, report) {
return {
uri: 'https://adwords.google.com/api/adwords/reportdownload/' + apiVersion,
hostname: 'adwords.google.com',
port: 443,
path: '/api/adwords/reportdownload/' + apiVersion,
method: 'POST',
headers: headers,
form: this.buildReportBody(report)
};
}

/**
* Streams a report.
*
* @param {string} apiVersion The api version
* @param {Object} report The report
* @param {Function} callback The callback
* @return {boolean} { description_of_the_return_value }
*
* @throws {Object} { If there is an error in the call }
*/
streamReport(apiVersion, report, callback) {
report = report || {};
apiVersion = apiVersion || AdwordsConstants.DEFAULT_ADWORDS_VERSION;

this.getHeaders(report.additionalHeaders, (error, headers) => {
if (error) { return callback(error); }

let params = this.buildParams(apiVersion, headers, report);

let postData = querystring.stringify(params.form);
headers['Content-Type'] = 'application/x-www-form-urlencoded';
headers['Content-Length'] = postData.length;

let request = https.request(params, (response) => {
callback(response);
});

request.write(postData);
request.end();
});
}

/**
* Determines if the body contains an error message
* @param report {object} the report object
Expand Down
157 changes: 152 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"xmlbuilder": "^10.0.0"
},
"devDependencies": {
"mocha": "^5.2.0"
"mocha": "^5.2.0",
"mock-req": "^0.2.0",
"mock-res": "^0.5.0",
"sinon": "^7.1.1"
}
}
Loading