1010import com .mindee .v2 .parsing .CommonResponse ;
1111import com .mindee .v2 .parsing .JobResponse ;
1212import com .mindee .v2 .parsing .error .ErrorResponse ;
13+ import com .mindee .v2 .parsing .search .SearchResponse ;
1314import java .io .IOException ;
1415import java .net .URISyntaxException ;
1516import java .nio .charset .StandardCharsets ;
1617import lombok .Builder ;
1718import org .apache .hc .client5 .http .classic .methods .HttpGet ;
1819import org .apache .hc .client5 .http .classic .methods .HttpPost ;
20+ import org .apache .hc .client5 .http .classic .methods .HttpUriRequestBase ;
1921import org .apache .hc .client5 .http .config .RequestConfig ;
2022import org .apache .hc .client5 .http .entity .mime .HttpMultipartMode ;
2123import org .apache .hc .client5 .http .entity .mime .MultipartEntityBuilder ;
@@ -82,7 +84,7 @@ public JobResponse reqPostEnqueue(LocalInputSource inputSource, BaseParameters o
8284 inputSource .getFilename ()
8385 );
8486 post .setEntity (options .buildHttpBody (builder ).build ());
85- return executeEnqueue (post );
87+ return executeAPIRequest (post , JobResponse . class );
8688 }
8789
8890 /**
@@ -103,33 +105,7 @@ public JobResponse reqPostEnqueue(URLInputSource inputSource, BaseParameters opt
103105 builder .setMode (HttpMultipartMode .EXTENDED );
104106 builder .addTextBody ("url" , inputSource .getUrl ().toString ());
105107 post .setEntity (options .buildHttpBody (builder ).build ());
106- return executeEnqueue (post );
107- }
108-
109- /**
110- * Executes an enqueue action, common to URL & local inputs.
111- *
112- * @param post HTTP Post object.
113- * @return a valid job response.
114- */
115- private JobResponse executeEnqueue (HttpPost post ) {
116- try (var httpClient = httpClientBuilder .build ()) {
117- return httpClient .execute (post , response -> {
118- var responseEntity = response .getEntity ();
119- var statusCode = response .getCode ();
120- if (isInvalidStatusCode (statusCode )) {
121- throw getHttpError (response );
122- }
123- try {
124- var raw = EntityUtils .toString (response .getEntity (), StandardCharsets .UTF_8 );
125- return deserializeOrThrow (raw , JobResponse .class , response .getCode ());
126- } finally {
127- EntityUtils .consumeQuietly (responseEntity );
128- }
129- });
130- } catch (IOException err ) {
131- throw new MindeeException (err .getMessage (), err );
132- }
108+ return executeAPIRequest (post , JobResponse .class );
133109 }
134110
135111 @ Override
@@ -145,24 +121,7 @@ public JobResponse reqGetJob(String jobId) {
145121 var noRedirect = RequestConfig .custom ().setRedirectsEnabled (false ).build ();
146122 get .setConfig (noRedirect );
147123
148- try (var httpClient = httpClientBuilder .build ()) {
149- return httpClient .execute (get , response -> {
150- var responseEntity = response .getEntity ();
151- var statusCode = response .getCode ();
152- if (isInvalidStatusCode (statusCode )) {
153- throw getHttpError (response );
154- }
155- try {
156- var raw = EntityUtils .toString (response .getEntity (), StandardCharsets .UTF_8 );
157-
158- return deserializeOrThrow (raw , JobResponse .class , response .getCode ());
159- } finally {
160- EntityUtils .consumeQuietly (responseEntity );
161- }
162- });
163- } catch (IOException err ) {
164- throw new MindeeException (err .getMessage (), err );
165- }
124+ return this .executeAPIRequest (get , JobResponse .class );
166125 }
167126
168127 @ Override
@@ -179,25 +138,54 @@ public <TResponse extends CommonResponse> TResponse reqGetResult(
179138 inferenceId
180139 );
181140 var get = new HttpGet (url );
141+ return executeAPIRequest (get , responseClass );
142+ }
143+
144+ @ Override
145+ public SearchResponse reqGetSearchModels (String modelName , String modelType ) {
146+ URIBuilder url ;
147+ try {
148+ url = new URIBuilder (this .mindeeSettings .getBaseUrl () + "/models/search" );
149+ } catch (URISyntaxException e ) {
150+ throw new RuntimeException (e );
151+ }
152+ if (modelName != null ) {
153+ url .addParameter ("name" , modelName );
154+ }
155+ if (modelType != null ) {
156+ url .addParameter ("type" , modelType );
157+ }
158+ var get = new HttpGet (url .toString ());
159+ return executeAPIRequest (get , SearchResponse .class );
160+ }
182161
162+ /**
163+ * Executes an enqueue action, common to URL & local inputs.
164+ *
165+ * @param apiRequest HTTP request object.
166+ * @return a valid job response.
167+ */
168+ private <TResponse extends CommonResponse > TResponse executeAPIRequest (
169+ HttpUriRequestBase apiRequest ,
170+ Class <TResponse > responseClass
171+ ) {
183172 if (this .mindeeSettings .getApiKey ().isPresent ()) {
184- get .setHeader (HttpHeaders .AUTHORIZATION , this .mindeeSettings .getApiKey ().get ());
173+ apiRequest .setHeader (HttpHeaders .AUTHORIZATION , this .mindeeSettings .getApiKey ().get ());
185174 }
186- get .setHeader (HttpHeaders .USER_AGENT , getUserAgent ());
175+ apiRequest .setHeader (HttpHeaders .USER_AGENT , getUserAgent ());
187176
188177 try (var httpClient = httpClientBuilder .build ()) {
189-
190- return httpClient .execute (get , response -> {
191- var entity = response .getEntity ();
192- var status = response .getCode ();
178+ return httpClient .execute (apiRequest , response -> {
179+ var responseEntity = response .getEntity ();
180+ var statusCode = response .getCode ();
181+ if (isInvalidStatusCode (statusCode )) {
182+ throw getHttpError (response );
183+ }
193184 try {
194- if (isInvalidStatusCode (status )) {
195- throw getHttpError (response );
196- }
197- var raw = EntityUtils .toString (entity , StandardCharsets .UTF_8 );
198- return deserializeOrThrow (raw , responseClass , status );
185+ var raw = EntityUtils .toString (response .getEntity (), StandardCharsets .UTF_8 );
186+ return deserializeOrThrow (raw , responseClass , response .getCode ());
199187 } finally {
200- EntityUtils .consumeQuietly (entity );
188+ EntityUtils .consumeQuietly (responseEntity );
201189 }
202190 });
203191 } catch (IOException err ) {
@@ -235,11 +223,6 @@ private HttpPost buildHttpPost(String url) {
235223 catch (URISyntaxException err ) {
236224 return new HttpPost ("invalid URI" );
237225 }
238-
239- if (this .mindeeSettings .getApiKey ().isPresent ()) {
240- post .setHeader (HttpHeaders .AUTHORIZATION , this .mindeeSettings .getApiKey ().get ());
241- }
242- post .setHeader (HttpHeaders .USER_AGENT , getUserAgent ());
243226 return post ;
244227 }
245228
0 commit comments