1- package com .mindee .input ;
1+ package com .mindee .parsing ;
22
3- import com .fasterxml .jackson .databind .ObjectMapper ;
4- import com .mindee .MindeeException ;
5- import com .mindee .v2 .parsing .CommonResponse ;
63import java .io .BufferedReader ;
74import java .io .File ;
85import java .io .IOException ;
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 BaseLocalResponse {
25+ protected final byte [] file ;
3026
3127 /**
3228 * Load from an {@link InputStream}.
3329 *
3430 * @param input will be decoded as UTF-8.
3531 */
36- public LocalResponse (InputStream input ) {
32+ public BaseLocalResponse (InputStream input ) {
3733 this .file = this
3834 .getBytes (new BufferedReader (new InputStreamReader (input , StandardCharsets .UTF_8 )).lines ());
3935 }
@@ -43,7 +39,10 @@ public LocalResponse(InputStream input) {
4339 *
4440 * @param input will be decoded as UTF-8.
4541 */
46- public LocalResponse (String input ) {
42+ public BaseLocalResponse (String input ) {
43+ if (input == null || input .isEmpty ()) {
44+ throw new IllegalArgumentException ("Input string cannot be empty or null." );
45+ }
4746 this .file = input .getBytes (StandardCharsets .UTF_8 );
4847 }
4948
@@ -52,7 +51,7 @@ public LocalResponse(String input) {
5251 *
5352 * @param input will be decoded as UTF-8.
5453 */
55- public LocalResponse (File input ) throws IOException {
54+ public BaseLocalResponse (File input ) throws IOException {
5655 this .file = this .getBytes (Files .lines (input .toPath (), StandardCharsets .UTF_8 ));
5756 }
5857
@@ -61,7 +60,7 @@ public LocalResponse(File input) throws IOException {
6160 *
6261 * @param input will be decoded as UTF-8.
6362 */
64- public LocalResponse (Path input ) throws IOException {
63+ public BaseLocalResponse (Path input ) throws IOException {
6564 this .file = this .getBytes (Files .lines (input , StandardCharsets .UTF_8 ));
6665 }
6766
@@ -106,24 +105,4 @@ public String getHmacSignature(String secretKey) {
106105 public boolean isValidHmacSignature (String secretKey , String signature ) {
107106 return signature .equals (getHmacSignature (secretKey ));
108107 }
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- }
129108}
0 commit comments