From 3cad2714c28fab9f95bfd621156e4469775ab5e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ianar=C3=A9=20S=C3=A9vi?= Date: Wed, 8 Apr 2026 18:40:36 +0200 Subject: [PATCH] :recycle: :boom: polling options should be version-specific --- .../java/com/mindee/AsyncPollingOptions.java | 30 --------- .../clientOptions/BasePollingOptions.java | 52 ++++++++++++++ .../mindee/{ => v1}/CommandLineInterface.java | 7 +- src/main/java/com/mindee/v1/MindeeClient.java | 18 ++--- .../v1/clientOptions/PollingOptions.java | 12 ++++ src/main/java/com/mindee/v2/MindeeClient.java | 67 ++++++++++++++++--- .../v2/clientOptions/BaseParameters.java | 24 ------- .../v2/clientOptions/PollingOptions.java | 11 +++ .../params/ClassificationParameters.java | 12 +--- .../product/crop/params/CropParameters.java | 12 +--- .../params/ExtractionParameters.java | 5 +- .../v2/product/ocr/params/OcrParameters.java | 12 +--- .../product/split/params/SplitParameters.java | 12 +--- .../java/com/mindee/v2/MindeeClientIT.java | 15 +++-- .../v2/clientOptions/PollingOptionsTest.java | 40 +++++++++++ .../java/com/mindee/v2/product/CropIT.java | 16 +++-- .../java/com/mindee/v2/product/SplitIT.java | 14 ++-- 17 files changed, 227 insertions(+), 132 deletions(-) delete mode 100644 src/main/java/com/mindee/AsyncPollingOptions.java create mode 100644 src/main/java/com/mindee/clientOptions/BasePollingOptions.java rename src/main/java/com/mindee/{ => v1}/CommandLineInterface.java (98%) create mode 100644 src/main/java/com/mindee/v1/clientOptions/PollingOptions.java create mode 100644 src/main/java/com/mindee/v2/clientOptions/PollingOptions.java create mode 100644 src/test/java/com/mindee/v2/clientOptions/PollingOptionsTest.java diff --git a/src/main/java/com/mindee/AsyncPollingOptions.java b/src/main/java/com/mindee/AsyncPollingOptions.java deleted file mode 100644 index 004c6f72a..000000000 --- a/src/main/java/com/mindee/AsyncPollingOptions.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.mindee; - -import lombok.Builder; -import lombok.Value; - -/** - * Options to pass for asynchronous parsing with polling. - */ -@Value -public class AsyncPollingOptions { - /** - * Wait this many seconds before the first polling attempt. - */ - Double initialDelaySec; - /** - * Wait this many seconds between each polling attempt. - */ - Double intervalSec; - /** - * Maximum number of times to poll. - */ - Integer maxRetries; - - @Builder - private AsyncPollingOptions(Double initialDelaySec, Double intervalSec, Integer maxRetries) { - this.initialDelaySec = initialDelaySec == null ? 2.0 : initialDelaySec; - this.intervalSec = intervalSec == null ? 1.5 : intervalSec; - this.maxRetries = maxRetries == null ? 80 : maxRetries; - } -} diff --git a/src/main/java/com/mindee/clientOptions/BasePollingOptions.java b/src/main/java/com/mindee/clientOptions/BasePollingOptions.java new file mode 100644 index 000000000..1694f3c4b --- /dev/null +++ b/src/main/java/com/mindee/clientOptions/BasePollingOptions.java @@ -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); + } + } +} diff --git a/src/main/java/com/mindee/CommandLineInterface.java b/src/main/java/com/mindee/v1/CommandLineInterface.java similarity index 98% rename from src/main/java/com/mindee/CommandLineInterface.java rename to src/main/java/com/mindee/v1/CommandLineInterface.java index d4957ada6..09c6bfa45 100644 --- a/src/main/java/com/mindee/CommandLineInterface.java +++ b/src/main/java/com/mindee/v1/CommandLineInterface.java @@ -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; @@ -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 { diff --git a/src/main/java/com/mindee/v1/MindeeClient.java b/src/main/java/com/mindee/v1/MindeeClient.java index f9c3ea5b3..ce949cf69 100644 --- a/src/main/java/com/mindee/v1/MindeeClient.java +++ b/src/main/java/com/mindee/v1/MindeeClient.java @@ -1,6 +1,5 @@ package com.mindee.v1; -import com.mindee.AsyncPollingOptions; import com.mindee.MindeeException; import com.mindee.input.InputSourceUtils; import com.mindee.input.LocalInputSource; @@ -8,6 +7,7 @@ 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; @@ -287,7 +287,7 @@ public AsyncPredictResponse enqueueAndParse( public AsyncPredictResponse enqueueAndParse( Class type, LocalInputSource localInputSource, - AsyncPollingOptions pollingOptions + PollingOptions pollingOptions ) throws IOException, InterruptedException { return this .enqueueAndParse( @@ -319,7 +319,7 @@ public AsyncPredictResponse enqueueAndParse( LocalInputSource localInputSource, PredictOptions predictOptions, PageOptions pageOptions, - AsyncPollingOptions pollingOptions + PollingOptions pollingOptions ) throws IOException, InterruptedException { return this .enqueueAndParse( @@ -349,7 +349,7 @@ public AsyncPredictResponse enqueueAndParse( Class type, LocalInputSource localInputSource, PredictOptions predictOptions, - AsyncPollingOptions pollingOptions + PollingOptions pollingOptions ) throws IOException, InterruptedException { return this .enqueueAndParse( @@ -415,7 +415,7 @@ public AsyncPredictResponse 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; @@ -440,14 +440,14 @@ private void validateAsyncParams(AsyncPollingOptions pollingOptions) { private AsyncPredictResponse enqueueAndParse( Class 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); @@ -832,7 +832,7 @@ public AsyncPredictResponse enqueueAndParse( Class type, Endpoint endpoint, LocalInputSource localInputSource, - AsyncPollingOptions pollingOptions + PollingOptions pollingOptions ) throws IOException, InterruptedException { return this .enqueueAndParse( @@ -866,7 +866,7 @@ public AsyncPredictResponse enqueueAndParse( LocalInputSource localInputSource, PredictOptions predictOptions, PageOptions pageOptions, - AsyncPollingOptions pollingOptions + PollingOptions pollingOptions ) throws IOException, InterruptedException { return this .enqueueAndParse( diff --git a/src/main/java/com/mindee/v1/clientOptions/PollingOptions.java b/src/main/java/com/mindee/v1/clientOptions/PollingOptions.java new file mode 100644 index 000000000..b1b8d4fc5 --- /dev/null +++ b/src/main/java/com/mindee/v1/clientOptions/PollingOptions.java @@ -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); + } +} diff --git a/src/main/java/com/mindee/v2/MindeeClient.java b/src/main/java/com/mindee/v2/MindeeClient.java index be9a1a94f..f9e5ea184 100644 --- a/src/main/java/com/mindee/v2/MindeeClient.java +++ b/src/main/java/com/mindee/v2/MindeeClient.java @@ -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; @@ -83,9 +84,10 @@ public 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. @@ -95,16 +97,41 @@ public 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 enqueueAndGetResult( + Class 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. @@ -114,9 +141,29 @@ public 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 enqueueAndGetResult( + Class responseClass, + URLInputSource inputSource, + BaseParameters params, + PollingOptions pollingOptions + ) throws IOException, InterruptedException { + JobResponse job = enqueue(inputSource, params); + return pollAndFetch(responseClass, job, pollingOptions); } /** @@ -129,16 +176,16 @@ public TResponse enqueueAndGetResult( private TResponse pollAndFetch( Class 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")) { diff --git a/src/main/java/com/mindee/v2/clientOptions/BaseParameters.java b/src/main/java/com/mindee/v2/clientOptions/BaseParameters.java index 7d38c309f..e5f2421bb 100644 --- a/src/main/java/com/mindee/v2/clientOptions/BaseParameters.java +++ b/src/main/java/com/mindee/v2/clientOptions/BaseParameters.java @@ -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; @@ -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()); @@ -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> { protected final String modelId; protected String alias; protected String[] webhookIds = new String[] {}; - protected AsyncPollingOptions pollingOptions = AsyncPollingOptions.builder().build(); @SuppressWarnings("unchecked") protected T self() { @@ -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(); - } } } diff --git a/src/main/java/com/mindee/v2/clientOptions/PollingOptions.java b/src/main/java/com/mindee/v2/clientOptions/PollingOptions.java new file mode 100644 index 000000000..95f0a23fd --- /dev/null +++ b/src/main/java/com/mindee/v2/clientOptions/PollingOptions.java @@ -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); + } +} diff --git a/src/main/java/com/mindee/v2/product/classification/params/ClassificationParameters.java b/src/main/java/com/mindee/v2/product/classification/params/ClassificationParameters.java index 7fa37267e..d3d29144f 100644 --- a/src/main/java/com/mindee/v2/product/classification/params/ClassificationParameters.java +++ b/src/main/java/com/mindee/v2/product/classification/params/ClassificationParameters.java @@ -1,18 +1,12 @@ package com.mindee.v2.product.classification.params; -import com.mindee.AsyncPollingOptions; import com.mindee.v2.clientOptions.BaseParameters; import com.mindee.v2.http.ProductInfo; @ProductInfo(slug = "classification") public class ClassificationParameters extends BaseParameters { - public ClassificationParameters( - String modelId, - String alias, - String[] webhookIds, - AsyncPollingOptions pollingOptions - ) { - super(modelId, alias, webhookIds, pollingOptions); + public ClassificationParameters(String modelId, String alias, String[] webhookIds) { + super(modelId, alias, webhookIds); } /** @@ -33,7 +27,7 @@ public static final class Builder extends BaseParameters.BaseBuilder { /** Build an immutable {@link ClassificationParameters} instance. */ public ClassificationParameters build() { - return new ClassificationParameters(modelId, alias, webhookIds, pollingOptions); + return new ClassificationParameters(modelId, alias, webhookIds); } } } diff --git a/src/main/java/com/mindee/v2/product/crop/params/CropParameters.java b/src/main/java/com/mindee/v2/product/crop/params/CropParameters.java index 0daa2c6fe..823606d55 100644 --- a/src/main/java/com/mindee/v2/product/crop/params/CropParameters.java +++ b/src/main/java/com/mindee/v2/product/crop/params/CropParameters.java @@ -1,19 +1,13 @@ package com.mindee.v2.product.crop.params; -import com.mindee.AsyncPollingOptions; import com.mindee.v2.clientOptions.BaseParameters; import com.mindee.v2.http.ProductInfo; @ProductInfo(slug = "crop") public class CropParameters extends BaseParameters { - public CropParameters( - String modelId, - String alias, - String[] webhookIds, - AsyncPollingOptions pollingOptions - ) { - super(modelId, alias, webhookIds, pollingOptions); + public CropParameters(String modelId, String alias, String[] webhookIds) { + super(modelId, alias, webhookIds); } /** @@ -34,7 +28,7 @@ public static final class Builder extends BaseParameters.BaseBuilder { /** Build an immutable {@link CropParameters} instance. */ public CropParameters build() { - return new CropParameters(modelId, alias, webhookIds, pollingOptions); + return new CropParameters(modelId, alias, webhookIds); } } } diff --git a/src/main/java/com/mindee/v2/product/extraction/params/ExtractionParameters.java b/src/main/java/com/mindee/v2/product/extraction/params/ExtractionParameters.java index 077d8f73c..06652899d 100644 --- a/src/main/java/com/mindee/v2/product/extraction/params/ExtractionParameters.java +++ b/src/main/java/com/mindee/v2/product/extraction/params/ExtractionParameters.java @@ -1,6 +1,5 @@ package com.mindee.v2.product.extraction.params; -import com.mindee.AsyncPollingOptions; import com.mindee.v2.clientOptions.BaseParameters; import com.mindee.v2.http.ProductInfo; import lombok.EqualsAndHashCode; @@ -45,7 +44,6 @@ private ExtractionParameters( String modelId, String alias, String[] webhookIds, - AsyncPollingOptions pollingOptions, Boolean rag, Boolean rawText, Boolean polygon, @@ -53,7 +51,7 @@ private ExtractionParameters( String textContext, String dataSchema ) { - super(modelId, alias, webhookIds, pollingOptions); + super(modelId, alias, webhookIds); this.rag = rag; this.rawText = rawText; this.polygon = polygon; @@ -155,7 +153,6 @@ public ExtractionParameters build() { modelId, alias, webhookIds, - pollingOptions, rag, rawText, polygon, diff --git a/src/main/java/com/mindee/v2/product/ocr/params/OcrParameters.java b/src/main/java/com/mindee/v2/product/ocr/params/OcrParameters.java index 0ab4a11a5..74359638b 100644 --- a/src/main/java/com/mindee/v2/product/ocr/params/OcrParameters.java +++ b/src/main/java/com/mindee/v2/product/ocr/params/OcrParameters.java @@ -1,19 +1,13 @@ package com.mindee.v2.product.ocr.params; -import com.mindee.AsyncPollingOptions; import com.mindee.v2.clientOptions.BaseParameters; import com.mindee.v2.http.ProductInfo; @ProductInfo(slug = "ocr") public class OcrParameters extends BaseParameters { - public OcrParameters( - String modelId, - String alias, - String[] webhookIds, - AsyncPollingOptions pollingOptions - ) { - super(modelId, alias, webhookIds, pollingOptions); + public OcrParameters(String modelId, String alias, String[] webhookIds) { + super(modelId, alias, webhookIds); } /** @@ -34,7 +28,7 @@ public static final class Builder extends BaseParameters.BaseBuilder { /** Build an immutable {@link OcrParameters} instance. */ public OcrParameters build() { - return new OcrParameters(modelId, alias, webhookIds, pollingOptions); + return new OcrParameters(modelId, alias, webhookIds); } } } diff --git a/src/main/java/com/mindee/v2/product/split/params/SplitParameters.java b/src/main/java/com/mindee/v2/product/split/params/SplitParameters.java index 4624b3177..a1aab32b3 100644 --- a/src/main/java/com/mindee/v2/product/split/params/SplitParameters.java +++ b/src/main/java/com/mindee/v2/product/split/params/SplitParameters.java @@ -1,19 +1,13 @@ package com.mindee.v2.product.split.params; -import com.mindee.AsyncPollingOptions; import com.mindee.v2.clientOptions.BaseParameters; import com.mindee.v2.http.ProductInfo; @ProductInfo(slug = "split") public class SplitParameters extends BaseParameters { - public SplitParameters( - String modelId, - String alias, - String[] webhookIds, - AsyncPollingOptions pollingOptions - ) { - super(modelId, alias, webhookIds, pollingOptions); + public SplitParameters(String modelId, String alias, String[] webhookIds) { + super(modelId, alias, webhookIds); } /** @@ -34,7 +28,7 @@ public static final class Builder extends BaseParameters.BaseBuilder { /** Build an immutable {@link SplitParameters} instance. */ public SplitParameters build() { - return new SplitParameters(modelId, alias, webhookIds, pollingOptions); + return new SplitParameters(modelId, alias, webhookIds); } } } diff --git a/src/test/java/com/mindee/v2/MindeeClientIT.java b/src/test/java/com/mindee/v2/MindeeClientIT.java index d8ca1d942..9e6998f41 100644 --- a/src/test/java/com/mindee/v2/MindeeClientIT.java +++ b/src/test/java/com/mindee/v2/MindeeClientIT.java @@ -4,9 +4,9 @@ import static com.mindee.TestingUtilities.getV2ResourcePath; import static org.junit.jupiter.api.Assertions.*; -import com.mindee.AsyncPollingOptions; import com.mindee.input.LocalInputSource; import com.mindee.input.URLInputSource; +import com.mindee.v2.clientOptions.PollingOptions; import com.mindee.v2.http.MindeeHttpExceptionV2; import com.mindee.v2.product.extraction.ExtractionInference; import com.mindee.v2.product.extraction.ExtractionResponse; @@ -42,12 +42,17 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep .confidence(null) .alias("java-integration-test_multipage") .textContext(null) - .pollingOptions( - AsyncPollingOptions.builder().initialDelaySec(3.0).intervalSec(1.5).maxRetries(80).build() - ) .build(); - var response = mindeeClient.enqueueAndGetResult(ExtractionResponse.class, source, params); + var pollingOptions = PollingOptions + .builder() + .initialDelaySec(3.0) + .intervalSec(1.5) + .maxRetries(80) + .build(); + + var response = mindeeClient + .enqueueAndGetResult(ExtractionResponse.class, source, params, pollingOptions); assertNotNull(response); var inference = response.getInference(); diff --git a/src/test/java/com/mindee/v2/clientOptions/PollingOptionsTest.java b/src/test/java/com/mindee/v2/clientOptions/PollingOptionsTest.java new file mode 100644 index 000000000..e0d646c46 --- /dev/null +++ b/src/test/java/com/mindee/v2/clientOptions/PollingOptionsTest.java @@ -0,0 +1,40 @@ +package com.mindee.v2.clientOptions; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class PollingOptionsTest { + + @Test + void shouldSetDefaultValues() { + PollingOptions pollingOptions = PollingOptions.builder().build(); + + Assertions.assertEquals(3.0, pollingOptions.getInitialDelaySec()); + Assertions.assertEquals(1.5, pollingOptions.getIntervalSec()); + Assertions.assertEquals(100, pollingOptions.getMaxRetries()); + } + + @Test + void shouldSetCustomValues() { + PollingOptions pollingOptions = PollingOptions + .builder() + .initialDelaySec(4.0) + .intervalSec(2.5) + .maxRetries(50) + .build(); + + Assertions.assertEquals(4.0, pollingOptions.getInitialDelaySec()); + Assertions.assertEquals(2.5, pollingOptions.getIntervalSec()); + Assertions.assertEquals(50, pollingOptions.getMaxRetries()); + } + + @Test + void shouldThrowWhenInitialDelayIsTooLow() { + IllegalArgumentException exception = Assertions + .assertThrows( + IllegalArgumentException.class, + () -> PollingOptions.builder().initialDelaySec(0.1).build() + ); + Assertions.assertEquals("Initial delay must be ≥ 1.0", exception.getMessage()); + } +} diff --git a/src/test/java/com/mindee/v2/product/CropIT.java b/src/test/java/com/mindee/v2/product/CropIT.java index 336dabe22..c99cd124d 100644 --- a/src/test/java/com/mindee/v2/product/CropIT.java +++ b/src/test/java/com/mindee/v2/product/CropIT.java @@ -3,9 +3,9 @@ import static com.mindee.TestingUtilities.getResourcePath; import static org.junit.jupiter.api.Assertions.*; -import com.mindee.AsyncPollingOptions; import com.mindee.input.LocalInputSource; import com.mindee.v2.MindeeClient; +import com.mindee.v2.clientOptions.PollingOptions; import com.mindee.v2.product.crop.CropResponse; import com.mindee.v2.product.crop.params.CropParameters; import java.io.IOException; @@ -34,15 +34,19 @@ void setUp() { @DisplayName("Empty, multi-page PDF – enqueue & parse must succeed") void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedException { var source = new LocalInputSource(getResourcePath("file_types/pdf/multipage_cut-2.pdf")); - CropParameters params = CropParameters + var params = CropParameters .builder(modelId) .alias("java_integration-test_crop_multipage") - .pollingOptions( - AsyncPollingOptions.builder().initialDelaySec(3.0).intervalSec(1.5).maxRetries(80).build() - ) + .build(); + var pollingOptions = PollingOptions + .builder() + .initialDelaySec(3.0) + .intervalSec(1.5) + .maxRetries(80) .build(); - CropResponse response = mindeeClient.enqueueAndGetResult(CropResponse.class, source, params); + CropResponse response = mindeeClient + .enqueueAndGetResult(CropResponse.class, source, params, pollingOptions); assertNotNull(response); var inference = response.getInference(); diff --git a/src/test/java/com/mindee/v2/product/SplitIT.java b/src/test/java/com/mindee/v2/product/SplitIT.java index 6419c0bac..967b71c10 100644 --- a/src/test/java/com/mindee/v2/product/SplitIT.java +++ b/src/test/java/com/mindee/v2/product/SplitIT.java @@ -5,9 +5,9 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import com.mindee.AsyncPollingOptions; import com.mindee.input.LocalInputSource; import com.mindee.v2.MindeeClient; +import com.mindee.v2.clientOptions.PollingOptions; import com.mindee.v2.product.split.SplitResponse; import com.mindee.v2.product.split.params.SplitParameters; import java.io.IOException; @@ -39,12 +39,16 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep var params = SplitParameters .builder(modelId) .alias("java_integration-test_split_multipage") - .pollingOptions( - AsyncPollingOptions.builder().initialDelaySec(3.0).intervalSec(1.5).maxRetries(80).build() - ) + .build(); + var pollingOptions = PollingOptions + .builder() + .initialDelaySec(3.0) + .intervalSec(1.5) + .maxRetries(80) .build(); - var response = mindeeClient.enqueueAndGetResult(SplitResponse.class, source, params); + var response = mindeeClient + .enqueueAndGetResult(SplitResponse.class, source, params, pollingOptions); assertNotNull(response); var inference = response.getInference();