Skip to content

Commit 3cbe4fa

Browse files
committed
♻️ use a base classes for v2 products
1 parent 03beddf commit 3cbe4fa

File tree

11 files changed

+199
-143
lines changed

11 files changed

+199
-143
lines changed

src/main/java/com/mindee/InferenceParameters.java

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package com.mindee;
22

3+
import com.mindee.v2.clientOptions.BaseParameters;
34
import java.util.Objects;
4-
import lombok.Data;
5+
import lombok.EqualsAndHashCode;
56
import lombok.Getter;
7+
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
68

79
/**
810
* Options to pass when calling methods using the API V2.
911
*/
1012
@Getter
11-
@Data
12-
public final class InferenceParameters {
13-
/**
14-
* Model ID to use for the inference (required).
15-
*/
16-
private final String modelId;
13+
@EqualsAndHashCode(callSuper = true)
14+
public final class InferenceParameters extends BaseParameters {
1715
/**
1816
* Enhance extraction accuracy with Retrieval-Augmented Generation.
1917
*/
@@ -31,19 +29,6 @@ public final class InferenceParameters {
3129
* Calculate confidence scores for all fields.
3230
*/
3331
private final Boolean confidence;
34-
/**
35-
* Optional alias for the file.
36-
*/
37-
private final String alias;
38-
/**
39-
* Webhook IDs to call after all processing is finished.
40-
* If empty, no webhooks will be used.
41-
*/
42-
private final String[] webhookIds;
43-
/**
44-
* Polling options. Set only if having timeout issues.
45-
*/
46-
private final AsyncPollingOptions pollingOptions;
4732
/**
4833
* Additional text context used by the model during inference.
4934
* Not recommended, for specific use only.
@@ -54,6 +39,50 @@ public final class InferenceParameters {
5439
*/
5540
private final String dataSchema;
5641

42+
private InferenceParameters(
43+
String modelId,
44+
String alias,
45+
String[] webhookIds,
46+
AsyncPollingOptions pollingOptions,
47+
Boolean rag,
48+
Boolean rawText,
49+
Boolean polygon,
50+
Boolean confidence,
51+
String textContext,
52+
String dataSchema
53+
) {
54+
super(modelId, alias, webhookIds, pollingOptions);
55+
this.rag = rag;
56+
this.rawText = rawText;
57+
this.polygon = polygon;
58+
this.confidence = confidence;
59+
this.textContext = textContext;
60+
this.dataSchema = dataSchema;
61+
}
62+
63+
public MultipartEntityBuilder buildHttpBody(MultipartEntityBuilder builder) {
64+
builder = super.buildHttpBody(builder);
65+
if (this.getRag() != null) {
66+
builder.addTextBody("rag", this.getRag().toString().toLowerCase());
67+
}
68+
if (this.getRawText() != null) {
69+
builder.addTextBody("raw_text", this.getRawText().toString().toLowerCase());
70+
}
71+
if (this.getPolygon() != null) {
72+
builder.addTextBody("polygon", this.getPolygon().toString().toLowerCase());
73+
}
74+
if (this.getConfidence() != null) {
75+
builder.addTextBody("confidence", this.getConfidence().toString().toLowerCase());
76+
}
77+
if (this.getTextContext() != null) {
78+
builder.addTextBody("text_context", this.getTextContext());
79+
}
80+
if (this.getDataSchema() != null) {
81+
builder.addTextBody("data_schema", this.getDataSchema());
82+
}
83+
return builder;
84+
}
85+
5786
/**
5887
* Create a new builder.
5988
*
@@ -145,13 +174,13 @@ public Builder pollingOptions(AsyncPollingOptions pollingOptions) {
145174
public InferenceParameters build() {
146175
return new InferenceParameters(
147176
modelId,
177+
alias,
178+
webhookIds,
179+
pollingOptions,
148180
rag,
149181
rawText,
150182
polygon,
151183
confidence,
152-
alias,
153-
webhookIds,
154-
pollingOptions,
155184
textContext,
156185
dataSchema
157186
);

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.mindee.parsing.v2.ErrorResponse;
99
import com.mindee.parsing.v2.InferenceResponse;
1010
import com.mindee.parsing.v2.JobResponse;
11+
import com.mindee.v2.clientOptions.BaseParameters;
1112
import java.io.IOException;
1213

1314
/**
@@ -36,7 +37,7 @@ public MindeeClientV2(MindeeApiV2 mindeeApi) {
3637
*/
3738
public JobResponse enqueueInference(
3839
LocalInputSource inputSource,
39-
InferenceParameters params
40+
BaseParameters params
4041
) throws IOException {
4142
return mindeeApi.reqPostInferenceEnqueue(inputSource, params);
4243
}
@@ -46,7 +47,7 @@ public JobResponse enqueueInference(
4647
*/
4748
public JobResponse enqueueInference(
4849
URLInputSource inputSource,
49-
InferenceParameters params
50+
BaseParameters params
5051
) throws IOException {
5152
return mindeeApi.reqPostInferenceEnqueue(inputSource, params);
5253
}
@@ -75,7 +76,7 @@ public InferenceResponse getInference(String inferenceId) {
7576

7677
/**
7778
* Send a local file to an async queue, poll, and parse when complete.
78-
*
79+
*
7980
* @param inputSource The input source to send.
8081
* @param options The options to send along with the file.
8182
* @return an instance of {@link InferenceResponse}.
@@ -84,7 +85,7 @@ public InferenceResponse getInference(String inferenceId) {
8485
*/
8586
public InferenceResponse enqueueAndGetInference(
8687
LocalInputSource inputSource,
87-
InferenceParameters options
88+
BaseParameters options
8889
) throws IOException, InterruptedException {
8990
validatePollingOptions(options.getPollingOptions());
9091
JobResponse job = enqueueInference(inputSource, options);
@@ -93,7 +94,7 @@ public InferenceResponse enqueueAndGetInference(
9394

9495
/**
9596
* Send a local file to an async queue, poll, and parse when complete.
96-
*
97+
*
9798
* @param inputSource The input source to send.
9899
* @param options The options to send along with the file.
99100
* @return an instance of {@link InferenceResponse}.
@@ -102,7 +103,7 @@ public InferenceResponse enqueueAndGetInference(
102103
*/
103104
public InferenceResponse enqueueAndGetInference(
104105
URLInputSource inputSource,
105-
InferenceParameters options
106+
BaseParameters options
106107
) throws IOException, InterruptedException {
107108
validatePollingOptions(options.getPollingOptions());
108109
JobResponse job = enqueueInference(inputSource, options);
@@ -111,14 +112,14 @@ public InferenceResponse enqueueAndGetInference(
111112

112113
/**
113114
* Common logic for polling an asynchronous job for local & url files.
114-
*
115+
*
115116
* @param initialJob The initial job response.
116117
* @return an instance of {@link InferenceResponse}.
117118
* @throws InterruptedException Throws if interrupted.
118119
*/
119120
private InferenceResponse pollAndFetch(
120121
JobResponse initialJob,
121-
InferenceParameters options
122+
BaseParameters options
122123
) throws InterruptedException {
123124
Thread.sleep((long) (options.getPollingOptions().getInitialDelaySec() * 1000));
124125

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.mindee.http;
22

3-
import com.mindee.InferenceParameters;
43
import com.mindee.input.LocalInputSource;
54
import com.mindee.input.URLInputSource;
65
import com.mindee.parsing.v2.ErrorResponse;
76
import com.mindee.parsing.v2.InferenceResponse;
87
import com.mindee.parsing.v2.JobResponse;
8+
import com.mindee.v2.clientOptions.BaseParameters;
99
import java.io.IOException;
1010

1111
/**
@@ -14,36 +14,36 @@
1414
public abstract class MindeeApiV2 extends MindeeApiCommon {
1515
/**
1616
* Send a file to the prediction queue with a local file.
17-
*
17+
*
1818
* @param inputSource Local input source from URL.
1919
* @param options parameters.
2020
*/
2121
public abstract JobResponse reqPostInferenceEnqueue(
2222
LocalInputSource inputSource,
23-
InferenceParameters options
23+
BaseParameters options
2424
) throws IOException;
2525

2626
/**
2727
* Send a file to the prediction queue with a remote file.
28-
*
28+
*
2929
* @param inputSource Remote input source from URL.
3030
* @param options parameters.
3131
*/
3232
public abstract JobResponse reqPostInferenceEnqueue(
3333
URLInputSource inputSource,
34-
InferenceParameters options
34+
BaseParameters options
3535
) throws IOException;
3636

3737
/**
3838
* Attempts to poll the queue.
39-
*
39+
*
4040
* @param jobId id of the job to get.
4141
*/
4242
public abstract JobResponse reqGetJob(String jobId);
4343

4444
/**
4545
* Retrieves the inference from a 302 redirect.
46-
*
46+
*
4747
* @param inferenceId ID of the inference to poll.
4848
*/
4949
abstract public InferenceResponse reqGetInference(String inferenceId);

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

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.mindee.http;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import com.mindee.InferenceParameters;
54
import com.mindee.MindeeException;
65
import com.mindee.MindeeSettingsV2;
76
import com.mindee.input.LocalInputSource;
@@ -10,6 +9,7 @@
109
import com.mindee.parsing.v2.ErrorResponse;
1110
import com.mindee.parsing.v2.InferenceResponse;
1211
import com.mindee.parsing.v2.JobResponse;
12+
import com.mindee.v2.clientOptions.BaseParameters;
1313
import java.io.IOException;
1414
import java.net.URISyntaxException;
1515
import java.nio.charset.StandardCharsets;
@@ -68,10 +68,7 @@ private MindeeHttpApiV2(MindeeSettingsV2 mindeeSettings, HttpClientBuilder httpC
6868
* @return A job response.
6969
*/
7070
@Override
71-
public JobResponse reqPostInferenceEnqueue(
72-
LocalInputSource inputSource,
73-
InferenceParameters options
74-
) {
71+
public JobResponse reqPostInferenceEnqueue(LocalInputSource inputSource, BaseParameters options) {
7572
String url = this.mindeeSettings.getBaseUrl() + "/products/extraction/enqueue";
7673
HttpPost post = buildHttpPost(url);
7774

@@ -84,7 +81,7 @@ public JobResponse reqPostInferenceEnqueue(
8481
ContentType.DEFAULT_BINARY,
8582
inputSource.getFilename()
8683
);
87-
post.setEntity(buildHttpBody(builder, options));
84+
post.setEntity(options.buildHttpBody(builder).build());
8885
return executeEnqueue(post);
8986
}
9087

@@ -96,17 +93,14 @@ public JobResponse reqPostInferenceEnqueue(
9693
* @return A job response.
9794
*/
9895
@Override
99-
public JobResponse reqPostInferenceEnqueue(
100-
URLInputSource inputSource,
101-
InferenceParameters options
102-
) {
96+
public JobResponse reqPostInferenceEnqueue(URLInputSource inputSource, BaseParameters options) {
10397
String url = this.mindeeSettings.getBaseUrl() + "/products/extraction/enqueue";
10498
HttpPost post = buildHttpPost(url);
10599

106100
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
107101
builder.setMode(HttpMultipartMode.EXTENDED);
108102
builder.addTextBody("url", inputSource.getUrl());
109-
post.setEntity(buildHttpBody(builder, options));
103+
post.setEntity(options.buildHttpBody(builder).build());
110104
return executeEnqueue(post);
111105
}
112106

@@ -224,35 +218,6 @@ private MindeeHttpExceptionV2 getHttpError(ClassicHttpResponse response) {
224218
}
225219
}
226220

227-
private HttpEntity buildHttpBody(MultipartEntityBuilder builder, InferenceParameters params) {
228-
builder.addTextBody("model_id", params.getModelId());
229-
if (params.getRag() != null) {
230-
builder.addTextBody("rag", params.getRag().toString().toLowerCase());
231-
}
232-
if (params.getRawText() != null) {
233-
builder.addTextBody("raw_text", params.getRawText().toString().toLowerCase());
234-
}
235-
if (params.getPolygon() != null) {
236-
builder.addTextBody("polygon", params.getPolygon().toString().toLowerCase());
237-
}
238-
if (params.getConfidence() != null) {
239-
builder.addTextBody("confidence", params.getConfidence().toString().toLowerCase());
240-
}
241-
if (params.getAlias() != null) {
242-
builder.addTextBody("alias", params.getAlias());
243-
}
244-
if (params.getWebhookIds().length > 0) {
245-
builder.addTextBody("webhook_ids", String.join(",", params.getWebhookIds()));
246-
}
247-
if (params.getTextContext() != null) {
248-
builder.addTextBody("text_context", params.getTextContext());
249-
}
250-
if (params.getDataSchema() != null) {
251-
builder.addTextBody("data_schema", params.getDataSchema());
252-
}
253-
return builder.build();
254-
}
255-
256221
private HttpPost buildHttpPost(String url) {
257222
HttpPost post;
258223
try {

0 commit comments

Comments
 (0)