Skip to content

Commit 2c5c6b8

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

File tree

7 files changed

+115
-47
lines changed

7 files changed

+115
-47
lines changed

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: 81 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,83 @@ 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+
return getResult(InferenceResponse.class, inferenceId);
95+
}
96+
6697
/**
6798
* Get the result of an inference that was previously enqueued.
6899
* The inference will only be available after it has finished processing.
69100
*/
70-
public InferenceResponse getInference(String inferenceId) {
101+
public <TResponse extends CommonResponse> TResponse getResult(
102+
Class<TResponse> responseClass,
103+
String inferenceId
104+
) {
71105
if (inferenceId == null || inferenceId.trim().isEmpty()) {
72106
throw new IllegalArgumentException("inferenceId must not be null or blank.");
73107
}
74-
return mindeeApi.reqGetInference(inferenceId);
108+
return mindeeApi.reqGetResult(responseClass, inferenceId);
109+
}
110+
111+
/**
112+
* @deprecated use `enqueueAndGetResult` instead.
113+
*/
114+
public InferenceResponse enqueueAndGetInference(
115+
LocalInputSource inputSource,
116+
InferenceParameters options
117+
) throws IOException, InterruptedException {
118+
return enqueueAndGetResult(InferenceResponse.class, inputSource, options);
119+
}
120+
121+
/**
122+
* @deprecated use `enqueueAndGetResult` instead.
123+
*/
124+
public InferenceResponse enqueueAndGetInference(
125+
URLInputSource inputSource,
126+
InferenceParameters options
127+
) throws IOException, InterruptedException {
128+
return enqueueAndGetResult(InferenceResponse.class, inputSource, options);
75129
}
76130

77131
/**
78132
* Send a local file to an async queue, poll, and parse when complete.
79133
*
80-
* @param inputSource The input source to send.
81-
* @param options The options to send along with the file.
134+
* @param inputSource The local input source to send.
135+
* @param params The parameters to send along with the file.
82136
* @return an instance of {@link InferenceResponse}.
83137
* @throws IOException Throws if the file can't be accessed.
84138
* @throws InterruptedException Throws if the thread is interrupted.
85139
*/
86-
public InferenceResponse enqueueAndGetInference(
140+
public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
141+
Class<TResponse> responseClass,
87142
LocalInputSource inputSource,
88-
BaseParameters options
143+
BaseParameters params
89144
) throws IOException, InterruptedException {
90-
validatePollingOptions(options.getPollingOptions());
91-
JobResponse job = enqueueInference(inputSource, options);
92-
return pollAndFetch(job, options);
145+
validatePollingOptions(params.getPollingOptions());
146+
JobResponse job = enqueue(inputSource, params);
147+
return pollAndFetch(responseClass, job, params);
93148
}
94149

95150
/**
96-
* Send a local file to an async queue, poll, and parse when complete.
151+
* Send a remote file to an async queue, poll, and parse when complete.
97152
*
98-
* @param inputSource The input source to send.
99-
* @param options The options to send along with the file.
153+
* @param inputSource The URL input source to send.
154+
* @param params The parameters to send along with the file.
100155
* @return an instance of {@link InferenceResponse}.
101156
* @throws IOException Throws if the file can't be accessed.
102157
* @throws InterruptedException Throws if the thread is interrupted.
103158
*/
104-
public InferenceResponse enqueueAndGetInference(
159+
public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
160+
Class<TResponse> responseClass,
105161
URLInputSource inputSource,
106-
BaseParameters options
162+
BaseParameters params
107163
) throws IOException, InterruptedException {
108-
validatePollingOptions(options.getPollingOptions());
109-
JobResponse job = enqueueInference(inputSource, options);
110-
return pollAndFetch(job, options);
164+
validatePollingOptions(params.getPollingOptions());
165+
JobResponse job = enqueue(inputSource, params);
166+
return pollAndFetch(responseClass, job, params);
111167
}
112168

113169
/**
@@ -117,7 +173,8 @@ public InferenceResponse enqueueAndGetInference(
117173
* @return an instance of {@link InferenceResponse}.
118174
* @throws InterruptedException Throws if interrupted.
119175
*/
120-
private InferenceResponse pollAndFetch(
176+
private <TResponse extends CommonResponse> TResponse pollAndFetch(
177+
Class<TResponse> responseClass,
121178
JobResponse initialJob,
122179
BaseParameters options
123180
) throws InterruptedException {
@@ -135,7 +192,7 @@ private InferenceResponse pollAndFetch(
135192
attempts = max;
136193
}
137194
if (resp.getJob().getStatus().equals("Processed")) {
138-
return getInference(resp.getJob().getId());
195+
return getResult(responseClass, resp.getJob().getId());
139196
}
140197
attempts++;
141198
}

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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep
5353
)
5454
.build();
5555

56-
InferenceResponse response = mindeeClient.enqueueAndGetInference(source, params);
56+
InferenceResponse response = mindeeClient
57+
.enqueueAndGetResult(InferenceResponse.class, source, params);
5758
assertNotNull(response);
5859
Inference inference = response.getInference();
5960
assertNotNull(inference);
@@ -147,7 +148,8 @@ void parseFile_dataSchemaReplace_mustSucceed() throws IOException, InterruptedEx
147148
)
148149
.build();
149150

150-
InferenceResponse response = mindeeClient.enqueueAndGetInference(source, params);
151+
InferenceResponse response = mindeeClient
152+
.enqueueAndGetResult(InferenceResponse.class, source, params);
151153
assertNotNull(response);
152154
Inference inference = response.getInference();
153155
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)