Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/main/java/com/mindee/v2/MindeeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
Expand All @@ -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.");
}
Expand All @@ -86,6 +91,7 @@ public <TResponse extends CommonResponse> TResponse getResult(
Class<TResponse> 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.");
}
Expand All @@ -100,6 +106,7 @@ public <TResponse extends CommonResponse> TResponse getResultFromUrl(
Class<TResponse> responseClass,
String inferenceUrl
) {
logger.log(System.Logger.Level.INFO, "Getting result at: {0}", inferenceUrl);
Comment thread
ianardee marked this conversation as resolved.
if (inferenceUrl == null || inferenceUrl.trim().isEmpty()) {
throw new IllegalArgumentException("inferenceUrl must not be null or blank.");
}
Expand Down Expand Up @@ -147,7 +154,13 @@ public <TResponse extends CommonResponse> 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);
}

/**
Expand Down Expand Up @@ -192,7 +205,13 @@ public <TResponse extends CommonResponse> 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);
}

/**
Expand Down Expand Up @@ -251,11 +270,17 @@ public SearchResponse searchModels(String modelName, String modelType) {
* @return an instance of {@link ExtractionResponse}.
* @throws InterruptedException Throws if interrupted.
*/
private <TResponse extends CommonResponse> TResponse pollAndFetch(
private <TResponse extends CommonResponse> TResponse pollForResult(
Class<TResponse> 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;
Expand All @@ -265,12 +290,20 @@ private <TResponse extends CommonResponse> 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++;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/mindee/v2/http/MindeeHttpApiV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/**
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -157,6 +163,8 @@ public <TResponse extends CommonResponse> TResponse reqGetResultByUrl(
}
validateInferenceUrl(inferenceUrl);
var get = new HttpGet(inferenceUrl);

logger.log(System.Logger.Level.DEBUG, "HTTP GET to {0}...", inferenceUrl);
return executeAPIRequest(get, responseClass);
}

Expand All @@ -173,6 +181,8 @@ public <TSearchResponse extends BaseSearchResponse> 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());
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -286,6 +298,7 @@ private <TResponse extends CommonResponse> TResponse executeAPIRequest(
}
try {
var raw = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
logger.log(System.Logger.Level.DEBUG, "HTTP response: {0}", raw);
Comment thread
ianardee marked this conversation as resolved.
return deserializeOrThrow(raw, responseClass, response.getCode());
} finally {
EntityUtils.consumeQuietly(responseEntity);
Expand All @@ -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) {
Expand Down
Loading