|
| 1 | +package com.mindee; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.mindee.http.MindeeApiV2; |
| 5 | +import com.mindee.http.MindeeHttpApiV2; |
| 6 | +import com.mindee.input.LocalInputSource; |
| 7 | +import com.mindee.input.LocalResponse; |
| 8 | +import com.mindee.input.PageOptions; |
| 9 | +import com.mindee.pdf.PdfBoxApi; |
| 10 | +import com.mindee.pdf.PdfOperation; |
| 11 | +import com.mindee.pdf.SplitQuery; |
| 12 | +import com.mindee.parsing.v2.AsyncInferenceResponse; |
| 13 | +import com.mindee.parsing.v2.AsyncJobResponse; |
| 14 | +import com.mindee.parsing.v2.PredictParameterV2; |
| 15 | +import com.mindee.parsing.v2.InferenceOptionsV2; |
| 16 | +import com.mindee.parsing.v2.AsyncPollingOptions; |
| 17 | +import java.io.IOException; |
| 18 | + |
| 19 | +/** |
| 20 | + * Entry point for the Mindee **V2** API features. |
| 21 | + */ |
| 22 | +public class MindeeClientV2 { |
| 23 | + |
| 24 | + private final PdfOperation pdfOperation; |
| 25 | + private final MindeeApiV2 mindeeApi; |
| 26 | + |
| 27 | + /** Uses an API-key read from the environment variables. */ |
| 28 | + public MindeeClientV2() { |
| 29 | + this(new PdfBoxApi(), createDefaultApiV2("")); |
| 30 | + } |
| 31 | + |
| 32 | + /** Uses the supplied API-key. */ |
| 33 | + public MindeeClientV2(String apiKey) { |
| 34 | + this(new PdfBoxApi(), createDefaultApiV2(apiKey)); |
| 35 | + } |
| 36 | + |
| 37 | + /** Directly inject an already configured {@link MindeeApiV2}. */ |
| 38 | + public MindeeClientV2(MindeeApiV2 mindeeApi) { |
| 39 | + this(new PdfBoxApi(), mindeeApi); |
| 40 | + } |
| 41 | + |
| 42 | + /** Inject both a PDF implementation and a HTTP implementation. */ |
| 43 | + public MindeeClientV2(PdfOperation pdfOperation, MindeeApiV2 mindeeApi) { |
| 44 | + this.pdfOperation = pdfOperation; |
| 45 | + this.mindeeApi = mindeeApi; |
| 46 | + } |
| 47 | + |
| 48 | + /* ------------------------------------------------------------------ */ |
| 49 | + /* Queue helpers */ |
| 50 | + /* ------------------------------------------------------------------ */ |
| 51 | + |
| 52 | + /** |
| 53 | + * Enqueue a document in the asynchronous “Generated” queue. |
| 54 | + */ |
| 55 | + public AsyncJobResponse enqueue( |
| 56 | + LocalInputSource inputSource, |
| 57 | + InferenceOptionsV2 options, |
| 58 | + PageOptions pageOptions) throws IOException { |
| 59 | + |
| 60 | + if (pageOptions != null && inputSource.isPdf()) { |
| 61 | + inputSource.setFileBytes( |
| 62 | + pdfOperation.split(new SplitQuery(inputSource.getFileBytes(), pageOptions)).getFile()); |
| 63 | + } |
| 64 | + |
| 65 | + PredictParameterV2 payload = new PredictParameterV2( |
| 66 | + inputSource, |
| 67 | + options.getModelId(), |
| 68 | + options.getAlias(), |
| 69 | + options.getWebhookIds(), |
| 70 | + options.getRag()); |
| 71 | + |
| 72 | + return mindeeApi.enqueuePost(payload); |
| 73 | + } |
| 74 | + |
| 75 | + /** Overload without page options. */ |
| 76 | + public AsyncJobResponse enqueue( |
| 77 | + LocalInputSource inputSource, |
| 78 | + InferenceOptionsV2 options) throws IOException { |
| 79 | + return enqueue(inputSource, options, null); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Retrieve results for a previously enqueued document. |
| 84 | + */ |
| 85 | + public AsyncInferenceResponse parseQueued(String jobId) { |
| 86 | + if (jobId == null || jobId.isBlank()) { |
| 87 | + throw new IllegalArgumentException("jobId must not be null or blank."); |
| 88 | + } |
| 89 | + return mindeeApi.getInferenceFromQueue(jobId); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Convenience helper: enqueue, poll, and return the final inference. |
| 94 | + */ |
| 95 | + public AsyncInferenceResponse enqueueAndParse( |
| 96 | + LocalInputSource inputSource, |
| 97 | + InferenceOptionsV2 options, |
| 98 | + PageOptions pageOptions, |
| 99 | + AsyncPollingOptions polling) throws IOException, InterruptedException { |
| 100 | + |
| 101 | + if (polling == null) { |
| 102 | + polling = new AsyncPollingOptions(); // default values |
| 103 | + } |
| 104 | + validatePollingOptions(polling); |
| 105 | + |
| 106 | + AsyncJobResponse job = enqueue(inputSource, options, pageOptions); |
| 107 | + |
| 108 | + Thread.sleep((long) (polling.getInitialDelaySec() * 1000)); |
| 109 | + |
| 110 | + int attempts = 0; |
| 111 | + int max = polling.getMaxRetries(); |
| 112 | + while (attempts < max) { |
| 113 | + Thread.sleep((long) (polling.getIntervalSec() * 1000)); |
| 114 | + AsyncInferenceResponse resp = parseQueued(job.getJob().getId()); |
| 115 | + if (resp.getInference() != null) { |
| 116 | + return resp; |
| 117 | + } |
| 118 | + attempts++; |
| 119 | + } |
| 120 | + throw new RuntimeException("Max retries exceeded (" + max + ")."); |
| 121 | + } |
| 122 | + |
| 123 | + /** Overload with defaults (no page splitting, default polling). */ |
| 124 | + public AsyncInferenceResponse enqueueAndParse( |
| 125 | + LocalInputSource inputSource, |
| 126 | + InferenceOptionsV2 options) throws IOException, InterruptedException { |
| 127 | + return enqueueAndParse(inputSource, options, null, null); |
| 128 | + } |
| 129 | + |
| 130 | + /* ------------------------------------------------------------------ */ |
| 131 | + /* Utility / helpers */ |
| 132 | + /* ------------------------------------------------------------------ */ |
| 133 | + |
| 134 | + /** |
| 135 | + * Deserialize a webhook payload (or any saved response) into |
| 136 | + * {@link AsyncInferenceResponse}. |
| 137 | + */ |
| 138 | + public AsyncInferenceResponse loadInference(LocalResponse localResponse) throws IOException { |
| 139 | + ObjectMapper mapper = new ObjectMapper().findAndConfigureModules(); |
| 140 | + AsyncInferenceResponse model = |
| 141 | + mapper.readValue(localResponse.getFile(), AsyncInferenceResponse.class); |
| 142 | + model.setRawResponse(localResponse.toString()); |
| 143 | + return model; |
| 144 | + } |
| 145 | + |
| 146 | + private static MindeeApiV2 createDefaultApiV2(String apiKey) { |
| 147 | + MindeeSettings settings = apiKey == null || apiKey.isBlank() |
| 148 | + ? new MindeeSettings() |
| 149 | + : new MindeeSettings(apiKey); |
| 150 | + return MindeeHttpApiV2.builder() |
| 151 | + .mindeeSettings(settings) |
| 152 | + .build(); |
| 153 | + } |
| 154 | + |
| 155 | + private static void validatePollingOptions(AsyncPollingOptions p) { |
| 156 | + if (p.getInitialDelaySec() < 1) { |
| 157 | + throw new IllegalArgumentException("Initial delay must be ≥ 1 s"); |
| 158 | + } |
| 159 | + if (p.getIntervalSec() < 1) { |
| 160 | + throw new IllegalArgumentException("Interval must be ≥ 1 s"); |
| 161 | + } |
| 162 | + if (p.getMaxRetries() < 2) { |
| 163 | + throw new IllegalArgumentException("Max retries must be ≥ 2"); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments