Skip to content

Commit 86bf8d7

Browse files
switch response deserialization to out-of-client syntax
1 parent ace3274 commit 86bf8d7

File tree

5 files changed

+32
-19
lines changed

5 files changed

+32
-19
lines changed

.github/workflows/_test-code-samples.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ jobs:
3434

3535
- name: Tests sample code
3636
run: |
37-
./tests/test_code_samples.sh ${{ secrets.MINDEE_ACCOUNT_SE_TESTS }} ${{ secrets.MINDEE_ENDPOINT_SE_TESTS }} ${{ secrets.MINDEE_API_KEY_SE_TESTS }} ${{ secrets.MINDEE_V2_SE_TESTS_API_KEY }} ${{ secrets.MINDEE_V2_SE_TESTS_FINDOC_MODEL_ID }}
37+
./tests/test_code_samples.sh ${{ secrets.MINDEE_ACCOUNT_SE_TESTS }} ${{ secrets.MINDEE_ENDPOINT_SE_TESTS }} ${{ secrets.MINDEE_API_KEY_SE_TESTS }} ${{ secrets.MINDEE_V2_SE_TESTS_API_KEY }} ${{ secrets.MINDEE_V2_SE_TESTS_FINDOC_MODEL_ID }}

src/main/java/com/mindee/MindeeClientV2.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,6 @@ else if (resp.getJob().getStatus().equals("Processed")) {
103103
throw new RuntimeException("Max retries exceeded (" + max + ").");
104104
}
105105

106-
/**
107-
* Deserialize a webhook payload (or any saved response) into an
108-
* {@link InferenceResponse}.
109-
*/
110-
public InferenceResponse loadInference(LocalResponse localResponse) throws IOException {
111-
ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
112-
InferenceResponse model =
113-
mapper.readValue(localResponse.getFile(), InferenceResponse.class);
114-
model.setRawResponse(localResponse.toString());
115-
return model;
116-
}
117-
118106
private static MindeeApiV2 createDefaultApiV2(String apiKey) {
119107
MindeeSettingsV2 settings = apiKey == null || apiKey.trim().isEmpty()
120108
? new MindeeSettingsV2()

src/main/java/com/mindee/input/LocalResponse.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
import java.util.stream.Stream;
1414
import javax.crypto.Mac;
1515
import javax.crypto.spec.SecretKeySpec;
16+
17+
import com.fasterxml.jackson.databind.ObjectMapper;
18+
import com.mindee.MindeeException;
19+
import com.mindee.parsing.v2.CommonResponse;
1620
import lombok.Getter;
1721
import org.apache.commons.codec.binary.Hex;
1822

@@ -91,4 +95,26 @@ public String getHmacSignature(String secretKey) {
9195
public boolean isValidHmacSignature(String secretKey, String signature) {
9296
return signature.equals(getHmacSignature(secretKey));
9397
}
98+
99+
100+
/**
101+
* Deserialize this local JSON payload into a specific {@link CommonResponse}
102+
* subtype: {@code InferenceResponse}, {@code JobResponse}.
103+
*
104+
* @param responseClass the concrete class to instantiate
105+
* @param <T> generic {@link CommonResponse}
106+
* @return a fully populated instance of {@code responseClass}
107+
* @throws MindeeException if the payload cannot be deserialized into the
108+
* requested type
109+
*/
110+
public <T extends CommonResponse> T deserializeResponse(Class<T> responseClass) {
111+
ObjectMapper mapper = new ObjectMapper();
112+
try {
113+
T response = mapper.readValue(this.file, responseClass);
114+
response.setRawResponse(new String(this.file, StandardCharsets.UTF_8));
115+
return response;
116+
} catch (Exception ex) {
117+
throw new MindeeException("Invalid class specified for deserialization.", ex);
118+
}
119+
}
94120
}

src/test/java/com/mindee/MindeeClientV2Test.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,17 @@ void document_getInference_async() throws IOException {
114114
}
115115

116116
@Nested
117-
@DisplayName("loadInference()")
118-
class LoadInference {
117+
@DisplayName("deserializeResponse()")
118+
class DeserializeResponse {
119119

120120
@Test
121121
@DisplayName("parses local JSON and exposes correct field values")
122122
void inference_loadsLocally() throws IOException {
123-
MindeeClientV2 mindeeClient = new MindeeClientV2("dummy");
124123
File jsonFile =
125124
new File("src/test/resources/v2/products/financial_document/complete.json");
126125
LocalResponse localResponse = new LocalResponse(jsonFile);
127126

128-
InferenceResponse loaded = mindeeClient.loadInference(localResponse);
127+
InferenceResponse loaded = localResponse.deserializeResponse(InferenceResponse.class);
129128

130129
assertNotNull(loaded, "Loaded InferenceResponse must not be null");
131130
assertEquals(

src/test/java/com/mindee/parsing/v2/InferenceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
class InferenceTest {
2020

2121
private InferenceResponse loadFromResource(String resourcePath) throws IOException {
22-
MindeeClientV2 dummyClient = new MindeeClientV2("dummy");
23-
return dummyClient.loadInference(new LocalResponse(InferenceTest.class.getClassLoader().getResourceAsStream(resourcePath)));
22+
LocalResponse localResponse = new LocalResponse(InferenceTest.class.getClassLoader().getResourceAsStream(resourcePath));
23+
return localResponse.deserializeResponse(InferenceResponse.class);
2424
}
2525

2626
private String readFileAsString(String path)

0 commit comments

Comments
 (0)