Skip to content

Commit

Permalink
Fix downloads issue (#26)
Browse files Browse the repository at this point in the history
* Use byte arrays for downloads instead of input streams

* Bump version
  • Loading branch information
ciaran16 authored May 5, 2020
1 parent 121abc5 commit 32bc208
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion onfido-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.onfido</groupId>
<artifactId>onfido-api-java</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>

<name>Onfido API Java Client</name>
<description>Official Java API client library for the Onfido API</description>
Expand Down
6 changes: 2 additions & 4 deletions onfido-java/src/main/java/com/onfido/api/FileDownload.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.onfido.api;

import java.io.InputStream;

/**
* A wrapper class for any file downloaded from the API. Stores an content as an InputStream and
* file type as a String.
*/
public class FileDownload {
/** The content of the downloaded file. */
public final InputStream content;
public final byte[] content;
/** The file type of the associated content. */
public final String contentType;

Expand All @@ -18,7 +16,7 @@ public class FileDownload {
* @param content the content of the file
* @param contentType the file type of the associated content
*/
public FileDownload(InputStream content, String contentType) {
public FileDownload(byte[] content, String contentType) {
this.content = content;
this.contentType = contentType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private String performRequest(Request request) throws OnfidoException {
private FileDownload performDownload(Request request) throws OnfidoException {
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
return new FileDownload(response.body().byteStream(), response.header("content-type"));
return new FileDownload(response.body().bytes(), response.header("content-type"));
} else {
throw ApiException.fromResponseBody(response.body().string(), response.code());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ protected MockWebServer mockRequestResponse(String response) throws IOException
return server;
}

protected MockWebServer mockFileRequestResponse() throws IOException {
Buffer buffer = new Buffer();
buffer.writeInt(5);
protected MockWebServer mockFileRequestResponse(String content, String type) throws IOException {
server = new MockWebServer();
server.enqueue(new MockResponse().setBody(buffer));
server.enqueue(new MockResponse().setBody(content).addHeader("content-type", type));
server.start();

return server;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.onfido.integration;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.onfido.JsonObject;
import com.onfido.Onfido;
import com.onfido.api.FileDownload;
import com.onfido.exceptions.ApiException;
import com.onfido.models.Document;
import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -58,19 +58,20 @@ public void uploadDocument() throws Exception {

@Test
public void downloadDocument() throws Exception {
MockWebServer server = mockFileRequestResponse();
MockWebServer server = mockFileRequestResponse("test", "image/png");

Onfido onfido =
Onfido.builder().apiToken("token").unknownApiUrl(server.url("/").toString()).build();

InputStream inputStream = onfido.document.download("document id").content;
FileDownload download = onfido.document.download("document id");

// Correct path
RecordedRequest request = server.takeRequest();
assertEquals("/documents/document%20id/download", request.getPath());

// Correct response body
assertTrue(inputStream != null);
assertEquals("test", new String(download.content));
assertEquals("image/png", download.contentType);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.onfido.integration;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import com.onfido.JsonObject;
import com.onfido.Onfido;
import com.onfido.api.FileDownload;
import com.onfido.exceptions.ApiException;
import com.onfido.models.LivePhoto;
import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -49,19 +49,20 @@ public void uploadLivePhoto() throws Exception {

@Test
public void downloadLivePhoto() throws Exception {
MockWebServer server = mockFileRequestResponse();
MockWebServer server = mockFileRequestResponse("test", "image/png");

Onfido onfido =
Onfido.builder().apiToken("token").unknownApiUrl(server.url("/").toString()).build();

InputStream inputStream = onfido.livePhoto.download("live photo id").content;
FileDownload download = onfido.livePhoto.download("live photo id");

// Correct path
RecordedRequest request = server.takeRequest();
assertEquals("/live_photos/live%20photo%20id/download", request.getPath());

// Correct response body
assertNotNull(inputStream);
assertEquals("test", new String(download.content));
assertEquals("image/png", download.contentType);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.onfido.integration;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import com.onfido.JsonObject;
import com.onfido.Onfido;
import com.onfido.api.FileDownload;
import com.onfido.exceptions.ApiException;
import com.onfido.models.LiveVideo;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import okhttp3.mockwebserver.MockWebServer;
Expand All @@ -19,36 +18,38 @@ public class LiveVideoManagerTest extends ApiIntegrationTest {

@Test
public void downloadLiveVideo() throws Exception {
MockWebServer server = mockFileRequestResponse();
MockWebServer server = mockFileRequestResponse("test", "video/mp4");

Onfido onfido =
Onfido.builder().apiToken("token").unknownApiUrl(server.url("/").toString()).build();

InputStream inputStream = onfido.liveVideo.download("live video id").content;
FileDownload download = onfido.liveVideo.download("live video id");

// Correct path
RecordedRequest request = server.takeRequest();
assertEquals("/live_videos/live%20video%20id/download", request.getPath());

// Correct response body
assertNotNull(inputStream);
assertEquals("test", new String(download.content));
assertEquals("video/mp4", download.contentType);
}

@Test
public void downloadLiveVideoFrame() throws Exception {
MockWebServer server = mockFileRequestResponse();
MockWebServer server = mockFileRequestResponse("test", "image/png");

Onfido onfido =
Onfido.builder().apiToken("token").unknownApiUrl(server.url("/").toString()).build();

InputStream inputStream = onfido.liveVideo.downloadFrame("live video id").content;
FileDownload download = onfido.liveVideo.downloadFrame("live video id");

// Correct path
RecordedRequest request = server.takeRequest();
assertEquals("/live_videos/live%20video%20id/frame", request.getPath());

// Correct response body
assertNotNull(inputStream);
assertEquals("test", new String(download.content));
assertEquals("image/png", download.contentType);
}

@Test
Expand Down

0 comments on commit 32bc208

Please sign in to comment.