/** * Writes the contents of the response to the specified File. * * @param outputFile the output file to write to * @throws FileNotFoundException if unable to write to {@code outputFile} * @throws IOException if unable to read the response contents */ public void saveToFile(String outputFile) throws FileNotFoundException, IOException { Streams.copy(getInputStream(), new BufferedOutputStream(new FileOutputStream(outputFile))); }
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); ReportDownloadResponse response = new ReportDownloader(session).downloadReport(reportDefinition); response.saveToFile(reportFileName);
/** * Returns the contents of the response as a String. Inflates the response if it is in one of the * gzip formats. * * @throws IOException if unable to read the response contents */ public String getAsString() throws IOException { InputStream inputStream = isGzipped() ? new GZIPInputStream(getInputStream()) : getInputStream(); return Streams.readAll(inputStream, rawResponse.getCharset()); }
@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()); }
/** * Test method for * {@link com.google.api.ads.adwords.lib.utils.ReportDownloadResponse#getInputStream()}. */ @Test public void testGetInputStream() throws IOException { String expectedContents = "a,b,c\nd,e,f\n"; InputStream inputStream = new ByteArrayInputStream(expectedContents.getBytes(UTF_8)); RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(HttpURLConnection.HTTP_OK, inputStream, UTF_8, "CSV"); ReportDownloadResponse response = new ReportDownloadResponse(rawResponse); String actualContents = Streams.readAll(response.getInputStream(), UTF_8); assertEquals("input stream contents are incorrect", expectedContents, actualContents); }
ReportDownloadResponse response = helper.downloadReport(reportRequest, exceptionBuilder); assertNotNull("Null response", response); assertEquals("Response status code not success", 200, response.getHttpStatus()); assertEquals( "Response contents incorrect", "test", Streams.readAll(response.getInputStream(), REPORT_CHARSET));
/** * Test method for * {@link com.google.api.ads.adwords.lib.utils.ReportDownloadResponse#saveToFile(String)} */ @Test public void testSaveToFile() throws IOException { File outputFile = tempFolder.newFile("ReportOutput.csv"); String expectedContents = "a,b,c\nd,e,f\n"; InputStream inputStream = new ByteArrayInputStream(expectedContents.getBytes(UTF_8)); RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(HttpURLConnection.HTTP_OK, inputStream, UTF_8, "CSV"); ReportDownloadResponse response = new ReportDownloadResponse(rawResponse); response.saveToFile(outputFile.getPath()); assertEquals("contents saved to file are incorrect", expectedContents, Streams.readAll(new FileInputStream(outputFile), UTF_8)); }
@Test public void testFailedResponse_fails() { RawReportDownloadResponse rawResponse = new RawReportDownloadResponse( HttpURLConnection.HTTP_BAD_REQUEST, new ByteArrayInputStream("failed".getBytes(UTF_8)), UTF_8, "CSV"); thrown.expect(IllegalArgumentException.class); new ReportDownloadResponse(rawResponse); } }
/** * Test method for * {@link com.google.api.ads.adwords.lib.utils.ReportDownloadResponse#getAsString()}. */ @Test public void testGetAsString() throws IOException { String expectedContents = "a,b,c\nd,e,f\n"; InputStream inputStream = new ByteArrayInputStream(expectedContents.getBytes(UTF_8)); RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(HttpURLConnection.HTTP_OK, inputStream, UTF_8, "CSV"); ReportDownloadResponse response = new ReportDownloadResponse(rawResponse); assertEquals("contents as string are incorrect", expectedContents, response.getAsString()); }
@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()); }
ReportDownloadResponse response = helper.downloadReport(reportRequest, exceptionBuilder); assertNotNull("Null response", response); assertEquals("Response status code not success", 200, response.getHttpStatus()); assertEquals( "Response contents incorrect", "test", Streams.readAll(response.getInputStream(), REPORT_CHARSET));
Preconditions.checkNotNull(exceptionBuilder, "Null exception builder"); if (rawResponse.getHttpStatus() == HttpURLConnection.HTTP_OK) { return new ReportDownloadResponse(rawResponse);
/** * Test method for * {@link com.google.api.ads.adwords.lib.utils.ReportDownloadResponse#getAsString()} * when response data is compressed. */ @Test public void testGetAsString_gzippedCSV() throws IOException { String expectedContents = "a,b,c\nd,e,f\n"; ByteArrayOutputStream zippedBytesOut = new ByteArrayOutputStream(); Streams.copy(new ByteArrayInputStream(expectedContents.getBytes(UTF_8)), new GZIPOutputStream(zippedBytesOut)); RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(HttpURLConnection.HTTP_OK, new ByteArrayInputStream(zippedBytesOut.toByteArray()), UTF_8, "GZIPPED_CSV"); ReportDownloadResponse response = new ReportDownloadResponse(rawResponse); assertEquals("contents as string are incorrect for gzipped format", expectedContents, response.getAsString()); }
@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()); }
/** * Writes the contents of the response to the specified File. * * @param outputFile the output file to write to * @throws FileNotFoundException if unable to write to {@code outputFile} * @throws IOException if unable to read the response contents */ public void saveToFile(String outputFile) throws FileNotFoundException, IOException { Streams.copy(getInputStream(), new BufferedOutputStream(new FileOutputStream(outputFile))); }
/** * Returns the contents of the response as a String. Inflates the response if it is in one of the * gzip formats. * * @throws IOException if unable to read the response contents */ public String getAsString() throws IOException { InputStream inputStream = isGzipped() ? new GZIPInputStream(getInputStream()) : getInputStream(); return Streams.readAll(inputStream, rawResponse.getCharset()); }
Preconditions.checkNotNull(exceptionBuilder, "Null exception builder"); if (rawResponse.getHttpStatus() == HttpURLConnection.HTTP_OK) { return new ReportDownloadResponse(rawResponse);
/** * Downloads report from API (with retry logic) and returns a {@link File} object. */ @Override public File call() throws ReportProcessingException { ReportDownloadResponse reportDownloadResponse = getReportDownloadResponse(); InputStream inputStream = reportDownloadResponse.getInputStream(); return handleReportStreamResult(inputStream); }
adHocDownloadHelper.downloadReport( Matchers.any(ReportRequest.class), Matchers.any(Builder.class))) .thenReturn(new ReportDownloadResponse(rawResponse)); } else {
/** * Downloads the file from the API into an InputStream. * * @return the InputStream from the online report, null if httpStatus is not {@code HTTP_OK}. */ private InputStream getReportInputStream() { ReportDownloaderInterface reportDownloader = AdWordsServicesUtil.getUtility(session, ReportDownloaderInterface.class); InputStream result = null; try { ReportDownloadResponse reportDownloadResponse = reportDownloader.downloadReport(reportDefinition); result = reportDownloadResponse.getInputStream(); } catch (ReportException | ReportDownloadResponseException e) { logger.error( "Failed to download report stream for {} with account {}.", reportDefinition.getReportType(), session.getClientCustomerId(), e); } return result; }