gads
Version:
An unofficial JS client library for the SOAP-based DFP Ads API
111 lines • 4.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("../common/util");
const dfpReportError_1 = require("./dfpReportError");
const fs = require("fs");
const http = require("http");
const https = require("https");
const constants_1 = require("./constants");
// A utility that can be used to download reports and PQL result sets
class DataDownloader {
// Initializes a DataDownloader
constructor(
// The DfpClient whose attributes will be used to
// authorize your report download and PQL query requests.
dfpClient,
// Interval between checks for report status
timeout = constants_1.REPORT_JOB_STATUS_TIMEOUT_MS) {
this.dfpClient = dfpClient;
this.timeout = timeout;
}
// Lazily initializes a report service client
_getReportService() {
return (this.reportService) ?
this.reportService :
this.reportService = this.dfpClient.getService('ReportService');
}
downloadReport(
// The ID of the report job to wait for
reportJobId,
// The options for the report download request
options, cb) {
// Get the report service
const promise = this._getReportService()
// Get report download URL
.then(service => service.getReportDownloadUrlWithOptions({
reportJobId,
reportDownloadOptions: options
}))
// Download the report
.then(res => new Promise((resolve, reject) => {
const url = res.rval;
// Get the download client
const get = url.startsWith('https:') ? https.get : http.get;
// Download the report
get(url, (res) => {
if (res.statusCode === 200) {
resolve(res);
// Report error if not status OK
// Consume response data to free up memory
}
else {
res.resume();
reject(new Error(`Report download failed with status code ${res.statusCode}`));
}
}).on('error', reject);
}));
return util_1.callbackOrPromise(cb, promise);
}
downloadReportToFile(
// The ID of the report job to wait for
reportJobId,
// A writeable, file-like object to write to
outFile,
// The options for the report download request
options, cb) {
// Get download stream
return util_1.callbackOrPromise(cb, this.downloadReport(reportJobId, options)
// Write stream to file
.then((res) => new Promise((resolve, reject) => {
const stream = fs.createWriteStream(outFile);
res.pipe(stream, { end: true });
res.on('error', reject);
stream.on('error', reject);
stream.on('finish', resolve);
})));
}
waitForReport(
// The report job to wait for. This may be a dictionary
// or an instance of the generated ReportJob class
reportJob, cb) {
const promise = this._getReportService()
.then(service => {
if (typeof reportJob === 'number') {
return this._waitForReport(service, reportJob);
}
return service.runReportJob({ reportJob })
.then(res => this._waitForReport(service, res.rval.id));
});
return util_1.callbackOrPromise(cb, promise);
}
_waitForReport(service, reportJobId) {
return service.getReportJobStatus({ reportJobId })
.then(runReportJobResponse => {
// Get report job status
const status = runReportJobResponse.rval;
// Return report job ID
if (status === 'COMPLETED') {
return reportJobId;
}
// Throw error
if (status === 'FAILED') {
throw new dfpReportError_1.DfpReportError(reportJobId);
}
// Poll report job's status until it terminates
return util_1.timeout(this.timeout)
.then(() => this._waitForReport(service, reportJobId));
});
}
}
exports.DataDownloader = DataDownloader;
//# sourceMappingURL=dataDownloader.js.map