Search in sources :

Example 11 with ReportCallback

use of com.google.api.ads.admanager.lib.utils.ReportCallback in project googleads-java-lib by googleads.

the class AsyncDownloadReport 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.
 */
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session) throws RemoteException {
    // Get the ReportService.
    ReportServiceInterface reportService = adManagerServices.get(session, ReportServiceInterface.class);
    // Create report query.
    ReportQuery reportQuery = new ReportQuery();
    reportQuery.setDimensions(new Dimension[] { Dimension.DATE, Dimension.ORDER_ID });
    reportQuery.setColumns(new Column[] { Column.AD_SERVER_IMPRESSIONS });
    // Set the dynamic date range type or a custom start and end date.
    reportQuery.setDateRangeType(DateRangeType.LAST_WEEK);
    // Create report job.
    ReportJob reportJob = new ReportJob();
    reportJob.setReportQuery(reportQuery);
    // Run report job.
    reportJob = reportService.runReportJob(reportJob);
    // Create report downloader.
    final ReportDownloader reportDownloader = new ReportDownloader(reportService, reportJob.getId());
    reportDownloader.whenReportReady(new ReportCallback() {

        @Override
        public void onSuccess() {
            try {
                // Change to your file location.
                File file = File.createTempFile("async-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.");
            } catch (IOException e) {
                System.err.println("Failed to write report.");
            }
        }

        @Override
        public void onInterruption() {
            System.err.println("Report download interupted.");
        }

        @Override
        public void onFailure() {
            System.err.println("Report download failed.");
        }

        @Override
        public void onException(Exception e) {
            System.err.println("Report download failed.");
            e.printStackTrace();
        }
    });
}
Also used : ReportDownloader(com.google.api.ads.admanager.axis.utils.v202111.ReportDownloader) ReportCallback(com.google.api.ads.admanager.lib.utils.ReportCallback) ReportDownloadOptions(com.google.api.ads.admanager.axis.v202111.ReportDownloadOptions) ReportQuery(com.google.api.ads.admanager.axis.v202111.ReportQuery) ReportServiceInterface(com.google.api.ads.admanager.axis.v202111.ReportServiceInterface) ReportJob(com.google.api.ads.admanager.axis.v202111.ReportJob) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL) ApiException(com.google.api.ads.admanager.axis.v202111.ApiException) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) ValidationException(com.google.api.ads.common.lib.exception.ValidationException)

Example 12 with ReportCallback

use of com.google.api.ads.admanager.lib.utils.ReportCallback in project googleads-java-lib by googleads.

the class ReportDownloaderTest method testWhenReportReady_failed.

@Test
public void testWhenReportReady_failed() throws IOException, InterruptedException {
    ReportDownloader downloader = new ReportDownloader(reportService, 1);
    when(reportService.getReportJobStatus(ArgumentMatchers.anyLong())).thenReturn(ReportJobStatus.FAILED);
    ReportCallback callback = mock(ReportCallback.class);
    downloader.whenReportReady(callback).join();
    Mockito.verify(callback).onFailure();
}
Also used : ReportCallback(com.google.api.ads.admanager.lib.utils.ReportCallback) Test(org.junit.Test)

Example 13 with ReportCallback

use of com.google.api.ads.admanager.lib.utils.ReportCallback in project googleads-java-lib by googleads.

the class ReportDownloaderTest method testWhenReportReady_complete.

@Test
public void testWhenReportReady_complete() throws IOException, InterruptedException {
    ReportDownloader downloader = new ReportDownloader(reportService, 1);
    when(reportService.getReportJobStatus(ArgumentMatchers.anyLong())).thenReturn(ReportJobStatus.COMPLETED);
    ReportCallback callback = mock(ReportCallback.class);
    downloader.whenReportReady(callback).join();
    Mockito.verify(callback).onSuccess();
}
Also used : ReportCallback(com.google.api.ads.admanager.lib.utils.ReportCallback) Test(org.junit.Test)

Example 14 with ReportCallback

use of com.google.api.ads.admanager.lib.utils.ReportCallback in project googleads-java-lib by googleads.

the class ReportDownloaderTest method testWhenReportReady_failed.

@Test
public void testWhenReportReady_failed() throws IOException, InterruptedException {
    ReportDownloader downloader = new ReportDownloader(reportService, 1);
    when(reportService.getReportJobStatus(ArgumentMatchers.anyLong())).thenReturn(ReportJobStatus.FAILED);
    ReportCallback callback = mock(ReportCallback.class);
    downloader.whenReportReady(callback).join();
    Mockito.verify(callback).onFailure();
}
Also used : ReportCallback(com.google.api.ads.admanager.lib.utils.ReportCallback) Test(org.junit.Test)

Example 15 with ReportCallback

use of com.google.api.ads.admanager.lib.utils.ReportCallback in project googleads-java-lib by googleads.

the class ReportDownloaderTest method testWhenReportReady_remote.

@Test
public void testWhenReportReady_remote() throws IOException, InterruptedException {
    ReportDownloader downloader = new ReportDownloader(reportService, 1);
    RemoteException e = new RemoteException();
    when(reportService.getReportJobStatus(ArgumentMatchers.anyLong())).thenThrow(e);
    ReportCallback callback = mock(ReportCallback.class);
    downloader.whenReportReady(callback).join();
    Mockito.verify(callback).onException(e);
}
Also used : ReportCallback(com.google.api.ads.admanager.lib.utils.ReportCallback) RemoteException(java.rmi.RemoteException) Test(org.junit.Test)

Aggregations

ReportCallback (com.google.api.ads.admanager.lib.utils.ReportCallback)15 Test (org.junit.Test)12 RemoteException (java.rmi.RemoteException)7 ConfigurationLoadException (com.google.api.ads.common.lib.conf.ConfigurationLoadException)3 OAuthException (com.google.api.ads.common.lib.exception.OAuthException)3 ValidationException (com.google.api.ads.common.lib.exception.ValidationException)3 File (java.io.File)3 IOException (java.io.IOException)3 URL (java.net.URL)3 ReportDownloader (com.google.api.ads.admanager.axis.utils.v202108.ReportDownloader)1 ReportDownloader (com.google.api.ads.admanager.axis.utils.v202111.ReportDownloader)1 ReportDownloader (com.google.api.ads.admanager.axis.utils.v202202.ReportDownloader)1 ApiException (com.google.api.ads.admanager.axis.v202108.ApiException)1 ReportDownloadOptions (com.google.api.ads.admanager.axis.v202108.ReportDownloadOptions)1 ReportJob (com.google.api.ads.admanager.axis.v202108.ReportJob)1 ReportQuery (com.google.api.ads.admanager.axis.v202108.ReportQuery)1 ReportServiceInterface (com.google.api.ads.admanager.axis.v202108.ReportServiceInterface)1 ApiException (com.google.api.ads.admanager.axis.v202111.ApiException)1 ReportDownloadOptions (com.google.api.ads.admanager.axis.v202111.ReportDownloadOptions)1 ReportJob (com.google.api.ads.admanager.axis.v202111.ReportJob)1