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
@@ -138,31 +114,10 @@ public JobResponse reqGetJob(String jobId) {
138114 var url = this .mindeeSettings .getBaseUrl () + "/jobs/" + jobId ;
139115 var get = new HttpGet (url );
140116
141- if (this .mindeeSettings .getApiKey ().isPresent ()) {
142- get .setHeader (HttpHeaders .AUTHORIZATION , this .mindeeSettings .getApiKey ().get ());
143- }
144- get .setHeader (HttpHeaders .USER_AGENT , getUserAgent ());
145117 var noRedirect = RequestConfig .custom ().setRedirectsEnabled (false ).build ();
146118 get .setConfig (noRedirect );
147119
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- }
120+ return this .executeAPIRequest (get , JobResponse .class );
166121 }
167122
168123 @ Override
@@ -179,25 +134,54 @@ public <TResponse extends CommonResponse> TResponse reqGetResult(
179134 inferenceId
180135 );
181136 var get = new HttpGet (url );
137+ return executeAPIRequest (get , responseClass );
138+ }
182139
140+ @ Override
141+ public SearchResponse reqGetSearchModels (String modelName , String modelType ) {
142+ URIBuilder url ;
143+ try {
144+ url = new URIBuilder (this .mindeeSettings .getBaseUrl () + "/search/models" );
145+ } catch (URISyntaxException e ) {
146+ throw new RuntimeException (e );
147+ }
148+ if (modelName != null ) {
149+ url .addParameter ("name" , modelName );
150+ }
151+ if (modelType != null ) {
152+ url .addParameter ("type" , modelType );
153+ }
154+ var get = new HttpGet (url .toString ());
155+ return executeAPIRequest (get , SearchResponse .class );
156+ }
157+
158+ /**
159+ * Executes an enqueue action, common to URL & local inputs.
160+ *
161+ * @param apiRequest HTTP request object.
162+ * @return a valid job response.
163+ */
164+ private <TResponse extends CommonResponse > TResponse executeAPIRequest (
165+ HttpUriRequestBase apiRequest ,
166+ Class <TResponse > responseClass
167+ ) {
183168 if (this .mindeeSettings .getApiKey ().isPresent ()) {
184- get .setHeader (HttpHeaders .AUTHORIZATION , this .mindeeSettings .getApiKey ().get ());
169+ apiRequest .setHeader (HttpHeaders .AUTHORIZATION , this .mindeeSettings .getApiKey ().get ());
185170 }
186- get .setHeader (HttpHeaders .USER_AGENT , getUserAgent ());
171+ apiRequest .setHeader (HttpHeaders .USER_AGENT , getUserAgent ());
187172
188173 try (var httpClient = httpClientBuilder .build ()) {
189-
190- return httpClient .execute (get , response -> {
191- var entity = response .getEntity ();
192- var status = response .getCode ();
174+ return httpClient .execute (apiRequest , response -> {
175+ var responseEntity = response .getEntity ();
176+ var statusCode = response .getCode ();
177+ if (isInvalidStatusCode (statusCode )) {
178+ throw getHttpError (response );
179+ }
193180 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 );
181+ var raw = EntityUtils .toString (response .getEntity (), StandardCharsets .UTF_8 );
182+ return deserializeOrThrow (raw , responseClass , response .getCode ());
199183 } finally {
200- EntityUtils .consumeQuietly (entity );
184+ EntityUtils .consumeQuietly (responseEntity );
201185 }
202186 });
203187 } catch (IOException err ) {
@@ -235,11 +219,6 @@ private HttpPost buildHttpPost(String url) {
235219 catch (URISyntaxException err ) {
236220 return new HttpPost ("invalid URI" );
237221 }
238-
239- if (this .mindeeSettings .getApiKey ().isPresent ()) {
240- post .setHeader (HttpHeaders .AUTHORIZATION , this .mindeeSettings .getApiKey ().get ());
241- }
242- post .setHeader (HttpHeaders .USER_AGENT , getUserAgent ());
243222 return post ;
244223 }
245224
0 commit comments