Skip to content

Commit d34365e

Browse files
fix lingering issues (NO TESTS)
1 parent b31cb2e commit d34365e

13 files changed

Lines changed: 98 additions & 72 deletions

docs/code_samples/default_v2.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import com.mindee.MindeeClientV2;
22
import com.mindee.InferencePredictOptions;
33
import com.mindee.input.LocalInputSource;
4-
import com.mindee.parsing.common.InferenceResponse;
4+
import com.mindee.parsing.v2.InferenceResponse;
55
import java.io.File;
66
import java.io.IOException;
77

88
public class SimpleMindeeClient {
99

10-
public static void main(String[] args) throws IOException {
10+
public static void main(String[] args) throws IOException, InterruptedException {
1111
String apiKey = "MY_API_KEY";
1212
String filePath = "/path/to/the/file.ext";
1313

@@ -18,16 +18,17 @@ public class SimpleMindeeClient {
1818
LocalInputSource inputSource = new LocalInputSource(new File(filePath));
1919

2020
// Prepare the enqueueing options
21-
InferencePredictOptions options = InferencePredictOptions.builder().modelId("MY_MODEL_ID").build();
21+
// Note: modelId is mandatory.
22+
InferencePredictOptions options = InferencePredictOptions.builder("MY_MODEL_ID").build();
2223

2324
// Parse the file
2425
InferenceResponse response = mindeeClient.enqueueAndParse(
25-
inputSource;
26+
inputSource,
2627
options
2728
);
2829

2930
// Print a summary of the response
30-
System.out.println(response.toString());
31+
System.out.println(response.getInference().toString());
3132

3233
// Print a summary of the predictions
3334
// System.out.println(response.getDocument().toString());

src/main/java/com/mindee/InferencePredictOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static final class Builder {
6565
private String alias;
6666
private List<String> webhookIds = Collections.emptyList();
6767
private PageOptions pageOptions = null;
68-
private AsyncPollingOptions pollingOptions = null;
68+
private AsyncPollingOptions pollingOptions = AsyncPollingOptions.builder().build();
6969

7070
private Builder(String modelId) {
7171
this.modelId = Objects.requireNonNull(modelId, "modelId must not be null");

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import com.mindee.http.MindeeHttpApiV2;
66
import com.mindee.input.LocalInputSource;
77
import com.mindee.input.LocalResponse;
8+
import com.mindee.parsing.v2.CommonResponse;
89
import com.mindee.parsing.v2.InferenceResponse;
910
import com.mindee.parsing.v2.JobResponse;
10-
import com.mindee.parsing.v2.CommonResponse;
1111
import com.mindee.pdf.PdfBoxApi;
1212
import com.mindee.pdf.PdfOperation;
1313
import java.io.IOException;
@@ -65,26 +65,27 @@ public CommonResponse parseQueued(String jobId) {
6565
}
6666

6767
/**
68-
* Convenience helper: enqueue, poll, and return the final inference.
68+
* Send a local file to an async queue, poll, and parse when complete.
69+
* @param inputSource The input source to send.
70+
* @param options The options to send along with the file.
71+
* @return an instance of {@link InferenceResponse}.
72+
* @throws IOException Throws if the file can't be accessed.
73+
* @throws InterruptedException Throws if the thread is interrupted.
6974
*/
7075
public InferenceResponse enqueueAndParse(
7176
LocalInputSource inputSource,
72-
InferencePredictOptions options,
73-
AsyncPollingOptions polling) throws IOException, InterruptedException {
77+
InferencePredictOptions options) throws IOException, InterruptedException {
7478

75-
if (polling == null) {
76-
polling = AsyncPollingOptions.builder().build(); // default values
77-
}
78-
validatePollingOptions(polling);
79+
validatePollingOptions(options.getPollingOptions());
7980

8081
JobResponse job = enqueue(inputSource, options);
8182

82-
Thread.sleep((long) (polling.getInitialDelaySec() * 1000));
83+
Thread.sleep((long) (options.getPollingOptions().getInitialDelaySec() * 1000));
8384

8485
int attempts = 0;
85-
int max = polling.getMaxRetries();
86+
int max = options.getPollingOptions().getMaxRetries();
8687
while (attempts < max) {
87-
Thread.sleep((long) (polling.getIntervalSec() * 1000));
88+
Thread.sleep((long) (options.getPollingOptions().getIntervalSec() * 1000));
8889
CommonResponse resp = parseQueued(job.getJob().getId());
8990
if (resp instanceof InferenceResponse) {
9091
return (InferenceResponse) resp;

src/main/java/com/mindee/MindeeSettingsV2.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package com.mindee;
22

33
import java.util.Optional;
4+
import lombok.Builder;
45
import lombok.Getter;
56

67
/**
78
* Mindee API V2 configuration.
89
*/
910
@Getter
11+
@Builder
1012
public class MindeeSettingsV2 {
1113

12-
private static final String DEFAULT_MINDEE_V2_API_URL = "https://api-v2.mindee.net/v1";
14+
private static final String DEFAULT_MINDEE_V2_API_URL = "https://api-v2.mindee.net/v2";
1315
private final String apiKey;
1416
private final String baseUrl;
1517

src/main/java/com/mindee/http/MindeeApiV2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.mindee.InferencePredictOptions;
44
import com.mindee.input.LocalInputSource;
5-
import com.mindee.parsing.v2.JobResponse;
65
import com.mindee.parsing.v2.CommonResponse;
6+
import com.mindee.parsing.v2.JobResponse;
77
import java.io.IOException;
88

99
/**

src/main/java/com/mindee/http/MindeeHttpApiV2.java

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
import com.mindee.MindeeException;
66
import com.mindee.MindeeSettingsV2;
77
import com.mindee.input.LocalInputSource;
8-
import com.mindee.parsing.v2.InferenceResponse;
9-
import com.mindee.parsing.v2.JobResponse;
108
import com.mindee.parsing.v2.CommonResponse;
119
import com.mindee.parsing.v2.ErrorResponse;
10+
import com.mindee.parsing.v2.InferenceResponse;
11+
import com.mindee.parsing.v2.JobResponse;
1212
import java.io.IOException;
1313
import java.net.URISyntaxException;
1414
import java.nio.charset.StandardCharsets;
15-
import java.util.ArrayList;
16-
import java.util.List;
1715
import lombok.Builder;
1816
import org.apache.hc.client5.http.classic.methods.HttpGet;
1917
import org.apache.hc.client5.http.classic.methods.HttpPost;
@@ -25,9 +23,7 @@
2523
import org.apache.hc.core5.http.ContentType;
2624
import org.apache.hc.core5.http.HttpEntity;
2725
import org.apache.hc.core5.http.HttpHeaders;
28-
import org.apache.hc.core5.http.NameValuePair;
2926
import org.apache.hc.core5.http.io.entity.EntityUtils;
30-
import org.apache.hc.core5.http.message.BasicNameValuePair;
3127
import org.apache.hc.core5.net.URIBuilder;
3228

3329
/**
@@ -103,6 +99,10 @@ public CommonResponse getInferenceFromQueue(
10399
String url = this.mindeeSettings.getBaseUrl() + "/inferences/" + jobId;
104100
HttpGet get = new HttpGet(url);
105101

102+
if (this.mindeeSettings.getApiKey().isPresent()) {
103+
get.setHeader(HttpHeaders.AUTHORIZATION, this.mindeeSettings.getApiKey().get());
104+
}
105+
get.setHeader(HttpHeaders.USER_AGENT, getUserAgent());
106106
mapper.findAndRegisterModules();
107107
try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
108108
return httpClient.execute(
@@ -120,34 +120,13 @@ public CommonResponse getInferenceFromQueue(
120120
/* make sure the connection can be reused even if parsing fails */
121121
EntityUtils.consumeQuietly(responseEntity);
122122
}
123-
124123
}
125124
);
126125
} catch (IOException err) {
127126
throw new MindeeException(err.getMessage(), err);
128127
}
129128
}
130129

131-
private List<NameValuePair> buildPostParams(
132-
InferencePredictOptions options
133-
) {
134-
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
135-
params.add(new BasicNameValuePair("model_id", options.getModelId()));
136-
if (options.isFullText()) {
137-
params.add(new BasicNameValuePair("full_text_ocr", "true"));
138-
}
139-
if (options.isRag()) {
140-
params.add(new BasicNameValuePair("rag", "true"));
141-
}
142-
if (options.getAlias() != null) {
143-
params.add(new BasicNameValuePair("alias", options.getAlias()));
144-
}
145-
if (!options.getWebhookIds().isEmpty()) {
146-
params.add(new BasicNameValuePair("webhook_ids", String.join(",", options.getWebhookIds())));
147-
}
148-
return params;
149-
}
150-
151130
private MindeeHttpExceptionV2 getHttpError(ClassicHttpResponse response) {
152131
String rawBody;
153132
try {
@@ -175,7 +154,7 @@ private HttpEntity buildHttpBody(
175154
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
176155
builder.setMode(HttpMultipartMode.EXTENDED);
177156
builder.addBinaryBody(
178-
"document",
157+
"file",
179158
inputSource.getFile(),
180159
ContentType.DEFAULT_BINARY,
181160
inputSource.getFilename()
@@ -187,6 +166,20 @@ private HttpEntity buildHttpBody(
187166
options.getAlias().toLowerCase()
188167
);
189168
}
169+
170+
builder.addTextBody("model_id", options.getModelId());
171+
if (options.isFullText()) {
172+
builder.addTextBody("full_text_ocr", "true");
173+
}
174+
if (options.isRag()) {
175+
builder.addTextBody("rag", "true");
176+
}
177+
if (options.getAlias() != null) {
178+
builder.addTextBody("alias", options.getAlias());
179+
}
180+
if (!options.getWebhookIds().isEmpty()) {
181+
builder.addTextBody("webhook_ids", String.join(",", options.getWebhookIds()));
182+
}
190183
return builder.build();
191184
}
192185

@@ -199,7 +192,6 @@ private HttpPost buildHttpPost(
199192
HttpPost post;
200193
try {
201194
URIBuilder uriBuilder = new URIBuilder(url);
202-
uriBuilder.addParameters(buildPostParams(options));
203195
post = new HttpPost(uriBuilder.build());
204196
}
205197
// This exception will never happen because we are providing the URL internally.
@@ -222,10 +214,11 @@ private <R extends CommonResponse> R deserializeOrThrow(
222214

223215
if (httpStatus >= 200 && httpStatus < 300) {
224216
try {
225-
R model = mapper.readValue(body, clazz);
217+
R model = mapper.readerFor(clazz).readValue(body);
226218
model.setRawResponse(body);
227219
return model;
228-
} catch (Exception ignored) {
220+
} catch (Exception exception) {
221+
throw new MindeeException("Couldn't deserialize server response:\n" + exception.getMessage());
229222
}
230223
}
231224

src/main/java/com/mindee/parsing/v2/InferenceFields.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import java.util.HashMap;
5+
import java.util.Map;
56
import lombok.EqualsAndHashCode;
67

78
/**
@@ -10,4 +11,12 @@
1011
@EqualsAndHashCode(callSuper = true)
1112
@JsonIgnoreProperties(ignoreUnknown = true)
1213
public final class InferenceFields extends HashMap<String, DynamicField> {
14+
@Override
15+
public String toString() {
16+
StringBuilder strBuilder = new StringBuilder();
17+
for (Map.Entry<String, DynamicField> entry : this.entrySet()) {
18+
strBuilder.append(':').append(entry.getKey()).append(": ").append(entry.getValue());
19+
}
20+
return strBuilder.toString();
21+
}
1322
}

src/main/java/com/mindee/parsing/v2/InferenceResponse.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.mindee.parsing.v2;
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.Getter;
45

56
/**
67
* Represents an asynchronous inference response (V2).
78
*/
9+
@Getter
810
public class InferenceResponse extends CommonResponse {
911

1012
/**

src/main/java/com/mindee/parsing/v2/InferenceResult.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5-
import java.util.Map;
65
import java.util.StringJoiner;
76
import lombok.AllArgsConstructor;
87
import lombok.EqualsAndHashCode;
@@ -19,22 +18,24 @@
1918
@NoArgsConstructor
2019
public final class InferenceResult {
2120

22-
/** Model fields. */
21+
/**
22+
* Model fields.
23+
*/
2324
@JsonProperty("fields")
2425
private InferenceFields fields;
2526

26-
/** Options. */
27+
/**
28+
* Options.
29+
*/
2730
@JsonProperty("options")
2831
private InferenceOptions options;
2932

3033
@Override
3134
public String toString() {
32-
if (fields == null || fields.isEmpty()) {
33-
return "";
34-
}
3535
StringJoiner joiner = new StringJoiner("\n");
36-
for (Map.Entry<String, DynamicField> e : fields.entrySet()) {
37-
joiner.add(":" + e.getKey() + ": " + e.getValue());
36+
joiner.add(fields.toString());
37+
if (options != null) {
38+
joiner.add(options.toString());
3839
}
3940
return joiner.toString();
4041
}

src/main/java/com/mindee/parsing/v2/Job.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public final class Job {
6161
/**
6262
* Optional alias of the file.
6363
*/
64-
@JsonProperty("file_name")
64+
@JsonProperty("file_alias")
6565
private String fileAlias;
6666

6767
/**

0 commit comments

Comments
 (0)