use of com.google.api.ads.admanager.axis.v202205.ReportDownloadOptions in project googleads-java-lib by googleads.
the class ReportDownloader method getDownloadUrl.
/**
* Gets the download URL for a GZip or plain-text format report. If you requested a compressed
* report, you may want to save your file with a gz or zip extension.
*
* <pre><code>
* URL url = reportDownloader.getDownloadUrl(options);
* Resources.asByteSource(url).copyTo(Files.asByteSink(file));
* </code></pre>
*
* @param options the options to download the report with
* @return the URL for the report download
* @throws RemoteException if there was an error performing any Axis call
* @throws MalformedURLException if there is an error forming the download URL
* @throws IllegalStateException if the report is not ready to be downloaded
*/
public URL getDownloadUrl(ReportDownloadOptions options) throws RemoteException, MalformedURLException {
ReportJobStatus status = reportService.getReportJobStatus(reportJobId);
Preconditions.checkState(status == ReportJobStatus.COMPLETED, "Report " + reportJobId + " must be completed before downloading. It is currently: " + status);
return new URL(reportService.getReportDownloadUrlWithOptions(reportJobId, options));
}
use of com.google.api.ads.admanager.axis.v202205.ReportDownloadOptions in project googleads-java-lib by googleads.
the class RunInventoryReport method runExample.
/**
* Runs the example.
*
* @param adManagerServices the services factory.
* @param session the session.
* @throws ApiException if the API request failed with one or more service errors.
* @throws RemoteException if the API request failed due to other errors.
* @throws IOException if the report's contents could not be written to a temp file.
* @throws InterruptedException if the thread was interrupted while waiting for the report to be
* ready.
*/
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session) throws IOException, InterruptedException {
// Get the ReportService.
ReportServiceInterface reportService = adManagerServices.get(session, ReportServiceInterface.class);
// Create report query.
ReportQuery reportQuery = new ReportQuery();
reportQuery.setDimensions(new Dimension[] { Dimension.AD_UNIT_ID, Dimension.AD_UNIT_NAME });
reportQuery.setColumns(new Column[] { Column.AD_SERVER_IMPRESSIONS, Column.AD_SERVER_CLICKS, Column.AD_EXCHANGE_IMPRESSIONS, Column.AD_EXCHANGE_CLICKS, Column.TOTAL_LINE_ITEM_LEVEL_IMPRESSIONS, Column.TOTAL_LINE_ITEM_LEVEL_CPM_AND_CPC_REVENUE });
// Set the ad unit view to hierarchical.
reportQuery.setAdUnitView(ReportQueryAdUnitView.HIERARCHICAL);
// Set the dynamic date range type or a custom start and end date.
reportQuery.setDateRangeType(DateRangeType.YESTERDAY);
// Create report job.
ReportJob reportJob = new ReportJob();
reportJob.setReportQuery(reportQuery);
// Run report job.
reportJob = reportService.runReportJob(reportJob);
// Create report downloader.
ReportDownloader reportDownloader = new ReportDownloader(reportService, reportJob.getId());
// Wait for the report to be ready.
reportDownloader.waitForReportReady();
// Change to your file location.
File file = File.createTempFile("inventory-report-", ".csv.gz");
System.out.printf("Downloading report to %s ...", file.toString());
// Download the report.
ReportDownloadOptions options = new ReportDownloadOptions();
options.setExportFormat(ExportFormat.CSV_DUMP);
options.setUseGzipCompression(true);
URL url = reportDownloader.getDownloadUrl(options);
Resources.asByteSource(url).copyTo(Files.asByteSink(file));
System.out.println("done.");
}
use of com.google.api.ads.admanager.axis.v202205.ReportDownloadOptions in project googleads-java-lib by googleads.
the class RunSavedQuery method runExample.
/**
* Runs the example.
*
* @param adManagerServices the services factory.
* @param session the session.
* @param savedQueryId the ID of the saved query to run. This ID is part of the URL in the Ad
* Manager UI.
* @throws ApiException if the API request failed with one or more service errors.
* @throws RemoteException if the API request failed due to other errors.
* @throws IOException if unable to write the response to a file.
* @throws InterruptedException if the thread is interrupted while waiting for the report to
* complete.
*/
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session, long savedQueryId) throws IOException, InterruptedException {
// Get the ReportService.
ReportServiceInterface reportService = adManagerServices.get(session, ReportServiceInterface.class);
// Create statement to retrieve the saved query.
StatementBuilder statementBuilder = new StatementBuilder().where("id = :id").orderBy("id ASC").limit(1).withBindVariableValue("id", savedQueryId);
SavedQueryPage page = reportService.getSavedQueriesByStatement(statementBuilder.toStatement());
SavedQuery savedQuery = Iterables.getOnlyElement(Arrays.asList(page.getResults()));
if (!savedQuery.getIsCompatibleWithApiVersion()) {
throw new IllegalStateException("The saved query is not compatible with this API version.");
}
// Optionally modify the query.
ReportQuery reportQuery = savedQuery.getReportQuery();
reportQuery.setAdUnitView(ReportQueryAdUnitView.HIERARCHICAL);
// Create report job using the saved query.
ReportJob reportJob = new ReportJob();
reportJob.setReportQuery(reportQuery);
// Run report job.
reportJob = reportService.runReportJob(reportJob);
// Create report downloader.
ReportDownloader reportDownloader = new ReportDownloader(reportService, reportJob.getId());
// Wait for the report to be ready.
reportDownloader.waitForReportReady();
// Change to your file location.
File file = File.createTempFile("inventory-report-", ".csv.gz");
System.out.printf("Downloading report to %s ...", file.toString());
// Download the report.
ReportDownloadOptions options = new ReportDownloadOptions();
options.setExportFormat(ExportFormat.CSV_DUMP);
options.setUseGzipCompression(true);
URL url = reportDownloader.getDownloadUrl(options);
Resources.asByteSource(url).copyTo(Files.asByteSink(file));
System.out.println("done.");
}
use of com.google.api.ads.admanager.axis.v202205.ReportDownloadOptions in project googleads-java-lib by googleads.
the class ReportDownloaderTest method testGetReportAsCharSource.
@Test
public void testGetReportAsCharSource() throws IOException {
ReportDownloader downloader = new ReportDownloader(reportService, 1);
URL resourceUrl = ReportProvider.TEST_REPORT_RESOURCE;
String report = Resources.toString(resourceUrl, Charset.forName("UTF-8"));
when(reportService.getReportJobStatus(ArgumentMatchers.anyLong())).thenReturn(ReportJobStatus.COMPLETED);
when(reportService.getReportDownloadUrlWithOptions(ArgumentMatchers.anyLong(), ArgumentMatchers.any(ReportDownloadOptions.class))).thenReturn(resourceUrl.toString());
ReportDownloadOptions options = new ReportDownloadOptions();
options.setExportFormat(ExportFormat.CSV_DUMP);
options.setUseGzipCompression(false);
CharSource reportCharSource = downloader.getReportAsCharSource(options);
assertEquals(report, reportCharSource.read());
}
use of com.google.api.ads.admanager.axis.v202205.ReportDownloadOptions in project googleads-java-lib by googleads.
the class ReportDownloaderTest method testGetReportDownloadUrlWithOptions.
@Test
public void testGetReportDownloadUrlWithOptions() throws ApiException, RemoteException, MalformedURLException {
ReportDownloader downloader = new ReportDownloader(reportService, 1);
when(reportService.getReportJobStatus(ArgumentMatchers.anyLong())).thenReturn(ReportJobStatus.COMPLETED);
when(reportService.getReportDownloadUrlWithOptions(ArgumentMatchers.anyLong(), ArgumentMatchers.any(ReportDownloadOptions.class))).thenReturn("https://www.google.com/");
URL downloadUrl = downloader.getDownloadUrl(new ReportDownloadOptions());
assertEquals("https://www.google.com/", downloadUrl.toString());
}
Aggregations