Skip to content

Commit da3b846

Browse files
committed
add post slug to v2 parameters
1 parent cfbd000 commit da3b846

7 files changed

Lines changed: 116 additions & 46 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private InferenceParameters(
5151
String textContext,
5252
String dataSchema
5353
) {
54-
super(modelId, alias, webhookIds, pollingOptions);
54+
super(modelId, alias, webhookIds, pollingOptions, "extraction");
5555
this.rag = rag;
5656
this.rawText = rawText;
5757
this.polygon = polygon;

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

Lines changed: 84 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.mindee.http.MindeeHttpExceptionV2;
66
import com.mindee.input.LocalInputSource;
77
import com.mindee.input.URLInputSource;
8+
import com.mindee.parsing.v2.CommonResponse;
89
import com.mindee.parsing.v2.ErrorResponse;
910
import com.mindee.parsing.v2.InferenceResponse;
1011
import com.mindee.parsing.v2.JobResponse;
@@ -33,23 +34,46 @@ public MindeeClientV2(MindeeApiV2 mindeeApi) {
3334
}
3435

3536
/**
36-
* Enqueue a document in the asynchronous queue.
37+
* @deprecated use `enqueue` instead.
3738
*/
3839
public JobResponse enqueueInference(
3940
LocalInputSource inputSource,
40-
BaseParameters params
41+
InferenceParameters params
4142
) throws IOException {
42-
return mindeeApi.reqPostInferenceEnqueue(inputSource, params);
43+
return enqueue(inputSource, params);
4344
}
4445

4546
/**
46-
* Enqueue a document in the asynchronous queue.
47+
* @deprecated use `enqueue` instead.
4748
*/
4849
public JobResponse enqueueInference(
4950
URLInputSource inputSource,
5051
BaseParameters params
5152
) throws IOException {
52-
return mindeeApi.reqPostInferenceEnqueue(inputSource, params);
53+
return enqueue(inputSource, params);
54+
}
55+
56+
/**
57+
* Enqueue a document in the asynchronous queue.
58+
*
59+
* @param inputSource The local input source to send.
60+
* @param params The parameters to send along with the file.
61+
*/
62+
public JobResponse enqueue(
63+
LocalInputSource inputSource,
64+
BaseParameters params
65+
) throws IOException {
66+
return mindeeApi.reqPostEnqueue(inputSource, params);
67+
}
68+
69+
/**
70+
* Enqueue a document in the asynchronous queue.
71+
*
72+
* @param inputSource The URL input source to send.
73+
* @param params The parameters to send along with the file.
74+
*/
75+
public JobResponse enqueue(URLInputSource inputSource, BaseParameters params) throws IOException {
76+
return mindeeApi.reqPostEnqueue(inputSource, params);
5377
}
5478

5579
/**
@@ -63,51 +87,86 @@ public JobResponse getJob(String jobId) {
6387
return mindeeApi.reqGetJob(jobId);
6488
}
6589

90+
/**
91+
* @deprecated use `getResult` instead.
92+
*/
93+
public InferenceResponse getInference(String inferenceId) {
94+
if (inferenceId == null || inferenceId.trim().isEmpty()) {
95+
throw new IllegalArgumentException("inferenceId must not be null or blank.");
96+
}
97+
return getResult(InferenceResponse.class, inferenceId);
98+
}
99+
66100
/**
67101
* Get the result of an inference that was previously enqueued.
68102
* The inference will only be available after it has finished processing.
69103
*/
70-
public InferenceResponse getInference(String inferenceId) {
104+
public <TResponse extends CommonResponse> TResponse getResult(
105+
Class<TResponse> responseClass,
106+
String inferenceId
107+
) {
71108
if (inferenceId == null || inferenceId.trim().isEmpty()) {
72109
throw new IllegalArgumentException("inferenceId must not be null or blank.");
73110
}
74-
return mindeeApi.reqGetInference(inferenceId);
111+
return mindeeApi.reqGetResult(responseClass, inferenceId);
112+
}
113+
114+
/**
115+
* @deprecated use `enqueueAndGetResult` instead.
116+
*/
117+
public InferenceResponse enqueueAndGetInference(
118+
LocalInputSource inputSource,
119+
InferenceParameters options
120+
) throws IOException, InterruptedException {
121+
return enqueueAndGetResult(InferenceResponse.class, inputSource, options);
122+
}
123+
124+
/**
125+
* @deprecated use `enqueueAndGetResult` instead.
126+
*/
127+
public InferenceResponse enqueueAndGetInference(
128+
URLInputSource inputSource,
129+
InferenceParameters options
130+
) throws IOException, InterruptedException {
131+
return enqueueAndGetResult(InferenceResponse.class, inputSource, options);
75132
}
76133

77134
/**
78135
* Send a local file to an async queue, poll, and parse when complete.
79136
*
80-
* @param inputSource The input source to send.
81-
* @param options The options to send along with the file.
137+
* @param inputSource The local input source to send.
138+
* @param params The parameters to send along with the file.
82139
* @return an instance of {@link InferenceResponse}.
83140
* @throws IOException Throws if the file can't be accessed.
84141
* @throws InterruptedException Throws if the thread is interrupted.
85142
*/
86-
public InferenceResponse enqueueAndGetInference(
143+
public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
144+
Class<TResponse> responseClass,
87145
LocalInputSource inputSource,
88-
BaseParameters options
146+
BaseParameters params
89147
) throws IOException, InterruptedException {
90-
validatePollingOptions(options.getPollingOptions());
91-
JobResponse job = enqueueInference(inputSource, options);
92-
return pollAndFetch(job, options);
148+
validatePollingOptions(params.getPollingOptions());
149+
JobResponse job = enqueue(inputSource, params);
150+
return pollAndFetch(responseClass, job, params);
93151
}
94152

95153
/**
96-
* Send a local file to an async queue, poll, and parse when complete.
154+
* Send a remote file to an async queue, poll, and parse when complete.
97155
*
98-
* @param inputSource The input source to send.
99-
* @param options The options to send along with the file.
156+
* @param inputSource The URL input source to send.
157+
* @param params The parameters to send along with the file.
100158
* @return an instance of {@link InferenceResponse}.
101159
* @throws IOException Throws if the file can't be accessed.
102160
* @throws InterruptedException Throws if the thread is interrupted.
103161
*/
104-
public InferenceResponse enqueueAndGetInference(
162+
public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
163+
Class<TResponse> responseClass,
105164
URLInputSource inputSource,
106-
BaseParameters options
165+
BaseParameters params
107166
) throws IOException, InterruptedException {
108-
validatePollingOptions(options.getPollingOptions());
109-
JobResponse job = enqueueInference(inputSource, options);
110-
return pollAndFetch(job, options);
167+
validatePollingOptions(params.getPollingOptions());
168+
JobResponse job = enqueue(inputSource, params);
169+
return pollAndFetch(responseClass, job, params);
111170
}
112171

113172
/**
@@ -117,7 +176,8 @@ public InferenceResponse enqueueAndGetInference(
117176
* @return an instance of {@link InferenceResponse}.
118177
* @throws InterruptedException Throws if interrupted.
119178
*/
120-
private InferenceResponse pollAndFetch(
179+
private <TResponse extends CommonResponse> TResponse pollAndFetch(
180+
Class<TResponse> responseClass,
121181
JobResponse initialJob,
122182
BaseParameters options
123183
) throws InterruptedException {
@@ -135,7 +195,7 @@ private InferenceResponse pollAndFetch(
135195
attempts = max;
136196
}
137197
if (resp.getJob().getStatus().equals("Processed")) {
138-
return getInference(resp.getJob().getId());
198+
return getResult(responseClass, resp.getJob().getId());
139199
}
140200
attempts++;
141201
}

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

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

33
import com.mindee.input.LocalInputSource;
44
import com.mindee.input.URLInputSource;
5+
import com.mindee.parsing.v2.CommonResponse;
56
import com.mindee.parsing.v2.ErrorResponse;
6-
import com.mindee.parsing.v2.InferenceResponse;
77
import com.mindee.parsing.v2.JobResponse;
88
import com.mindee.v2.clientOptions.BaseParameters;
99
import java.io.IOException;
@@ -18,7 +18,7 @@ public abstract class MindeeApiV2 extends MindeeApiCommon {
1818
* @param inputSource Local input source from URL.
1919
* @param options parameters.
2020
*/
21-
public abstract JobResponse reqPostInferenceEnqueue(
21+
public abstract JobResponse reqPostEnqueue(
2222
LocalInputSource inputSource,
2323
BaseParameters options
2424
) throws IOException;
@@ -29,7 +29,7 @@ public abstract JobResponse reqPostInferenceEnqueue(
2929
* @param inputSource Remote input source from URL.
3030
* @param options parameters.
3131
*/
32-
public abstract JobResponse reqPostInferenceEnqueue(
32+
public abstract JobResponse reqPostEnqueue(
3333
URLInputSource inputSource,
3434
BaseParameters options
3535
) throws IOException;
@@ -46,7 +46,10 @@ public abstract JobResponse reqPostInferenceEnqueue(
4646
*
4747
* @param inferenceId ID of the inference to poll.
4848
*/
49-
abstract public InferenceResponse reqGetInference(String inferenceId);
49+
abstract public <TResponse extends CommonResponse> TResponse reqGetResult(
50+
Class<TResponse> responseClass,
51+
String inferenceId
52+
);
5053

5154
/**
5255
* Creates an "unknown error" response from an HTTP status code.

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.mindee.input.URLInputSource;
88
import com.mindee.parsing.v2.CommonResponse;
99
import com.mindee.parsing.v2.ErrorResponse;
10-
import com.mindee.parsing.v2.InferenceResponse;
1110
import com.mindee.parsing.v2.JobResponse;
1211
import com.mindee.v2.clientOptions.BaseParameters;
1312
import java.io.IOException;
@@ -68,8 +67,9 @@ private MindeeHttpApiV2(MindeeSettingsV2 mindeeSettings, HttpClientBuilder httpC
6867
* @return A job response.
6968
*/
7069
@Override
71-
public JobResponse reqPostInferenceEnqueue(LocalInputSource inputSource, BaseParameters options) {
72-
String url = this.mindeeSettings.getBaseUrl() + "/products/extraction/enqueue";
70+
public JobResponse reqPostEnqueue(LocalInputSource inputSource, BaseParameters options) {
71+
String url = String
72+
.format("%s/products/%s/enqueue", this.mindeeSettings.getBaseUrl(), options.getSlug());
7373
HttpPost post = buildHttpPost(url);
7474

7575
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
@@ -93,8 +93,9 @@ public JobResponse reqPostInferenceEnqueue(LocalInputSource inputSource, BasePar
9393
* @return A job response.
9494
*/
9595
@Override
96-
public JobResponse reqPostInferenceEnqueue(URLInputSource inputSource, BaseParameters options) {
97-
String url = this.mindeeSettings.getBaseUrl() + "/products/extraction/enqueue";
96+
public JobResponse reqPostEnqueue(URLInputSource inputSource, BaseParameters options) {
97+
String url = String
98+
.format("%s/products/%s/enqueue", this.mindeeSettings.getBaseUrl(), options.getSlug());
9899
HttpPost post = buildHttpPost(url);
99100

100101
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
@@ -167,7 +168,10 @@ public JobResponse reqGetJob(String jobId) {
167168
}
168169

169170
@Override
170-
public InferenceResponse reqGetInference(String inferenceId) {
171+
public <TResponse extends CommonResponse> TResponse reqGetResult(
172+
Class<TResponse> responseClass,
173+
String inferenceId
174+
) {
171175

172176
String url = this.mindeeSettings.getBaseUrl() + "/products/extraction/results/" + inferenceId;
173177
HttpGet get = new HttpGet(url);
@@ -189,7 +193,7 @@ public InferenceResponse reqGetInference(String inferenceId) {
189193
throw getHttpError(response);
190194
}
191195
String raw = EntityUtils.toString(entity, StandardCharsets.UTF_8);
192-
return deserializeOrThrow(raw, InferenceResponse.class, status);
196+
return deserializeOrThrow(raw, responseClass, status);
193197
} finally {
194198
EntityUtils.consumeQuietly(entity);
195199
}

src/main/java/com/mindee/v2/clientOptions/BaseParameters.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public abstract class BaseParameters {
2323
* Polling options. Set only if having timeout issues.
2424
*/
2525
protected final AsyncPollingOptions pollingOptions;
26+
/**
27+
* Slug of the product type.
28+
*/
29+
private final String slug;
2630

2731
public MultipartEntityBuilder buildHttpBody(MultipartEntityBuilder builder) {
2832
builder.addTextBody("model_id", this.getModelId());

src/test/java/com/mindee/MindeeClientV2IT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ void parseFile_dataSchemaReplace_mustSucceed() throws IOException, InterruptedEx
147147
)
148148
.build();
149149

150-
InferenceResponse response = mindeeClient.enqueueAndGetInference(source, params);
150+
InferenceResponse response = mindeeClient
151+
.enqueueAndGetResult(InferenceResponse.class, source, params);
151152
assertNotNull(response);
152153
Inference inference = response.getInference();
153154
assertNotNull(inference);

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import static com.mindee.TestingUtilities.getResourcePath;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
55
import static org.junit.jupiter.api.Assertions.assertNotNull;
6-
import static org.mockito.ArgumentMatchers.any;
7-
import static org.mockito.ArgumentMatchers.anyString;
6+
import static org.mockito.ArgumentMatchers.*;
87
import static org.mockito.Mockito.atMostOnce;
98
import static org.mockito.Mockito.verify;
109
import static org.mockito.Mockito.when;
@@ -39,10 +38,8 @@ class Enqueue {
3938
@DisplayName("sends exactly one HTTP call and yields a non-null response")
4039
void enqueue_post_async() throws IOException {
4140
MindeeApiV2 predictable = Mockito.mock(MindeeApiV2.class);
42-
when(
43-
predictable
44-
.reqPostInferenceEnqueue(any(LocalInputSource.class), any(InferenceParameters.class))
45-
).thenReturn(new JobResponse());
41+
when(predictable.reqPostEnqueue(any(LocalInputSource.class), any(InferenceParameters.class)))
42+
.thenReturn(new JobResponse());
4643

4744
MindeeClientV2 mindeeClient = makeClientWithMockedApi(predictable);
4845

@@ -55,7 +52,7 @@ void enqueue_post_async() throws IOException {
5552

5653
assertNotNull(response, "enqueue() must return a response");
5754
verify(predictable, atMostOnce())
58-
.reqPostInferenceEnqueue(any(LocalInputSource.class), any(InferenceParameters.class));
55+
.reqPostEnqueue(any(LocalInputSource.class), any(InferenceParameters.class));
5956
}
6057
}
6158

@@ -100,7 +97,8 @@ void document_getInference_async() throws IOException {
10097

10198
InferenceResponse processing = mapper.readValue(json, InferenceResponse.class);
10299

103-
when(predictable.reqGetInference(anyString())).thenReturn(processing);
100+
when(predictable.reqGetResult(eq(InferenceResponse.class), anyString()))
101+
.thenReturn(processing);
104102

105103
MindeeClientV2 mindeeClient = makeClientWithMockedApi(predictable);
106104

@@ -123,7 +121,7 @@ void document_getInference_async() throws IOException {
123121
.getValue(),
124122
"Result must deserialize fields properly."
125123
);
126-
verify(predictable, atMostOnce()).reqGetInference(anyString());
124+
verify(predictable, atMostOnce()).reqGetResult(eq(InferenceResponse.class), anyString());
127125
}
128126
}
129127

0 commit comments

Comments
 (0)