-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMindeeHttpApiV2.java
More file actions
322 lines (281 loc) · 9.98 KB
/
MindeeHttpApiV2.java
File metadata and controls
322 lines (281 loc) · 9.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package com.mindee.http;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mindee.InferenceParameters;
import com.mindee.MindeeException;
import com.mindee.MindeeSettingsV2;
import com.mindee.input.LocalInputSource;
import com.mindee.input.URLInputSource;
import com.mindee.parsing.v2.CommonResponse;
import com.mindee.parsing.v2.ErrorResponse;
import com.mindee.parsing.v2.InferenceResponse;
import com.mindee.parsing.v2.JobResponse;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import lombok.Builder;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.net.URIBuilder;
/**
* HTTP Client class for the V2 API.
*/
public final class MindeeHttpApiV2 extends MindeeApiV2 {
private static final ObjectMapper mapper = new ObjectMapper();
/**
* The MindeeSetting needed to make the api call.
*/
private final MindeeSettingsV2 mindeeSettings;
/**
* The HttpClientBuilder used to create HttpClient objects used to make api calls over http.
* Defaults to HttpClientBuilder.create().useSystemProperties()
*/
private final HttpClientBuilder httpClientBuilder;
public MindeeHttpApiV2(MindeeSettingsV2 mindeeSettings) {
this(
mindeeSettings,
null
);
}
@Builder
private MindeeHttpApiV2(
MindeeSettingsV2 mindeeSettings,
HttpClientBuilder httpClientBuilder
) {
this.mindeeSettings = mindeeSettings;
if (httpClientBuilder != null) {
this.httpClientBuilder = httpClientBuilder;
} else {
this.httpClientBuilder = HttpClientBuilder.create().useSystemProperties();
}
}
/**
* Enqueues a doc with the POST method.
*
* @param inputSource Input source to send.
* @param options Options to send the file along with.
* @return A job response.
*/
@Override
public JobResponse reqPostInferenceEnqueue(
LocalInputSource inputSource,
InferenceParameters options
) {
String url = this.mindeeSettings.getBaseUrl() + "/inferences/enqueue";
HttpPost post = buildHttpPost(url, options);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.EXTENDED);
builder.addBinaryBody(
"file",
inputSource.getFile(),
ContentType.DEFAULT_BINARY,
inputSource.getFilename()
);
post.setEntity(buildHttpBody(builder, options));
return executeEnqueue(post);
}
/**
* Enqueues a doc with the POST method.
*
* @param inputSource Input source to send.
* @param options Options to send the file along with.
* @return A job response.
*/
@Override
public JobResponse reqPostInferenceEnqueue(
URLInputSource inputSource,
InferenceParameters options
) {
String url = this.mindeeSettings.getBaseUrl() + "/inferences/enqueue";
HttpPost post = buildHttpPost(url, options);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.EXTENDED);
builder.addTextBody(
"url",
inputSource.getUrl()
);
post.setEntity(buildHttpBody(builder, options));
return executeEnqueue(post);
}
/**
* Executes an enqueue action, common to URL & local inputs.
* @param post HTTP Post object.
* @return a valid job response.
*/
private JobResponse executeEnqueue(HttpPost post) {
mapper.findAndRegisterModules();
try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
return httpClient.execute(
post, response -> {
HttpEntity responseEntity = response.getEntity();
int statusCode = response.getCode();
if (isInvalidStatusCode(statusCode)) {
throw getHttpError(response);
}
try {
String raw = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
return deserializeOrThrow(raw, JobResponse.class, response.getCode());
} finally {
EntityUtils.consumeQuietly(responseEntity);
}
}
);
} catch (IOException err) {
throw new MindeeException(err.getMessage(), err);
}
}
@Override
public JobResponse reqGetJob(
String jobId
) {
String url = this.mindeeSettings.getBaseUrl() + "/jobs/" + jobId;
HttpGet get = new HttpGet(url);
if (this.mindeeSettings.getApiKey().isPresent()) {
get.setHeader(HttpHeaders.AUTHORIZATION, this.mindeeSettings.getApiKey().get());
}
get.setHeader(HttpHeaders.USER_AGENT, getUserAgent());
RequestConfig noRedirect =
RequestConfig.custom()
.setRedirectsEnabled(false)
.build();
get.setConfig(noRedirect);
mapper.findAndRegisterModules();
try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
return httpClient.execute(
get, response -> {
HttpEntity responseEntity = response.getEntity();
int statusCode = response.getCode();
if (isInvalidStatusCode(statusCode)) {
throw getHttpError(response);
}
try {
String raw = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
return deserializeOrThrow(raw, JobResponse.class, response.getCode());
} finally {
EntityUtils.consumeQuietly(responseEntity);
}
}
);
} catch (IOException err) {
throw new MindeeException(err.getMessage(), err);
}
}
@Override
public InferenceResponse reqGetInference(String inferenceId) {
String url = this.mindeeSettings.getBaseUrl() + "/inferences/" + inferenceId;
HttpGet get = new HttpGet(url);
if (this.mindeeSettings.getApiKey().isPresent()) {
get.setHeader(HttpHeaders.AUTHORIZATION, this.mindeeSettings.getApiKey().get());
}
get.setHeader(HttpHeaders.USER_AGENT, getUserAgent());
mapper.findAndRegisterModules();
try (CloseableHttpClient httpClient = httpClientBuilder
.build()) {
return httpClient.execute(
get,
response -> {
HttpEntity entity = response.getEntity();
int status = response.getCode();
try {
if (isInvalidStatusCode(status)) {
throw getHttpError(response);
}
String raw = EntityUtils.toString(entity, StandardCharsets.UTF_8);
return deserializeOrThrow(raw, InferenceResponse.class, status);
} finally {
EntityUtils.consumeQuietly(entity);
}
});
} catch (IOException err) {
throw new MindeeException(err.getMessage(), err);
}
}
private MindeeHttpExceptionV2 getHttpError(ClassicHttpResponse response) {
String rawBody;
try {
rawBody = response.getEntity() == null
? ""
: EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
ErrorResponse err = mapper.readValue(rawBody, ErrorResponse.class);
if (err.getDetail() == null) {
err = new ErrorResponse("Unknown error", response.getCode());
}
return new MindeeHttpExceptionV2(err.getStatus(), err.getDetail());
} catch (Exception e) {
return new MindeeHttpExceptionV2(response.getCode(), "Unknown error");
}
}
private HttpEntity buildHttpBody(
MultipartEntityBuilder builder,
InferenceParameters params
) {
if (params.getAlias() != null) {
builder.addTextBody(
"alias",
params.getAlias().toLowerCase()
);
}
builder.addTextBody("model_id", params.getModelId());
if (params.isRag()) {
builder.addTextBody("rag", "true");
}
if (params.getAlias() != null) {
builder.addTextBody("alias", params.getAlias());
}
if (params.getWebhookIds().length > 0) {
builder.addTextBody("webhook_ids", String.join(",", params.getWebhookIds()));
}
return builder.build();
}
private HttpPost buildHttpPost(
String url,
InferenceParameters params
) {
HttpPost post;
try {
URIBuilder uriBuilder = new URIBuilder(url);
post = new HttpPost(uriBuilder.build());
}
// This exception will never happen because we are providing the URL internally.
// Do this to avoid declaring the exception in the method signature.
catch (URISyntaxException err) {
return new HttpPost("invalid URI");
}
if (this.mindeeSettings.getApiKey().isPresent()) {
post.setHeader(HttpHeaders.AUTHORIZATION, this.mindeeSettings.getApiKey().get());
}
post.setHeader(HttpHeaders.USER_AGENT, getUserAgent());
return post;
}
private <R extends CommonResponse> R deserializeOrThrow(
String body, Class<R> clazz, int httpStatus) throws MindeeHttpExceptionV2 {
if (httpStatus >= 200 && httpStatus < 400) {
try {
R model = mapper.readerFor(clazz).readValue(body);
model.setRawResponse(body);
return model;
} catch (Exception exception) {
throw new MindeeException("Couldn't deserialize server response:\n" + exception.getMessage());
}
}
ErrorResponse err;
try {
err = mapper.readValue(body, ErrorResponse.class);
if (err.getDetail() == null) {
err = new ErrorResponse("Unknown error", httpStatus);
}
} catch (Exception ignored) {
err = new ErrorResponse("Unknown error", httpStatus);
}
throw new MindeeHttpExceptionV2(err.getStatus(), err.getDetail());
}
}