diff --git a/src/main/java/com/mindee/v2/MindeeClient.java b/src/main/java/com/mindee/v2/MindeeClient.java index 2648fa688..58c8ba9e0 100644 --- a/src/main/java/com/mindee/v2/MindeeClient.java +++ b/src/main/java/com/mindee/v2/MindeeClient.java @@ -23,6 +23,8 @@ * Entry point for the Mindee **V2** API features. */ public class MindeeClient { + private static final System.Logger logger = System.getLogger(MindeeClient.class.getName()); + private final MindeeApiV2 mindeeApi; /** Uses an API key read from the environment variables. */ @@ -50,6 +52,7 @@ public JobResponse enqueue( LocalInputSource inputSource, BaseProductParameters params ) throws IOException { + logger.log(System.Logger.Level.INFO, "Enqueuing: local source"); return mindeeApi.reqPostEnqueue(inputSource, params); } @@ -63,6 +66,7 @@ public JobResponse enqueue( URLInputSource inputSource, BaseProductParameters params ) throws IOException { + logger.log(System.Logger.Level.INFO, "Enqueuing: URL source"); inputSource.validateSecure(); return mindeeApi.reqPostEnqueue(inputSource, params); } @@ -72,6 +76,7 @@ public JobResponse enqueue( * Can be used for polling. */ public JobResponse getJob(String jobId) { + logger.log(System.Logger.Level.INFO, "Getting job ID: {0}", jobId); if (jobId == null || jobId.trim().isEmpty()) { throw new IllegalArgumentException("jobId must not be null or blank."); } @@ -86,6 +91,7 @@ public TResponse getResult( Class responseClass, String inferenceId ) { + logger.log(System.Logger.Level.INFO, "Getting result with ID: {0}", inferenceId); if (inferenceId == null || inferenceId.trim().isEmpty()) { throw new IllegalArgumentException("inferenceId must not be null or blank."); } @@ -100,6 +106,7 @@ public TResponse getResultFromUrl( Class responseClass, String inferenceUrl ) { + logger.log(System.Logger.Level.INFO, "Getting result at: {0}", inferenceUrl); if (inferenceUrl == null || inferenceUrl.trim().isEmpty()) { throw new IllegalArgumentException("inferenceUrl must not be null or blank."); } @@ -147,7 +154,13 @@ public TResponse enqueueAndGetResult( PollingOptions pollingOptions ) throws IOException, InterruptedException { JobResponse job = enqueue(inputSource, params); - return pollAndFetch(responseClass, job, pollingOptions); + logger + .log( + System.Logger.Level.INFO, + "Successfully enqueued document with job ID {0}", + job.getJob().getId() + ); + return pollForResult(responseClass, job, pollingOptions); } /** @@ -192,7 +205,13 @@ public TResponse enqueueAndGetResult( ) throws IOException, InterruptedException { inputSource.validateSecure(); JobResponse job = enqueue(inputSource, params); - return pollAndFetch(responseClass, job, pollingOptions); + logger + .log( + System.Logger.Level.INFO, + "Successfully enqueued document with job ID {0}", + job.getJob().getId() + ); + return pollForResult(responseClass, job, pollingOptions); } /** @@ -251,11 +270,17 @@ public SearchResponse searchModels(String modelName, String modelType) { * @return an instance of {@link ExtractionResponse}. * @throws InterruptedException Throws if interrupted. */ - private TResponse pollAndFetch( + private TResponse pollForResult( Class responseClass, JobResponse initialJob, PollingOptions pollingOptions ) throws InterruptedException { + logger + .log( + System.Logger.Level.DEBUG, + "Waiting {0} seconds before attempting to retrieve the result...", + pollingOptions.getInitialDelaySec() + ); interruptibleSleep((long) (pollingOptions.getInitialDelaySec() * 1000), pollingOptions); JobResponse resp = initialJob; @@ -265,12 +290,20 @@ private TResponse pollAndFetch( while (attempts < max) { interruptibleSleep(intervalMillis, pollingOptions); + logger.log(System.Logger.Level.DEBUG, "Poll attempt {0} of {1}", attempts + 1, max); resp = getJob(initialJob.getJob().getId()); if (resp.getJob().getStatus().equals("Failed")) { attempts = max; } if (resp.getJob().getStatus().equals("Processed")) { + logger + .log( + System.Logger.Level.DEBUG, + "Job ID {0} completed processing at: {1}", + resp.getJob().getId(), + resp.getJob().getCompletedAt() + ); return getResult(responseClass, resp.getJob().getId()); } attempts++; diff --git a/src/main/java/com/mindee/v2/http/MindeeHttpApiV2.java b/src/main/java/com/mindee/v2/http/MindeeHttpApiV2.java index 632e2a600..5cba4a7d5 100644 --- a/src/main/java/com/mindee/v2/http/MindeeHttpApiV2.java +++ b/src/main/java/com/mindee/v2/http/MindeeHttpApiV2.java @@ -36,6 +36,7 @@ */ public final class MindeeHttpApiV2 extends MindeeApiV2 { + private static final System.Logger logger = System.getLogger(MindeeHttpApiV2.class.getName()); private static final ObjectMapper mapper = JsonMapper.builder().findAndAddModules().build(); /** @@ -91,6 +92,8 @@ public JobResponse reqPostEnqueue( ); parameters.getRequestParameters().forEach(builder::addTextBody); post.setEntity(builder.build()); + + logger.log(System.Logger.Level.DEBUG, "HTTP POST to {0} ...", url); return executeAPIRequest(post, JobResponse.class); } @@ -113,6 +116,8 @@ public JobResponse reqPostEnqueue(URLInputSource inputSource, BaseProductParamet builder.addTextBody("url", inputSource.getUrl().toString()); options.getRequestParameters().forEach(builder::addTextBody); post.setEntity(builder.build()); + + logger.log(System.Logger.Level.DEBUG, "HTTP POST to {0} ...", url); return executeAPIRequest(post, JobResponse.class); } @@ -125,6 +130,7 @@ public JobResponse reqGetJobById(String jobId) { var noRedirect = RequestConfig.custom().setRedirectsEnabled(false).build(); get.setConfig(noRedirect); + logger.log(System.Logger.Level.DEBUG, "HTTP GET to {0}...", url); return this.executeAPIRequest(get, JobResponse.class); } @@ -157,6 +163,8 @@ public TResponse reqGetResultByUrl( } validateInferenceUrl(inferenceUrl); var get = new HttpGet(inferenceUrl); + + logger.log(System.Logger.Level.DEBUG, "HTTP GET to {0}...", inferenceUrl); return executeAPIRequest(get, responseClass); } @@ -173,6 +181,8 @@ public TSearchResponse reqGetSearch } parameters.getRequestParameters().forEach(url::addParameter); var get = new HttpGet(url.toString()); + + logger.log(System.Logger.Level.INFO, "Searching {0} ...", productInfo.slug()); return this.executeAPIRequest(get, parameters.getResponseClass()); } @@ -187,6 +197,8 @@ public SearchResponse reqGetSearch(ModelSearchParameters parameters) { } parameters.getRequestParameters().forEach(url::addParameter); var get = new HttpGet(url.toString()); + + logger.log(System.Logger.Level.INFO, "Model search..."); return this.executeAPIRequest(get, SearchResponse.class); } @@ -286,6 +298,7 @@ private TResponse executeAPIRequest( } try { var raw = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); + logger.log(System.Logger.Level.DEBUG, "HTTP response: {0}", raw); return deserializeOrThrow(raw, responseClass, response.getCode()); } finally { EntityUtils.consumeQuietly(responseEntity); @@ -303,6 +316,8 @@ private MindeeHttpExceptionV2 getHttpError(ClassicHttpResponse response) { ? "" : EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); + logger.log(System.Logger.Level.DEBUG, "HTTP response: {0}", rawBody); + var errorResponse = mapper.readValue(rawBody, ErrorResponse.class); if (errorResponse.getDetail() == null) { diff --git a/src/test/resources b/src/test/resources index 4b7f33766..8b8423d23 160000 --- a/src/test/resources +++ b/src/test/resources @@ -1 +1 @@ -Subproject commit 4b7f33766fab0e67804b84447b73c80a902d886a +Subproject commit 8b8423d239a360f7a8ac72d77e53fb644a76e345