2020import java .util .Map ;
2121import java .util .function .Function ;
2222import lombok .Builder ;
23- import org .apache .http .HttpEntity ;
24- import org .apache .http .HttpHeaders ;
25- import org .apache .http .NameValuePair ;
26- import org .apache .http .client . methods . CloseableHttpResponse ;
27- import org .apache .http .client . methods . HttpGet ;
28- import org .apache .http .client . methods . HttpPost ;
29- import org .apache .http . client . utils . URIBuilder ;
30- import org .apache .http . entity .ContentType ;
31- import org .apache .http . entity . StringEntity ;
32- import org .apache .http . entity . mime . HttpMultipartMode ;
33- import org .apache .http . entity . mime . MultipartEntityBuilder ;
34- import org .apache .http .impl . client . CloseableHttpClient ;
35- import org .apache .http . impl . client . HttpClientBuilder ;
36- import org .apache .http . message . BasicNameValuePair ;
23+ import org .apache .hc . client5 . http .classic . methods . HttpGet ;
24+ import org .apache .hc . client5 . http .classic . methods . HttpPost ;
25+ import org .apache .hc . client5 . http .entity . mime . HttpMultipartMode ;
26+ import org .apache .hc . client5 . http .entity . mime . MultipartEntityBuilder ;
27+ import org .apache .hc . client5 . http .impl . classic . CloseableHttpClient ;
28+ import org .apache .hc . client5 . http .impl . classic . HttpClientBuilder ;
29+ import org .apache .hc . core5 . http . ClassicHttpResponse ;
30+ import org .apache .hc . core5 . http .ContentType ;
31+ import org .apache .hc . core5 . http . HttpEntity ;
32+ import org .apache .hc . core5 . http . HttpHeaders ;
33+ import org .apache .hc . core5 . http . NameValuePair ;
34+ import org .apache .hc . core5 . http .io . entity . StringEntity ;
35+ import org .apache .hc . core5 . http . message . BasicNameValuePair ;
36+ import org .apache .hc . core5 . net . URIBuilder ;
3737
3838/**
3939 * HTTP Client class.
@@ -152,31 +152,33 @@ public <DocT extends Inference> AsyncPredictResponse<DocT> documentQueueGet(
152152 }
153153 get .setHeader (HttpHeaders .USER_AGENT , getUserAgent ());
154154
155- try (
156- CloseableHttpClient httpClient = httpClientBuilder .build ();
157- CloseableHttpResponse response = httpClient .execute (get )
158- ) {
159- HttpEntity responseEntity = response .getEntity ();
160- int statusCode = response .getStatusLine ().getStatusCode ();
161- if (!is2xxStatusCode (statusCode )) {
162- throw getHttpError (parametricType , response );
163- }
164- String rawResponse = readRawResponse (responseEntity );
165- AsyncPredictResponse <DocT > mappedResponse = mapper .readValue (rawResponse , parametricType );
166- mappedResponse .setRawResponse (rawResponse );
167- if (
168- mappedResponse .getJob () != null
169- && mappedResponse .getJob ().getError () != null
170- && mappedResponse .getJob ().getError ().getCode () != null
171- ) {
172- throw new MindeeHttpException (
173- 500 ,
174- mappedResponse .getJob ().getError ().getMessage (),
175- mappedResponse .getJob ().getError ().getDetails ().toString (),
176- mappedResponse .getJob ().getError ().getCode ()
177- );
178- }
179- return mappedResponse ;
155+ try (CloseableHttpClient httpClient = httpClientBuilder .build ()) {
156+ return httpClient .execute (
157+ get , response -> {
158+ HttpEntity responseEntity = response .getEntity ();
159+ int statusCode = response .getCode ();
160+ if (!is2xxStatusCode (statusCode )) {
161+ throw getHttpError (parametricType , response );
162+ }
163+ String rawResponse = readRawResponse (responseEntity );
164+ AsyncPredictResponse <DocT > mappedResponse =
165+ mapper .readValue (rawResponse , parametricType );
166+ mappedResponse .setRawResponse (rawResponse );
167+ if (
168+ mappedResponse .getJob () != null
169+ && mappedResponse .getJob ().getError () != null
170+ && mappedResponse .getJob ().getError ().getCode () != null
171+ ) {
172+ throw new MindeeHttpException (
173+ 500 ,
174+ mappedResponse .getJob ().getError ().getMessage (),
175+ mappedResponse .getJob ().getError ().getDetails ().toString (),
176+ mappedResponse .getJob ().getError ().getCode ()
177+ );
178+ }
179+ return mappedResponse ;
180+ }
181+ );
180182 } catch (IOException err ) {
181183 throw new MindeeException (err .getMessage (), err );
182184 }
@@ -200,22 +202,23 @@ public <DocT extends Inference> PredictResponse<DocT> predictPost(
200202 PredictResponse .class ,
201203 documentClass
202204 );
203- try (
204- CloseableHttpClient httpClient = httpClientBuilder .build ();
205- CloseableHttpResponse response = httpClient .execute (post )
206- ) {
207- HttpEntity responseEntity = response .getEntity ();
208- int statusCode = response .getStatusLine ().getStatusCode ();
209- if (!is2xxStatusCode (statusCode )) {
210- throw getHttpError (parametricType , response );
211- }
212- if (responseEntity .getContentLength () == 0 ) {
213- throw new MindeeException ("Empty response from server." );
214- }
215- String rawResponse = readRawResponse (responseEntity );
216- PredictResponse <DocT > mappedResponse = mapper .readValue (rawResponse , parametricType );
217- mappedResponse .setRawResponse (rawResponse );
218- return mappedResponse ;
205+ try (CloseableHttpClient httpClient = httpClientBuilder .build ()) {
206+ return httpClient .execute (
207+ post , response -> {
208+ HttpEntity responseEntity = response .getEntity ();
209+ int statusCode = response .getCode ();
210+ if (!is2xxStatusCode (statusCode )) {
211+ throw getHttpError (parametricType , response );
212+ }
213+ if (responseEntity .getContentLength () == 0 ) {
214+ throw new MindeeException ("Empty response from server." );
215+ }
216+ String rawResponse = readRawResponse (responseEntity );
217+ PredictResponse <DocT > mappedResponse = mapper .readValue (rawResponse , parametricType );
218+ mappedResponse .setRawResponse (rawResponse );
219+ return mappedResponse ;
220+ }
221+ );
219222 } catch (IOException err ) {
220223 throw new MindeeException (err .getMessage (), err );
221224 }
@@ -239,22 +242,24 @@ public <DocT extends Inference> AsyncPredictResponse<DocT> predictAsyncPost(
239242 AsyncPredictResponse .class ,
240243 documentClass
241244 );
242- try (
243- CloseableHttpClient httpClient = httpClientBuilder .build ();
244- CloseableHttpResponse response = httpClient .execute (post )
245- ) {
246- HttpEntity responseEntity = response .getEntity ();
247- int statusCode = response .getStatusLine ().getStatusCode ();
248- if (!is2xxStatusCode (statusCode )) {
249- throw getHttpError (parametricType , response );
250- }
251- if (responseEntity .getContentLength () == 0 ) {
252- throw new MindeeException ("Empty response from server." );
253- }
254- String rawResponse = readRawResponse (responseEntity );
255- AsyncPredictResponse <DocT > mappedResponse = mapper .readValue (rawResponse , parametricType );
256- mappedResponse .setRawResponse (rawResponse );
257- return mappedResponse ;
245+ try (CloseableHttpClient httpClient = httpClientBuilder .build ()) {
246+ return httpClient .execute (
247+ post , response -> {
248+ HttpEntity responseEntity = response .getEntity ();
249+ int statusCode = response .getCode ();
250+ if (!is2xxStatusCode (statusCode )) {
251+ throw getHttpError (parametricType , response );
252+ }
253+ if (responseEntity .getContentLength () == 0 ) {
254+ throw new MindeeException ("Empty response from server." );
255+ }
256+ String rawResponse = readRawResponse (responseEntity );
257+ AsyncPredictResponse <DocT > mappedResponse =
258+ mapper .readValue (rawResponse , parametricType );
259+ mappedResponse .setRawResponse (rawResponse );
260+ return mappedResponse ;
261+ }
262+ );
258263 } catch (IOException err ) {
259264 throw new MindeeException (err .getMessage (), err );
260265 }
@@ -279,22 +284,21 @@ public <DocT extends Inference> WorkflowResponse<DocT> executeWorkflowPost(
279284 WorkflowResponse .class ,
280285 documentClass
281286 );
282- try (
283- CloseableHttpClient httpClient = httpClientBuilder .build ();
284- CloseableHttpResponse response = httpClient .execute (post )
285- ) {
286- HttpEntity responseEntity = response .getEntity ();
287- int statusCode = response .getStatusLine ().getStatusCode ();
288- if (!is2xxStatusCode (statusCode )) {
289- throw getHttpError (parametricType , response );
290- }
291- if (responseEntity .getContentLength () == 0 ) {
292- throw new MindeeException ("Empty response from server." );
293- }
294- String rawResponse = readRawResponse (responseEntity );
295- WorkflowResponse <DocT > mappedResponse = mapper .readValue (rawResponse , parametricType );
296- mappedResponse .setRawResponse (rawResponse );
297- return mappedResponse ;
287+ try (CloseableHttpClient httpClient = httpClientBuilder .build ()) {
288+ return httpClient .execute (post , response -> {
289+ HttpEntity responseEntity = response .getEntity ();
290+ int statusCode = response .getCode ();
291+ if (!is2xxStatusCode (statusCode )) {
292+ throw getHttpError (parametricType , response );
293+ }
294+ if (responseEntity .getContentLength () == 0 ) {
295+ throw new MindeeException ("Empty response from server." );
296+ }
297+ String rawResponse = readRawResponse (responseEntity );
298+ WorkflowResponse <DocT > mappedResponse = mapper .readValue (rawResponse , parametricType );
299+ mappedResponse .setRawResponse (rawResponse );
300+ return mappedResponse ;
301+ });
298302 } catch (IOException err ) {
299303 throw new MindeeException (err .getMessage (), err );
300304 }
@@ -303,9 +307,9 @@ public <DocT extends Inference> WorkflowResponse<DocT> executeWorkflowPost(
303307
304308 private <ResponseT extends ApiResponse > MindeeHttpException getHttpError (
305309 JavaType parametricType ,
306- CloseableHttpResponse response
310+ ClassicHttpResponse response
307311 ) {
308- int statusCode = response .getStatusLine (). getStatusCode ();
312+ int statusCode = response .getCode ();
309313 String message = "HTTP Status " + statusCode + " - " ;
310314 String details ;
311315 String errorCode ;
@@ -392,7 +396,7 @@ private HttpEntity buildHttpBody(
392396 ) throws JsonProcessingException {
393397 if (requestParameters .getFile () != null ) {
394398 MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
395- builder .setMode (HttpMultipartMode .BROWSER_COMPATIBLE );
399+ builder .setMode (HttpMultipartMode .EXTENDED );
396400 builder .addBinaryBody (
397401 "document" ,
398402 requestParameters .getFile (),
0 commit comments