55import com .mindee .MindeeException ;
66import com .mindee .MindeeSettingsV2 ;
77import com .mindee .input .LocalInputSource ;
8- import com .mindee .parsing .v2 .InferenceResponse ;
9- import com .mindee .parsing .v2 .JobResponse ;
108import com .mindee .parsing .v2 .CommonResponse ;
119import com .mindee .parsing .v2 .ErrorResponse ;
10+ import com .mindee .parsing .v2 .InferenceResponse ;
11+ import com .mindee .parsing .v2 .JobResponse ;
1212import java .io .IOException ;
1313import java .net .URISyntaxException ;
1414import java .nio .charset .StandardCharsets ;
15- import java .util .ArrayList ;
16- import java .util .List ;
1715import lombok .Builder ;
1816import org .apache .hc .client5 .http .classic .methods .HttpGet ;
1917import org .apache .hc .client5 .http .classic .methods .HttpPost ;
2523import org .apache .hc .core5 .http .ContentType ;
2624import org .apache .hc .core5 .http .HttpEntity ;
2725import org .apache .hc .core5 .http .HttpHeaders ;
28- import org .apache .hc .core5 .http .NameValuePair ;
2926import org .apache .hc .core5 .http .io .entity .EntityUtils ;
30- import org .apache .hc .core5 .http .message .BasicNameValuePair ;
3127import org .apache .hc .core5 .net .URIBuilder ;
3228
3329/**
@@ -103,6 +99,10 @@ public CommonResponse getInferenceFromQueue(
10399 String url = this .mindeeSettings .getBaseUrl () + "/inferences/" + jobId ;
104100 HttpGet get = new HttpGet (url );
105101
102+ if (this .mindeeSettings .getApiKey ().isPresent ()) {
103+ get .setHeader (HttpHeaders .AUTHORIZATION , this .mindeeSettings .getApiKey ().get ());
104+ }
105+ get .setHeader (HttpHeaders .USER_AGENT , getUserAgent ());
106106 mapper .findAndRegisterModules ();
107107 try (CloseableHttpClient httpClient = httpClientBuilder .build ()) {
108108 return httpClient .execute (
@@ -120,34 +120,13 @@ public CommonResponse getInferenceFromQueue(
120120 /* make sure the connection can be reused even if parsing fails */
121121 EntityUtils .consumeQuietly (responseEntity );
122122 }
123-
124123 }
125124 );
126125 } catch (IOException err ) {
127126 throw new MindeeException (err .getMessage (), err );
128127 }
129128 }
130129
131- private List <NameValuePair > buildPostParams (
132- InferencePredictOptions options
133- ) {
134- ArrayList <NameValuePair > params = new ArrayList <NameValuePair >();
135- params .add (new BasicNameValuePair ("model_id" , options .getModelId ()));
136- if (options .isFullText ()) {
137- params .add (new BasicNameValuePair ("full_text_ocr" , "true" ));
138- }
139- if (options .isRag ()) {
140- params .add (new BasicNameValuePair ("rag" , "true" ));
141- }
142- if (options .getAlias () != null ) {
143- params .add (new BasicNameValuePair ("alias" , options .getAlias ()));
144- }
145- if (!options .getWebhookIds ().isEmpty ()) {
146- params .add (new BasicNameValuePair ("webhook_ids" , String .join ("," , options .getWebhookIds ())));
147- }
148- return params ;
149- }
150-
151130 private MindeeHttpExceptionV2 getHttpError (ClassicHttpResponse response ) {
152131 String rawBody ;
153132 try {
@@ -175,7 +154,7 @@ private HttpEntity buildHttpBody(
175154 MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
176155 builder .setMode (HttpMultipartMode .EXTENDED );
177156 builder .addBinaryBody (
178- "document " ,
157+ "file " ,
179158 inputSource .getFile (),
180159 ContentType .DEFAULT_BINARY ,
181160 inputSource .getFilename ()
@@ -187,6 +166,20 @@ private HttpEntity buildHttpBody(
187166 options .getAlias ().toLowerCase ()
188167 );
189168 }
169+
170+ builder .addTextBody ("model_id" , options .getModelId ());
171+ if (options .isFullText ()) {
172+ builder .addTextBody ("full_text_ocr" , "true" );
173+ }
174+ if (options .isRag ()) {
175+ builder .addTextBody ("rag" , "true" );
176+ }
177+ if (options .getAlias () != null ) {
178+ builder .addTextBody ("alias" , options .getAlias ());
179+ }
180+ if (!options .getWebhookIds ().isEmpty ()) {
181+ builder .addTextBody ("webhook_ids" , String .join ("," , options .getWebhookIds ()));
182+ }
190183 return builder .build ();
191184 }
192185
@@ -199,7 +192,6 @@ private HttpPost buildHttpPost(
199192 HttpPost post ;
200193 try {
201194 URIBuilder uriBuilder = new URIBuilder (url );
202- uriBuilder .addParameters (buildPostParams (options ));
203195 post = new HttpPost (uriBuilder .build ());
204196 }
205197 // This exception will never happen because we are providing the URL internally.
@@ -222,10 +214,11 @@ private <R extends CommonResponse> R deserializeOrThrow(
222214
223215 if (httpStatus >= 200 && httpStatus < 300 ) {
224216 try {
225- R model = mapper .readValue (body , clazz );
217+ R model = mapper .readerFor ( clazz ). readValue (body );
226218 model .setRawResponse (body );
227219 return model ;
228- } catch (Exception ignored ) {
220+ } catch (Exception exception ) {
221+ throw new MindeeException ("Couldn't deserialize server response:\n " + exception .getMessage ());
229222 }
230223 }
231224
0 commit comments