com.google.api.ads.adwords.lib.jaxb.v201809.DownloadFormat Java Examples
The following examples show how to use
com.google.api.ads.adwords.lib.jaxb.v201809.DownloadFormat.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReportProcessor.java From aw-reporting with Apache License 2.0 | 6 votes |
/** * Instantiates the report definition, setting all the correct formats. * * @param reportDefinitionReportType the report definition type. * @param dateRangeType the date range type. * @param selector the selector containing the report properties. * @return the {@code ReportDefinition} */ protected ReportDefinition instantiateReportDefinition( ReportDefinitionReportType reportDefinitionReportType, ReportDefinitionDateRangeType dateRangeType, Selector selector) { // Create the Report Definition ReportDefinition reportDefinition = new ReportDefinition(); reportDefinition.setReportName( REPORT_PREFIX + reportDefinitionReportType.value() + " " + System.currentTimeMillis()); reportDefinition.setDateRangeType(dateRangeType); reportDefinition.setReportType(reportDefinitionReportType); reportDefinition.setDownloadFormat(DownloadFormat.GZIPPED_CSV); reportDefinition.setSelector(selector); return reportDefinition; }
Example #2
Source File: ReportDownloaderTest.java From googleads-java-lib with Apache License 2.0 | 6 votes |
@Test public void testFailure_failedStatusCode() throws Exception { InputStream stream = new ByteArrayInputStream(ERROR_TEXT.getBytes(AdHocReportDownloadHelper.REPORT_CHARSET)); int statusCode = 400; RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(statusCode, stream, AdHocReportDownloadHelper.REPORT_CHARSET, DownloadFormat.XML.name()); try { downloadReport(DownloadFormat.XML, rawResponse, ERROR_TEXT); fail("Should have thrown an exception"); } catch (DetailedReportDownloadResponseException e) { assertEquals(statusCode, e.getHttpStatus()); assertEquals(ERROR_TEXT, e.getErrorText()); assertThat(e.toString(), org.hamcrest.Matchers.containsString(String.valueOf(statusCode))); assertThat(e.toString(), org.hamcrest.Matchers.containsString(ERROR_TEXT)); } }
Example #3
Source File: CallableAwqlReportDownloader.java From adwords-alerting with Apache License 2.0 | 6 votes |
/** * Downloads report from API (with retry logic) and transforms the result into a * {@link ReportData} object. */ @Override public ReportData call() throws AlertProcessingException { ReportDownloaderInterface reportDownloader = AdWordsServicesUtil.getUtility(session, ReportDownloaderInterface.class); ReportDownloadResponse reportDownloadResponse = null; try { reportDownloadResponse = reportDownloader.downloadReport(reportQuery.generateAWQL(), DownloadFormat.GZIPPED_CSV); } catch (ReportException | ReportDownloadResponseException e) { String msg = "Failed to download report account " + session.getClientCustomerId() + "."; LOGGER.error(msg, e); throw new AlertProcessingException(msg, e); } InputStream inputStream = reportDownloadResponse.getInputStream(); return handleReportStreamResult(inputStream); }
Example #4
Source File: ReportDownloaderTest.java From googleads-java-lib with Apache License 2.0 | 5 votes |
@Test public void testSuccess() throws Exception { ByteArrayInputStream stream = new ByteArrayInputStream("Report data".getBytes(AdHocReportDownloadHelper.REPORT_CHARSET)); RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(200, stream, AdHocReportDownloadHelper.REPORT_CHARSET, DownloadFormat.CSV.name()); ReportDownloadResponse response = downloadReport(DownloadFormat.CSV, rawResponse, null); assertEquals(200, response.getHttpStatus()); assertEquals(stream, response.getInputStream()); assertEquals("SUCCESS", response.getHttpResponseMessage()); }
Example #5
Source File: ReportDownloaderTest.java From googleads-java-lib with Apache License 2.0 | 5 votes |
@Test public void testFailure_cannotReadStream() throws Exception { InputStream stream = Mockito.mock(InputStream.class); when(stream.read(isA(byte[].class))).thenThrow(new IOException()); RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(400, stream, AdHocReportDownloadHelper.REPORT_CHARSET, DownloadFormat.CSV.name()); try { downloadReport(DownloadFormat.CSV, rawResponse, null); fail("Should have thrown an exception"); } catch (ReportDownloadResponseException e) { assertEquals(400, e.getHttpStatus()); } }
Example #6
Source File: ReportDownloaderTest.java From googleads-java-lib with Apache License 2.0 | 5 votes |
/** * Invokes {@link ReportDownloader#downloadReport(ReportDefinition)} or * {@link ReportDownloader#downloadReport(String, DownloadFormat)}, depending on whether this * instance is configured to use AWQL. * * @param downloadFormat the DownloadFormat for the request * @param rawResponse the response to return from the mocked ad hoc helper */ private ReportDownloadResponse downloadReport(DownloadFormat downloadFormat, RawReportDownloadResponse rawResponse, String expectedErrorText) throws ReportException, ReportDownloadResponseException { if (rawResponse.getHttpStatus() == 200) { // Response indicates success, so return a ReportDownloadResponse. when( adHocDownloadHelper.downloadReport( Matchers.any(ReportRequest.class), Matchers.any(Builder.class))) .thenReturn(new ReportDownloadResponse(rawResponse)); } else { // Response indicates failure, so throw an exception. when( adHocDownloadHelper.downloadReport( Matchers.any(ReportRequest.class), Matchers.any(Builder.class))) .thenThrow(new Builder().build(rawResponse.getHttpStatus(), expectedErrorText)); } if (isUseAwql) { return reportDownloader.downloadReport(AWQL_REQUEST, downloadFormat); } else { ReportDefinition reportDefinition = new ReportDefinition(); reportDefinition.setSelector(new Selector()); reportDefinition .getSelector() .getFields() .addAll(Arrays.asList("CampaignId", "CampaignName", "Impressions")); reportDefinition.setDateRangeType(ReportDefinitionDateRangeType.LAST_7_DAYS); reportDefinition.setReportName("Custom report"); reportDefinition.setReportType(ReportDefinitionReportType.CAMPAIGN_PERFORMANCE_REPORT); return reportDownloader.downloadReport(reportDefinition); } }
Example #7
Source File: XmlReportDefinitionRequestTest.java From googleads-java-lib with Apache License 2.0 | 5 votes |
@Test public void testGetRequestType() { ReportDefinition reportDefinition = new ReportDefinition(); reportDefinition.setDownloadFormat(DownloadFormat.GZIPPED_CSV); XmlReportDefinitionRequest request = new XmlReportDefinitionRequest(reportDefinition); assertEquals("Incorrect request type", RequestType.XML, request.getRequestType()); assertEquals( "Incorrect download format", DownloadFormat.GZIPPED_CSV, request.getDownloadFormat()); }
Example #8
Source File: ReportDownloader.java From googleads-java-lib with Apache License 2.0 | 5 votes |
@Override public ReportDownloadResponse downloadReport(String reportQuery, DownloadFormat format) throws ReportException, ReportDownloadResponseException { return adHocReportDownloadHelper.downloadReport( new AwqlReportRequest(reportQuery, format), new DetailedReportDownloadResponseException.Builder()); }
Example #9
Source File: AwqlReportRequest.java From googleads-java-lib with Apache License 2.0 | 4 votes |
@Override public DownloadFormat getDownloadFormat() { return downloadFormat; }
Example #10
Source File: AwqlReportRequest.java From googleads-java-lib with Apache License 2.0 | 4 votes |
public AwqlReportRequest(String awqlQuery, DownloadFormat downloadFormat) { this.awqlQuery = Preconditions.checkNotNull(awqlQuery, "Null AWQL query"); this.downloadFormat = Preconditions.checkNotNull(downloadFormat, "Null download format"); }
Example #11
Source File: XmlReportDefinitionRequest.java From googleads-java-lib with Apache License 2.0 | 4 votes |
@Override public DownloadFormat getDownloadFormat() { return reportDefinition.getDownloadFormat(); }
Example #12
Source File: AwqlReportRequestTest.java From googleads-java-lib with Apache License 2.0 | 4 votes |
@Test public void testConstructor_nullQuery_fails() { thrown.expect(NullPointerException.class); thrown.expectMessage("query"); new AwqlReportRequest(null, DownloadFormat.CSV); }
Example #13
Source File: AwqlReportRequestTest.java From googleads-java-lib with Apache License 2.0 | 4 votes |
@Test public void testGetRequestType() { AwqlReportRequest request = new AwqlReportRequest("SELECT foo, bar", DownloadFormat.CSV); assertEquals("Incorrect request type", RequestType.AWQL, request.getRequestType()); }
Example #14
Source File: DownloadCriteriaReportWithAwql.java From googleads-java-lib with Apache License 2.0 | 4 votes |
/** * Runs the example. * * @param adWordsServices the services factory. * @param session the session. * @param reportFile the output file for the report contents. * @throws DetailedReportDownloadResponseException if the report request failed with a detailed * error from the reporting service. * @throws ReportDownloadResponseException if the report request failed with a general error from * the reporting service. * @throws ReportException if the report request failed due to a transport layer error. * @throws IOException if the report's contents could not be written to {@code reportFile}. */ public static void runExample( AdWordsServicesInterface adWordsServices, AdWordsSession session, String reportFile) throws ReportDownloadResponseException, ReportException, IOException { // Create query. ReportQuery query = new ReportQuery.Builder() .fields( "CampaignId", "AdGroupId", "Id", "Criteria", "CriteriaType", "Impressions", "Clicks", "Cost") .from(ReportDefinitionReportType.CRITERIA_PERFORMANCE_REPORT) .where("Status").in("ENABLED", "PAUSED") .during(ReportDefinitionDateRangeType.LAST_7_DAYS) .build(); // Optional: Set the reporting configuration of the session to suppress header, column name, or // summary rows in the report output. You can also configure this via your ads.properties // configuration file. See AdWordsSession.Builder.from(Configuration) for details. // In addition, you can set whether you want to explicitly include or exclude zero impression // rows. ReportingConfiguration reportingConfiguration = new ReportingConfiguration.Builder() .skipReportHeader(false) .skipColumnHeader(false) .skipReportSummary(false) // Set to false to exclude rows with zero impressions. .includeZeroImpressions(true) .build(); session.setReportingConfiguration(reportingConfiguration); ReportDownloaderInterface reportDownloader = adWordsServices.getUtility(session, ReportDownloaderInterface.class); // Set the property api.adwords.reportDownloadTimeout or call // ReportDownloader.setReportDownloadTimeout to set a timeout (in milliseconds) // for CONNECT and READ in report downloads. ReportDownloadResponse response = reportDownloader.downloadReport(query.toString(), DownloadFormat.CSV); response.saveToFile(reportFile); System.out.printf("Report successfully downloaded to: %s%n", reportFile); }
Example #15
Source File: DownloadCriteriaReportWithSelector.java From googleads-java-lib with Apache License 2.0 | 4 votes |
/** * Runs the example. * * @param adWordsServices the services factory. * @param session the session. * @param reportFile the output file for the report contents. * @throws DetailedReportDownloadResponseException if the report request failed with a detailed * error from the reporting service. * @throws ReportDownloadResponseException if the report request failed with a general error from * the reporting service. * @throws ReportException if the report request failed due to a transport layer error. * @throws IOException if the report's contents could not be written to {@code reportFile}. */ public static void runExample( AdWordsServicesInterface adWordsServices, AdWordsSession session, String reportFile) throws ReportDownloadResponseException, ReportException, IOException { // Create selector. Selector selector = new Selector(); selector.getFields().addAll(Arrays.asList("CampaignId", "AdGroupId", "Id", "CriteriaType", "Criteria", "FinalUrls", "Impressions", "Clicks", "Cost")); // Create report definition. ReportDefinition reportDefinition = new ReportDefinition(); reportDefinition.setReportName("Criteria performance report #" + System.currentTimeMillis()); reportDefinition.setDateRangeType(ReportDefinitionDateRangeType.YESTERDAY); reportDefinition.setReportType(ReportDefinitionReportType.CRITERIA_PERFORMANCE_REPORT); reportDefinition.setDownloadFormat(DownloadFormat.CSV); // Optional: Set the reporting configuration of the session to suppress header, column name, or // summary rows in the report output. You can also configure this via your ads.properties // configuration file. See AdWordsSession.Builder.from(Configuration) for details. // In addition, you can set whether you want to explicitly include or exclude zero impression // rows. ReportingConfiguration reportingConfiguration = new ReportingConfiguration.Builder() .skipReportHeader(false) .skipColumnHeader(false) .skipReportSummary(false) // Enable to allow rows with zero impressions to show. .includeZeroImpressions(false) .build(); session.setReportingConfiguration(reportingConfiguration); reportDefinition.setSelector(selector); ReportDownloaderInterface reportDownloader = adWordsServices.getUtility(session, ReportDownloaderInterface.class); // Set the property api.adwords.reportDownloadTimeout or call // ReportDownloader.setReportDownloadTimeout to set a timeout (in milliseconds) // for CONNECT and READ in report downloads. ReportDownloadResponse response = reportDownloader.downloadReport(reportDefinition); response.saveToFile(reportFile); System.out.printf("Report successfully downloaded to: %s%n", reportFile); }
Example #16
Source File: AdvancedCreateCredentialFromScratch.java From googleads-java-lib with Apache License 2.0 | 4 votes |
/** * Runs the example. * * @param adWordsServices the services factory. * @param session the session. * @param reportFile the output file for the report contents. * @throws DetailedReportDownloadResponseException if the report request failed with a detailed * error from the reporting service. * @throws ReportDownloadResponseException if the report request failed with a general error from * the reporting service. * @throws ReportException if the report request failed due to a transport layer error. * @throws IOException if the report's contents could not be written to {@code reportFile}. */ public static void runExample( AdWordsServicesInterface adWordsServices, AdWordsSession session, String reportFile) throws ReportDownloadResponseException, ReportException, IOException { // Get the CampaignService. CampaignServiceInterface campaignService = adWordsServices.get(session, CampaignServiceInterface.class); // Create selector to retrieve the first 100 campaigns. Selector selector = new Selector(); selector.setFields(new String[] {"Id", "Name"}); Paging paging = new Paging(); paging.setStartIndex(0); paging.setNumberResults(100); // Get the first page of campaigns. CampaignPage page = campaignService.get(selector); System.out.printf("Found %d total campaigns.%n", page.getTotalNumEntries()); // Display campaigns. if (page.getEntries() != null) { for (Campaign campaign : page.getEntries()) { System.out.printf("Campaign with name '%s' and ID %d was found.%n", campaign.getName(), campaign.getId()); } } else { System.out.println("No campaigns were found."); } // Create selector. com.google.api.ads.adwords.lib.jaxb.v201809.Selector reportSelector = new com.google.api.ads.adwords.lib.jaxb.v201809.Selector(); reportSelector.getFields().addAll(Arrays.asList( "CampaignId", "AdGroupId", "Id", "CriteriaType", "Criteria", "Impressions", "Clicks", "Cost")); // Create report definition. ReportDefinition reportDefinition = new ReportDefinition(); reportDefinition.setReportName("Criteria performance report #" + System.currentTimeMillis()); reportDefinition.setDateRangeType(ReportDefinitionDateRangeType.YESTERDAY); reportDefinition.setReportType(ReportDefinitionReportType.CRITERIA_PERFORMANCE_REPORT); reportDefinition.setDownloadFormat(DownloadFormat.CSV); reportDefinition.setSelector(reportSelector); ReportingConfiguration reportingConfig = new ReportingConfiguration.Builder() // Enable to allow rows with zero impressions to show. .includeZeroImpressions(false) .build(); session.setReportingConfiguration(reportingConfig); ReportDownloadResponse response = new ReportDownloader(session).downloadReport(reportDefinition); FileOutputStream fos = new FileOutputStream(new File(reportFile)); Streams.copy(response.getInputStream(), fos); fos.close(); System.out.printf("Report successfully downloaded: %s%n", reportFile); }
Example #17
Source File: ReportDownloaderInterface.java From googleads-java-lib with Apache License 2.0 | 2 votes |
/** * Downloads a report query (AWQL) and returns a ReportDownloadResponse with the results. * * @param reportQuery to download a report for. * @param format Format to download the report as. CSV, * @return {@link ReportDownloadResponse} If the HTTP request completes successfully. * @throws ReportException If there is any issue making HTTP request with server. * @throws ReportDownloadResponseException If the server indicates a problem with the request. */ public ReportDownloadResponse downloadReport(String reportQuery, DownloadFormat format) throws ReportException, ReportDownloadResponseException;