Skip to content
Merged
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
30 changes: 0 additions & 30 deletions src/main/java/com/mindee/AsyncPollingOptions.java

This file was deleted.

52 changes: 52 additions & 0 deletions src/main/java/com/mindee/clientOptions/BasePollingOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.mindee.clientOptions;

import lombok.Getter;

/**
* Options to pass for asynchronous parsing with polling.
*/
public abstract class BasePollingOptions {
/**
* Wait this many seconds before the first polling attempt.
*/
@Getter
Double initialDelaySec;

/**
* Wait this many seconds between each polling attempt.
*/
@Getter
Double intervalSec;

/**
* Maximum number of times to poll.
*/
@Getter
Integer maxRetries;

protected BasePollingOptions(
Double initialDelaySec,
Double intervalSec,
Integer maxRetries,
double defaultInitialDelaySec,
double defaultIntervalSec,
int defaultMaxRetries,
double minInitialDelaySec,
double minIntervalSec,
int minMaxRetries
) {
this.initialDelaySec = initialDelaySec == null ? defaultInitialDelaySec : initialDelaySec;
this.intervalSec = intervalSec == null ? defaultIntervalSec : intervalSec;
this.maxRetries = maxRetries == null ? defaultMaxRetries : maxRetries;

if (this.initialDelaySec < minInitialDelaySec) {
throw new IllegalArgumentException("Initial delay must be ≥ " + minInitialDelaySec);
}
if (this.intervalSec < minIntervalSec) {
throw new IllegalArgumentException("Interval must be ≥ " + minIntervalSec);
}
if (this.maxRetries < minMaxRetries) {
throw new IllegalArgumentException("Max retries must be ≥ " + minMaxRetries);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.mindee;
package com.mindee.v1;

import com.mindee.MindeeException;
import com.mindee.input.LocalInputSource;
import com.mindee.input.PageOptions;
import com.mindee.input.PageOptionsOperation;
import com.mindee.v1.MindeeClient;
import com.mindee.v1.cli.CommandLineInterfaceProducts;
import com.mindee.v1.cli.ProductProcessor;
import com.mindee.v1.clientOptions.PollingOptions;
import com.mindee.v1.clientOptions.PredictOptions;
import com.mindee.v1.http.Endpoint;
import com.mindee.v1.parsing.common.AsyncPredictResponse;
Expand Down Expand Up @@ -205,7 +206,7 @@ void generatedMethod(
new LocalInputSource(file),
PredictOptions.builder().build(),
getDefaultPageOptions(),
AsyncPollingOptions.builder().build()
PollingOptions.builder().build()
);
System.out.println(document.toString());
} else {
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/mindee/v1/MindeeClient.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.mindee.v1;

import com.mindee.AsyncPollingOptions;
import com.mindee.MindeeException;
import com.mindee.input.InputSourceUtils;
import com.mindee.input.LocalInputSource;
import com.mindee.input.PageOptions;
import com.mindee.pdf.PdfBoxApi;
import com.mindee.pdf.PdfOperation;
import com.mindee.pdf.SplitQuery;
import com.mindee.v1.clientOptions.PollingOptions;
import com.mindee.v1.clientOptions.PredictOptions;
import com.mindee.v1.clientOptions.WorkflowOptions;
import com.mindee.v1.http.Endpoint;
Expand Down Expand Up @@ -287,7 +287,7 @@ public <T extends Inference> AsyncPredictResponse<T> enqueueAndParse(
public <T extends Inference> AsyncPredictResponse<T> enqueueAndParse(
Class<T> type,
LocalInputSource localInputSource,
AsyncPollingOptions pollingOptions
PollingOptions pollingOptions
) throws IOException, InterruptedException {
return this
.enqueueAndParse(
Expand Down Expand Up @@ -319,7 +319,7 @@ public <T extends Inference> AsyncPredictResponse<T> enqueueAndParse(
LocalInputSource localInputSource,
PredictOptions predictOptions,
PageOptions pageOptions,
AsyncPollingOptions pollingOptions
PollingOptions pollingOptions
) throws IOException, InterruptedException {
return this
.enqueueAndParse(
Expand Down Expand Up @@ -349,7 +349,7 @@ public <T extends Inference> AsyncPredictResponse<T> enqueueAndParse(
Class<T> type,
LocalInputSource localInputSource,
PredictOptions predictOptions,
AsyncPollingOptions pollingOptions
PollingOptions pollingOptions
) throws IOException, InterruptedException {
return this
.enqueueAndParse(
Expand Down Expand Up @@ -415,7 +415,7 @@ public <T extends Inference> AsyncPredictResponse<T> enqueueAndParse(
* @param pollingOptions Options for async call parameters
* @throws MindeeException Throws if settings aren't set properly.
*/
private void validateAsyncParams(AsyncPollingOptions pollingOptions) {
private void validateAsyncParams(PollingOptions pollingOptions) {
Double minimumInitialDelaySec = 1.0;
Double minimumIntervalSec = 1.0;
Integer minimumRetry = 2;
Expand All @@ -440,14 +440,14 @@ private void validateAsyncParams(AsyncPollingOptions pollingOptions) {
private <T extends Inference> AsyncPredictResponse<T> enqueueAndParse(
Class<T> type,
Endpoint endpoint,
AsyncPollingOptions pollingOptions,
PollingOptions pollingOptions,
byte[] file,
String filename,
PredictOptions predictOptions,
URL urlInputSource
) throws IOException, InterruptedException {
if (pollingOptions == null) {
pollingOptions = AsyncPollingOptions.builder().build();
pollingOptions = PollingOptions.builder().build();
}
this.validateAsyncParams(pollingOptions);
final int initialDelaySec = (int) (pollingOptions.getInitialDelaySec() * 1000);
Expand Down Expand Up @@ -832,7 +832,7 @@ public <T extends GeneratedV1> AsyncPredictResponse<T> enqueueAndParse(
Class<T> type,
Endpoint endpoint,
LocalInputSource localInputSource,
AsyncPollingOptions pollingOptions
PollingOptions pollingOptions
) throws IOException, InterruptedException {
return this
.enqueueAndParse(
Expand Down Expand Up @@ -866,7 +866,7 @@ public <T extends GeneratedV1> AsyncPredictResponse<T> enqueueAndParse(
LocalInputSource localInputSource,
PredictOptions predictOptions,
PageOptions pageOptions,
AsyncPollingOptions pollingOptions
PollingOptions pollingOptions
) throws IOException, InterruptedException {
return this
.enqueueAndParse(
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/mindee/v1/clientOptions/PollingOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mindee.v1.clientOptions;

import com.mindee.clientOptions.BasePollingOptions;
import lombok.Builder;

public class PollingOptions extends BasePollingOptions {

@Builder
public PollingOptions(Double initialDelaySec, Double intervalSec, Integer maxRetries) {
super(initialDelaySec, intervalSec, maxRetries, 2.0, 1.5, 80, 1.0, 1.0, 2);
}
}
67 changes: 57 additions & 10 deletions src/main/java/com/mindee/v2/MindeeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mindee.input.LocalInputSource;
import com.mindee.input.URLInputSource;
import com.mindee.v2.clientOptions.BaseParameters;
import com.mindee.v2.clientOptions.PollingOptions;
import com.mindee.v2.http.MindeeApiV2;
import com.mindee.v2.http.MindeeHttpApiV2;
import com.mindee.v2.http.MindeeHttpExceptionV2;
Expand Down Expand Up @@ -83,9 +84,10 @@ public <TResponse extends CommonResponse> TResponse getResult(

/**
* Send a local file to an async queue, poll, and parse when complete.
* Use default polling options.
*
* @param inputSource The local input source to send.
* @param params The parameters to send along with the file.
* @param params The product parameters to send along with the file.
* @return an instance of {@link ExtractionResponse}.
* @throws IOException Throws if the file can't be accessed.
* @throws InterruptedException Throws if the thread is interrupted.
Expand All @@ -95,16 +97,41 @@ public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
LocalInputSource inputSource,
BaseParameters params
) throws IOException, InterruptedException {
params.validatePollingOptions();
return enqueueAndGetResult(
responseClass,
inputSource,
params,
PollingOptions.builder().build()
);
}

/**
* Send a local file to an async queue, poll, and parse when complete.
* Specify polling options.
*
* @param inputSource The local input source to send.
* @param params The product parameters to send along with the file.
* @param pollingOptions The polling options to use.
* @return an instance of {@link ExtractionResponse}.
* @throws IOException Throws if the file can't be accessed.
* @throws InterruptedException Throws if the thread is interrupted.
*/
public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
Class<TResponse> responseClass,
LocalInputSource inputSource,
BaseParameters params,
PollingOptions pollingOptions
) throws IOException, InterruptedException {
JobResponse job = enqueue(inputSource, params);
return pollAndFetch(responseClass, job, params);
return pollAndFetch(responseClass, job, pollingOptions);
}

/**
* Send a remote file to an async queue, poll, and parse when complete.
* Use default polling options.
*
* @param inputSource The URL input source to send.
* @param params The parameters to send along with the file.
* @param params The product parameters to send along with the file.
* @return an instance of {@link ExtractionResponse}.
* @throws IOException Throws if the file can't be accessed.
* @throws InterruptedException Throws if the thread is interrupted.
Expand All @@ -114,9 +141,29 @@ public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
URLInputSource inputSource,
BaseParameters params
) throws IOException, InterruptedException {
params.validatePollingOptions();
JobResponse job = enqueue(inputSource, params);
return pollAndFetch(responseClass, job, params);
return pollAndFetch(responseClass, job, PollingOptions.builder().build());
}

/**
* Send a remote file to an async queue, poll, and parse when complete.
* Specify polling options.
*
* @param inputSource The URL input source to send.
* @param params The product parameters to send along with the file.
* @param pollingOptions The polling options to use.
* @return an instance of {@link ExtractionResponse}.
* @throws IOException Throws if the file can't be accessed.
* @throws InterruptedException Throws if the thread is interrupted.
*/
public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
Class<TResponse> responseClass,
URLInputSource inputSource,
BaseParameters params,
PollingOptions pollingOptions
) throws IOException, InterruptedException {
JobResponse job = enqueue(inputSource, params);
return pollAndFetch(responseClass, job, pollingOptions);
}

/**
Expand All @@ -129,16 +176,16 @@ public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
private <TResponse extends CommonResponse> TResponse pollAndFetch(
Class<TResponse> responseClass,
JobResponse initialJob,
BaseParameters options
PollingOptions pollingOptions
) throws InterruptedException {
Thread.sleep((long) (options.getPollingOptions().getInitialDelaySec() * 1000));
Thread.sleep((long) (pollingOptions.getInitialDelaySec() * 1000));

JobResponse resp = initialJob;
int attempts = 0;
int max = options.getPollingOptions().getMaxRetries();
int max = pollingOptions.getMaxRetries();

while (attempts < max) {
Thread.sleep((long) (options.getPollingOptions().getIntervalSec() * 1000));
Thread.sleep((long) (pollingOptions.getIntervalSec() * 1000));
resp = getJob(initialJob.getJob().getId());

if (resp.getJob().getStatus().equals("Failed")) {
Expand Down
24 changes: 0 additions & 24 deletions src/main/java/com/mindee/v2/clientOptions/BaseParameters.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mindee.v2.clientOptions;

import com.mindee.AsyncPollingOptions;
import java.util.Objects;
import lombok.Data;
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
Expand All @@ -20,10 +19,6 @@ public abstract class BaseParameters {
* If empty, no webhooks will be used.
*/
protected final String[] webhookIds;
/**
* Polling options. Set only if having timeout issues.
*/
protected final AsyncPollingOptions pollingOptions;

public MultipartEntityBuilder buildHttpBody(MultipartEntityBuilder builder) {
builder.addTextBody("model_id", this.getModelId());
Expand All @@ -36,23 +31,10 @@ public MultipartEntityBuilder buildHttpBody(MultipartEntityBuilder builder) {
return builder;
}

public void validatePollingOptions() {
if (pollingOptions.getInitialDelaySec() < 1) {
throw new IllegalArgumentException("Initial delay must be ≥ 1 s");
}
if (pollingOptions.getIntervalSec() < 1) {
throw new IllegalArgumentException("Interval must be ≥ 1 s");
}
if (pollingOptions.getMaxRetries() < 2) {
throw new IllegalArgumentException("Max retries must be ≥ 2");
}
}

protected static abstract class BaseBuilder<T extends BaseBuilder<T>> {
protected final String modelId;
protected String alias;
protected String[] webhookIds = new String[] {};
protected AsyncPollingOptions pollingOptions = AsyncPollingOptions.builder().build();

@SuppressWarnings("unchecked")
protected T self() {
Expand All @@ -74,12 +56,6 @@ public T webhookIds(String[] webhookIds) {
this.webhookIds = webhookIds;
return self();
}

/** Set polling options. */
public T pollingOptions(AsyncPollingOptions pollingOptions) {
this.pollingOptions = pollingOptions;
return self();
}
}

}
11 changes: 11 additions & 0 deletions src/main/java/com/mindee/v2/clientOptions/PollingOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mindee.v2.clientOptions;

import com.mindee.clientOptions.BasePollingOptions;
import lombok.Builder;

public class PollingOptions extends BasePollingOptions {
@Builder
public PollingOptions(Double initialDelaySec, Double intervalSec, Integer maxRetries) {
super(initialDelaySec, intervalSec, maxRetries, 3.0, 1.5, 100, 1.0, 1.0, 2);
}
}
Loading
Loading