Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.mindee.input;
package com.mindee.parsing;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mindee.MindeeException;
import com.mindee.v2.parsing.CommonResponse;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand All @@ -24,16 +21,15 @@
* A Mindee response saved locally.
*/
@Getter
public class LocalResponse {
private final byte[] file;
private static final ObjectMapper mapper = new ObjectMapper();
public abstract class BaseLocalResponse {
protected final byte[] file;

/**
* Load from an {@link InputStream}.
*
* @param input will be decoded as UTF-8.
*/
public LocalResponse(InputStream input) {
public BaseLocalResponse(InputStream input) {
this.file = this
.getBytes(new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines());
}
Expand All @@ -43,7 +39,10 @@ public LocalResponse(InputStream input) {
*
* @param input will be decoded as UTF-8.
*/
public LocalResponse(String input) {
public BaseLocalResponse(String input) {
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("Input string cannot be empty or null.");
}
this.file = input.getBytes(StandardCharsets.UTF_8);
}

Expand All @@ -52,7 +51,7 @@ public LocalResponse(String input) {
*
* @param input will be decoded as UTF-8.
*/
public LocalResponse(File input) throws IOException {
public BaseLocalResponse(File input) throws IOException {
this.file = this.getBytes(Files.lines(input.toPath(), StandardCharsets.UTF_8));
}

Expand All @@ -61,7 +60,7 @@ public LocalResponse(File input) throws IOException {
*
* @param input will be decoded as UTF-8.
*/
public LocalResponse(Path input) throws IOException {
public BaseLocalResponse(Path input) throws IOException {
this.file = this.getBytes(Files.lines(input, StandardCharsets.UTF_8));
}

Expand Down Expand Up @@ -106,24 +105,4 @@ public String getHmacSignature(String secretKey) {
public boolean isValidHmacSignature(String secretKey, String signature) {
return signature.equals(getHmacSignature(secretKey));
}

/**
* Deserialize this local JSON payload into a specific {@link CommonResponse}
* subtype: {@code InferenceResponse}, {@code JobResponse}.
*
* @param responseClass the concrete class to instantiate
* @param <T> generic {@link CommonResponse}
* @return Either a {@code InferenceResponse} or {@code JobResponse} instance.
* @throws MindeeException if the payload cannot be deserialized into the requested type
*/
public <T extends CommonResponse> T deserializeResponse(Class<T> responseClass) {
ObjectMapper mapper = new ObjectMapper();
try {
T response = mapper.readValue(this.file, responseClass);
response.setRawResponse(new String(this.file, StandardCharsets.UTF_8));
return response;
} catch (Exception ex) {
throw new MindeeException("Invalid class specified for deserialization.", ex);
}
}
}
27 changes: 0 additions & 27 deletions src/main/java/com/mindee/v1/MindeeClient.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.mindee.v1;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mindee.AsyncPollingOptions;
import com.mindee.MindeeException;
import com.mindee.input.InputSourceUtils;
import com.mindee.input.LocalInputSource;
import com.mindee.input.LocalResponse;
import com.mindee.input.PageOptions;
import com.mindee.pdf.PdfBoxApi;
import com.mindee.pdf.PdfOperation;
Expand Down Expand Up @@ -1071,28 +1068,4 @@ public <T extends GeneratedV1> AsyncPredictResponse<T> parseQueued(
) {
return this.mindeeApi.documentQueueGet(type, endpoint, jobId);
}

/**
* Load a local prediction.
* Typically used when wanting to load from a webhook callback.
* However, any kind of Mindee response may be loaded.
*
* @param <T> Type of inference.
* @param type Type of inference.
* @param localResponse A loaded local response.
* @return an instance of {@link AsyncPredictResponse}.
* @throws IOException Throws if the file can't be accessed.
*/
public <T extends Inference> AsyncPredictResponse<T> loadPrediction(
Class<T> type,
LocalResponse localResponse
) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();
JavaType parametricType = objectMapper
.getTypeFactory()
.constructParametricType(AsyncPredictResponse.class, type);
return objectMapper.readValue(localResponse.getFile(), parametricType);
}

}
85 changes: 85 additions & 0 deletions src/main/java/com/mindee/v1/parsing/LocalResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.mindee.v1.parsing;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mindee.MindeeException;
import com.mindee.parsing.BaseLocalResponse;
import com.mindee.v1.parsing.common.AsyncPredictResponse;
import com.mindee.v1.parsing.common.Inference;
import com.mindee.v1.parsing.common.PredictResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;

/**
* A Mindee response saved locally.
*/
public class LocalResponse extends BaseLocalResponse {

public LocalResponse(InputStream input) {
super(input);
}

public LocalResponse(String input) {
super(input);
}

public LocalResponse(File input) throws IOException {
super(input);
}

public LocalResponse(Path input) throws IOException {
super(input);
}

private <P extends Inference, R> R deserialize(
Class<R> responseClass,
Class<P> productClass
) throws IOException {
var mapper = new ObjectMapper().findAndRegisterModules();
var type = mapper.getTypeFactory().constructParametricType(responseClass, productClass);
try {
return mapper.readValue(this.file, type);
} catch (Exception e) {
if (e instanceof JacksonException) {
throw new MindeeException("Invalid JSON payload.", e);
}
throw e;
}
}

/**
* Deserialize this local JSON payload into a specific {@link AsyncPredictResponse}.
* subtype: {@code InferenceResponse}, {@code JobResponse}.
*
* @param productClass the concrete class to instantiate
* @param <T> generic {@link Inference}
* @return A {@link AsyncPredictResponse} instance.
* @throws MindeeException if the payload cannot be deserialized into the requested type
*/
public <T extends Inference> AsyncPredictResponse<T> deserializeAsyncResponse(
Class<T> productClass
) throws IOException {
AsyncPredictResponse<T> response = deserialize(AsyncPredictResponse.class, productClass);
response.setRawResponse(new String(this.file, StandardCharsets.UTF_8));
return response;
}

/**
* Deserialize this local JSON payload into a specific {@link PredictResponse}.
*
* @param productClass the concrete class to instantiate
* @param <T> generic {@link Inference}
* @return A {@link PredictResponse} instance.
* @throws MindeeException if the payload cannot be deserialized into the requested type
*/
public <T extends Inference> PredictResponse<T> deserializeSyncResponse(
Class<T> productClass
) throws IOException {
PredictResponse<T> response = deserialize(PredictResponse.class, productClass);
response.setRawResponse(new String(this.file, StandardCharsets.UTF_8));
return response;
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/mindee/v2/parsing/LocalResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.mindee.v2.parsing;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mindee.MindeeException;
import com.mindee.parsing.BaseLocalResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;

/**
* A Mindee response saved locally.
*/
public class LocalResponse extends BaseLocalResponse {

public LocalResponse(InputStream input) {
super(input);
}

public LocalResponse(String input) {
super(input);
}

public LocalResponse(File input) throws IOException {
super(input);
}

public LocalResponse(Path input) throws IOException {
super(input);
}

/**
* Deserialize this local JSON payload into a specific {@link CommonResponse}
* subtype: {@code InferenceResponse}, {@code JobResponse}.
*
* @param responseClass the concrete class to instantiate
* @param <T> generic {@link CommonResponse}
* @return Either a {@code InferenceResponse} or {@code JobResponse} instance.
* @throws MindeeException if the payload cannot be deserialized into the requested type
*/
public <T extends CommonResponse> T deserializeResponse(Class<T> responseClass) {
var mapper = new ObjectMapper().findAndRegisterModules();
try {
var response = mapper.readValue(this.file, responseClass);
response.setRawResponse(new String(this.file, StandardCharsets.UTF_8));
return response;
} catch (Exception e) {
throw new MindeeException("Invalid JSON payload.", e);
}
}
}
61 changes: 0 additions & 61 deletions src/test/java/com/mindee/input/LocalResponseV1Test.java

This file was deleted.

31 changes: 0 additions & 31 deletions src/test/java/com/mindee/v1/MindeeClientTest.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package com.mindee.v1;

import static com.mindee.TestingUtilities.assertStringEqualsFile;
import static com.mindee.TestingUtilities.getResourcePath;
import static com.mindee.TestingUtilities.getV1ResourcePathString;

import com.mindee.input.LocalInputSource;
import com.mindee.input.LocalResponse;
import com.mindee.input.PageOptions;
import com.mindee.input.PageOptionsOperation;
import com.mindee.v1.clientOptions.PredictOptions;
import com.mindee.v1.parsing.common.AsyncPredictResponse;
import com.mindee.v1.parsing.common.Document;
import com.mindee.v1.parsing.common.Job;
import com.mindee.v1.parsing.common.PredictResponse;
import com.mindee.v1.product.internationalid.InternationalIdV2;
import com.mindee.v1.product.invoice.InvoiceV4;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
Expand Down Expand Up @@ -156,30 +151,4 @@ void givenAnAsyncUrl_whenEnqueued_shouldInvokeApiCorrectly() throws IOException

Assertions.assertEquals("someid", jobId);
}

@Test
void givenJsonInput_whenSync_shouldDeserializeCorrectly() throws IOException {
File file = new File(getV1ResourcePathString("products/invoices/response_v4/complete.json"));
LocalResponse localResponse = new LocalResponse(file);
AsyncPredictResponse<InvoiceV4> predictResponse = new MindeeClient()
.loadPrediction(InvoiceV4.class, localResponse);
assertStringEqualsFile(
predictResponse.getDocumentObj().toString(),
getV1ResourcePathString("/products/invoices/response_v4/summary_full.rst")
);
}

@Test
void givenJsonInput_whenAsync_shouldDeserializeCorrectly() throws IOException {
File file = new File(
getV1ResourcePathString("products/international_id/response_v2/complete.json")
);
LocalResponse localResponse = new LocalResponse(file);
AsyncPredictResponse<InternationalIdV2> predictResponse = new MindeeClient()
.loadPrediction(InternationalIdV2.class, localResponse);
assertStringEqualsFile(
predictResponse.getDocumentObj().toString(),
getV1ResourcePathString("products/international_id/response_v2/summary_full.rst")
);
}
}
Loading
Loading