Skip to content

Commit a1aba43

Browse files
committed
♻️ 💥 LocalResponse class should be version-specific
1 parent ec21193 commit a1aba43

13 files changed

Lines changed: 170 additions & 51 deletions

File tree

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

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.mindee.input;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import com.mindee.MindeeException;
5-
import com.mindee.v2.parsing.CommonResponse;
63
import java.io.BufferedReader;
74
import java.io.File;
85
import java.io.IOException;
@@ -24,9 +21,8 @@
2421
* A Mindee response saved locally.
2522
*/
2623
@Getter
27-
public class LocalResponse {
28-
private final byte[] file;
29-
private static final ObjectMapper mapper = new ObjectMapper();
24+
public abstract class LocalResponse {
25+
protected final byte[] file;
3026

3127
/**
3228
* Load from an {@link InputStream}.
@@ -106,24 +102,4 @@ public String getHmacSignature(String secretKey) {
106102
public boolean isValidHmacSignature(String secretKey, String signature) {
107103
return signature.equals(getHmacSignature(secretKey));
108104
}
109-
110-
/**
111-
* Deserialize this local JSON payload into a specific {@link CommonResponse}
112-
* subtype: {@code InferenceResponse}, {@code JobResponse}.
113-
*
114-
* @param responseClass the concrete class to instantiate
115-
* @param <T> generic {@link CommonResponse}
116-
* @return Either a {@code InferenceResponse} or {@code JobResponse} instance.
117-
* @throws MindeeException if the payload cannot be deserialized into the requested type
118-
*/
119-
public <T extends CommonResponse> T deserializeResponse(Class<T> responseClass) {
120-
ObjectMapper mapper = new ObjectMapper();
121-
try {
122-
T response = mapper.readValue(this.file, responseClass);
123-
response.setRawResponse(new String(this.file, StandardCharsets.UTF_8));
124-
return response;
125-
} catch (Exception ex) {
126-
throw new MindeeException("Invalid class specified for deserialization.", ex);
127-
}
128-
}
129105
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.mindee.v1.input;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.mindee.MindeeException;
5+
import com.mindee.v1.parsing.common.AsyncPredictResponse;
6+
import com.mindee.v1.parsing.common.Inference;
7+
import com.mindee.v1.parsing.common.PredictResponse;
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.nio.charset.StandardCharsets;
12+
import java.nio.file.Path;
13+
14+
/**
15+
* A Mindee response saved locally.
16+
*/
17+
public class LocalResponse extends com.mindee.input.LocalResponse {
18+
19+
public LocalResponse(InputStream input) {
20+
super(input);
21+
}
22+
23+
public LocalResponse(String input) {
24+
super(input);
25+
}
26+
27+
public LocalResponse(File input) throws IOException {
28+
super(input);
29+
}
30+
31+
public LocalResponse(Path input) throws IOException {
32+
super(input);
33+
}
34+
35+
/**
36+
* Deserialize this local JSON payload into a specific {@link AsyncPredictResponse}.
37+
* subtype: {@code InferenceResponse}, {@code JobResponse}.
38+
*
39+
* @param responseClass the concrete class to instantiate
40+
* @param <T> generic {@link Inference}
41+
* @return A {@link AsyncPredictResponse} instance.
42+
* @throws MindeeException if the payload cannot be deserialized into the requested type
43+
*/
44+
public <T extends Inference> AsyncPredictResponse<T> deserializeAsyncResponse(
45+
Class<T> responseClass
46+
) {
47+
var objectMapper = new ObjectMapper();
48+
objectMapper.findAndRegisterModules();
49+
var type = objectMapper
50+
.getTypeFactory()
51+
.constructParametricType(AsyncPredictResponse.class, responseClass);
52+
try {
53+
AsyncPredictResponse<T> response = objectMapper.readValue(this.file, type);
54+
response.setRawResponse(new String(this.file, StandardCharsets.UTF_8));
55+
return response;
56+
} catch (Exception ex) {
57+
throw new MindeeException("Invalid class specified for deserialization.", ex);
58+
}
59+
}
60+
61+
/**
62+
* Deserialize this local JSON payload into a specific {@link PredictResponse}.
63+
*
64+
* @param responseClass the concrete class to instantiate
65+
* @param <T> generic {@link Inference}
66+
* @return A {@link PredictResponse} instance.
67+
* @throws MindeeException if the payload cannot be deserialized into the requested type
68+
*/
69+
public <T extends Inference> PredictResponse<T> deserializeSyncResponse(Class<T> responseClass) {
70+
var objectMapper = new ObjectMapper();
71+
objectMapper.findAndRegisterModules();
72+
var type = objectMapper
73+
.getTypeFactory()
74+
.constructParametricType(PredictResponse.class, responseClass);
75+
try {
76+
PredictResponse<T> response = objectMapper.readValue(this.file, type);
77+
response.setRawResponse(new String(this.file, StandardCharsets.UTF_8));
78+
return response;
79+
} catch (Exception ex) {
80+
throw new MindeeException("Invalid class specified for deserialization.", ex);
81+
}
82+
}
83+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.mindee.v2.input;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.mindee.MindeeException;
5+
import com.mindee.v2.parsing.CommonResponse;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.nio.charset.StandardCharsets;
10+
import java.nio.file.Path;
11+
12+
/**
13+
* A Mindee response saved locally.
14+
*/
15+
public class LocalResponse extends com.mindee.input.LocalResponse {
16+
17+
public LocalResponse(InputStream input) {
18+
super(input);
19+
}
20+
21+
public LocalResponse(String input) {
22+
super(input);
23+
}
24+
25+
public LocalResponse(File input) throws IOException {
26+
super(input);
27+
}
28+
29+
public LocalResponse(Path input) throws IOException {
30+
super(input);
31+
}
32+
33+
/**
34+
* Deserialize this local JSON payload into a specific {@link CommonResponse}
35+
* subtype: {@code InferenceResponse}, {@code JobResponse}.
36+
*
37+
* @param responseClass the concrete class to instantiate
38+
* @param <T> generic {@link CommonResponse}
39+
* @return Either a {@code InferenceResponse} or {@code JobResponse} instance.
40+
* @throws MindeeException if the payload cannot be deserialized into the requested type
41+
*/
42+
public <T extends CommonResponse> T deserializeResponse(Class<T> responseClass) {
43+
var mapper = new ObjectMapper();
44+
try {
45+
var response = mapper.readValue(this.file, responseClass);
46+
response.setRawResponse(new String(this.file, StandardCharsets.UTF_8));
47+
return response;
48+
} catch (Exception ex) {
49+
throw new MindeeException("Invalid class specified for deserialization.", ex);
50+
}
51+
}
52+
}

src/test/java/com/mindee/v1/MindeeClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import static com.mindee.TestingUtilities.getV1ResourcePathString;
66

77
import com.mindee.input.LocalInputSource;
8-
import com.mindee.input.LocalResponse;
98
import com.mindee.input.PageOptions;
109
import com.mindee.input.PageOptionsOperation;
1110
import com.mindee.v1.clientOptions.PredictOptions;
11+
import com.mindee.v1.input.LocalResponse;
1212
import com.mindee.v1.parsing.common.AsyncPredictResponse;
1313
import com.mindee.v1.parsing.common.Document;
1414
import com.mindee.v1.parsing.common.Job;

src/test/java/com/mindee/input/LocalResponseV1Test.java renamed to src/test/java/com/mindee/v1/input/LocalResponseTest.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,50 @@
1-
package com.mindee.input;
1+
package com.mindee.v1.input;
22

33
import static com.mindee.TestingUtilities.getV1ResourcePath;
44

5+
import com.mindee.v1.product.internationalid.InternationalIdV2;
56
import java.io.File;
67
import java.io.IOException;
78
import java.nio.file.Files;
89
import java.nio.file.Path;
910
import org.junit.jupiter.api.Assertions;
1011
import org.junit.jupiter.api.Test;
1112

12-
public class LocalResponseV1Test {
13+
public class LocalResponseTest {
1314
/**
1415
* Fake secret key.
1516
*/
1617
String secretKey = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH";
1718

1819
/**
19-
* Real signature using fake secret key.
20+
* Real signature using a fake secret key.
2021
*/
2122
String signature = "5ed1673e34421217a5dbfcad905ee62261a3dd66c442f3edd19302072bbf70d0";
2223

2324
/**
24-
* File which the signature applies to.
25+
* File that the signature applies to.
2526
*/
2627
Path filePath = getV1ResourcePath("async/get_completed_empty.json");
2728

2829
@Test
2930
void loadDocument_withFile_mustReturnValidLocalResponse() throws IOException {
30-
LocalResponse localResponse = new LocalResponse(new File(this.filePath.toString()));
31+
var localResponse = new LocalResponse(new File(this.filePath.toString()));
3132
Assertions.assertNotNull(localResponse.getFile());
3233
Assertions
3334
.assertFalse(
3435
localResponse.isValidHmacSignature(this.secretKey, "invalid signature is invalid")
3536
);
3637
Assertions.assertEquals(this.signature, localResponse.getHmacSignature(this.secretKey));
3738
Assertions.assertTrue(localResponse.isValidHmacSignature(this.secretKey, this.signature));
39+
40+
var response = localResponse.deserializeAsyncResponse(InternationalIdV2.class);
41+
Assertions.assertNotNull(response);
42+
Assertions.assertNotNull(response.getDocumentObj());
3843
}
3944

4045
@Test
4146
void loadDocument_withString_mustReturnValidLocalResponse() {
42-
LocalResponse localResponse = new LocalResponse("{'some': 'json', 'with': 'data'}");
47+
var localResponse = new LocalResponse("{'some': 'json', 'with': 'data'}");
4348
Assertions.assertNotNull(localResponse.getFile());
4449
Assertions
4550
.assertFalse(
@@ -49,13 +54,17 @@ void loadDocument_withString_mustReturnValidLocalResponse() {
4954

5055
@Test
5156
void loadDocument_withInputStream_mustReturnValidLocalResponse() throws IOException {
52-
LocalResponse localResponse = new LocalResponse(Files.newInputStream(this.filePath));
57+
var localResponse = new LocalResponse(Files.newInputStream(this.filePath));
5358
Assertions.assertNotNull(localResponse.getFile());
5459
Assertions
5560
.assertFalse(
5661
localResponse.isValidHmacSignature(this.secretKey, "invalid signature is invalid")
5762
);
5863
Assertions.assertEquals(this.signature, localResponse.getHmacSignature(this.secretKey));
5964
Assertions.assertTrue(localResponse.isValidHmacSignature(this.secretKey, this.signature));
65+
66+
var response = localResponse.deserializeAsyncResponse(InternationalIdV2.class);
67+
Assertions.assertNotNull(response);
68+
Assertions.assertNotNull(response.getDocumentObj());
6069
}
6170
}

src/test/java/com/mindee/v2/MindeeClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import com.fasterxml.jackson.core.JsonProcessingException;
88
import com.fasterxml.jackson.databind.ObjectMapper;
99
import com.mindee.input.LocalInputSource;
10-
import com.mindee.input.LocalResponse;
1110
import com.mindee.input.URLInputSource;
1211
import com.mindee.v2.clientOptions.BaseParameters;
1312
import com.mindee.v2.http.MindeeApiV2;
13+
import com.mindee.v2.input.LocalResponse;
1414
import com.mindee.v2.parsing.CommonResponse;
1515
import com.mindee.v2.parsing.JobResponse;
1616
import com.mindee.v2.product.extraction.ExtractionResponse;

src/test/java/com/mindee/input/LocalResponseV2Test.java renamed to src/test/java/com/mindee/v2/input/LocalResponseTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.mindee.input;
1+
package com.mindee.v2.input;
22

33
import static com.mindee.TestingUtilities.getV2ResourcePath;
44

@@ -10,19 +10,19 @@
1010
import org.junit.jupiter.api.Assertions;
1111
import org.junit.jupiter.api.Test;
1212

13-
public class LocalResponseV2Test {
13+
public class LocalResponseTest {
1414
/**
1515
* Fake secret key.
1616
*/
1717
String secretKey = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH";
1818

1919
/**
20-
* Real signature using fake secret key.
20+
* Real signature using a fake secret key.
2121
*/
2222
String signature = "e51bdf80f1a08ed44ee161100fc30a25cb35b4ede671b0a575dc9064a3f5dbf1";
2323

2424
/**
25-
* File which the signature applies to.
25+
* File that the signature applies to.
2626
*/
2727
Path filePath = getV2ResourcePath("products/extraction/standard_field_types.json");
2828

@@ -34,20 +34,19 @@ protected void assertLocalResponse(LocalResponse localResponse) {
3434
);
3535
Assertions.assertEquals(this.signature, localResponse.getHmacSignature(this.secretKey));
3636
Assertions.assertTrue(localResponse.isValidHmacSignature(this.secretKey, this.signature));
37-
ExtractionResponse response = localResponse.deserializeResponse(ExtractionResponse.class);
37+
var response = localResponse.deserializeResponse(ExtractionResponse.class);
3838
Assertions.assertNotNull(response);
3939
Assertions.assertNotNull(response.getInference());
4040
}
4141

4242
@Test
4343
void loadDocument_withFile_mustReturnValidLocalResponse() throws IOException {
44-
LocalResponse localResponse = new LocalResponse(new File(this.filePath.toString()));
45-
assertLocalResponse(localResponse);
44+
assertLocalResponse(new LocalResponse(new File(this.filePath.toString())));
4645
}
4746

4847
@Test
4948
void loadDocument_withString_mustReturnValidLocalResponse() {
50-
LocalResponse localResponse = new LocalResponse("{'some': 'json', 'with': 'data'}");
49+
var localResponse = new LocalResponse("{'some': 'json', 'with': 'data'}");
5150
Assertions.assertNotNull(localResponse.getFile());
5251
Assertions
5352
.assertFalse(
@@ -57,7 +56,7 @@ void loadDocument_withString_mustReturnValidLocalResponse() {
5756

5857
@Test
5958
void loadDocument_withInputStream_mustReturnValidLocalResponse() throws IOException {
60-
LocalResponse localResponse = new LocalResponse(Files.newInputStream(this.filePath));
59+
var localResponse = new LocalResponse(Files.newInputStream(this.filePath));
6160
assertLocalResponse(localResponse);
6261
}
6362
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import static org.junit.jupiter.api.Assertions.assertNotNull;
66
import static org.junit.jupiter.api.Assertions.assertNull;
77

8-
import com.mindee.input.LocalResponse;
8+
import com.mindee.v2.input.LocalResponse;
99
import java.io.IOException;
1010
import org.junit.jupiter.api.DisplayName;
1111
import org.junit.jupiter.api.Nested;

src/test/java/com/mindee/v2/product/ClassificationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import static org.junit.jupiter.api.Assertions.assertEquals;
55
import static org.junit.jupiter.api.Assertions.assertNotNull;
66

7-
import com.mindee.input.LocalResponse;
7+
import com.mindee.v2.input.LocalResponse;
88
import com.mindee.v2.product.classification.ClassificationResponse;
99
import java.io.IOException;
1010
import org.junit.jupiter.api.DisplayName;

src/test/java/com/mindee/v2/product/CropTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import static org.junit.jupiter.api.Assertions.assertEquals;
66
import static org.junit.jupiter.api.Assertions.assertNotNull;
77

8-
import com.mindee.input.LocalResponse;
8+
import com.mindee.v2.input.LocalResponse;
99
import com.mindee.v2.product.crop.CropResponse;
1010
import java.io.IOException;
1111
import org.junit.jupiter.api.DisplayName;

0 commit comments

Comments
 (0)