|
| 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.http.MindeeHttpExceptionV2; |
| 7 | +import com.mindee.input.LocalInputSource; |
| 8 | +import com.mindee.input.LocalResponse; |
| 9 | +import com.mindee.parsing.v2.ErrorResponse; |
| 10 | +import com.mindee.parsing.v2.InferenceResponse; |
| 11 | +import com.mindee.parsing.v2.JobResponse; |
| 12 | +import java.io.IOException; |
| 13 | + |
| 14 | +/** |
| 15 | + * Entry point for the Mindee **V2** API features. |
| 16 | + */ |
| 17 | +public class MindeeClientV2 { |
| 18 | + private final MindeeApiV2 mindeeApi; |
| 19 | + |
| 20 | + /** Uses an API-key read from the environment variables. */ |
| 21 | + public MindeeClientV2() { |
| 22 | + this(createDefaultApiV2("")); |
| 23 | + } |
| 24 | + |
| 25 | + /** Uses the supplied API-key. */ |
| 26 | + public MindeeClientV2(String apiKey) { |
| 27 | + this(createDefaultApiV2(apiKey)); |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + /** Inject both a PDF implementation and a HTTP implementation. */ |
| 32 | + public MindeeClientV2(MindeeApiV2 mindeeApi) { |
| 33 | + this.mindeeApi = mindeeApi; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Enqueue a document in the asynchronous queue. |
| 38 | + */ |
| 39 | + public JobResponse enqueueInference( |
| 40 | + LocalInputSource inputSource, |
| 41 | + InferenceParameters params) throws IOException { |
| 42 | + return mindeeApi.reqPostInferenceEnqueue(inputSource, params); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Get the status of an inference that was previously enqueued. |
| 47 | + * Can be used for polling. |
| 48 | + */ |
| 49 | + public JobResponse getJob(String jobId) { |
| 50 | + if (jobId == null || jobId.trim().isEmpty()) { |
| 51 | + throw new IllegalArgumentException("jobId must not be null or blank."); |
| 52 | + } |
| 53 | + return mindeeApi.reqGetJob(jobId); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Get the result of an inference that was previously enqueued. |
| 58 | + * The inference will only be available after it has finished processing. |
| 59 | + */ |
| 60 | + public InferenceResponse getInference(String inferenceId) { |
| 61 | + if (inferenceId == null || inferenceId.trim().isEmpty()) { |
| 62 | + throw new IllegalArgumentException("inferenceId must not be null or blank."); |
| 63 | + } |
| 64 | + |
| 65 | + return mindeeApi.reqGetInference(inferenceId); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Send a local file to an async queue, poll, and parse when complete. |
| 70 | + * @param inputSource The input source to send. |
| 71 | + * @param options The options to send along with the file. |
| 72 | + * @return an instance of {@link InferenceResponse}. |
| 73 | + * @throws IOException Throws if the file can't be accessed. |
| 74 | + * @throws InterruptedException Throws if the thread is interrupted. |
| 75 | + */ |
| 76 | + public InferenceResponse enqueueAndGetInference( |
| 77 | + LocalInputSource inputSource, |
| 78 | + InferenceParameters options) throws IOException, InterruptedException { |
| 79 | + |
| 80 | + validatePollingOptions(options.getPollingOptions()); |
| 81 | + |
| 82 | + JobResponse job = enqueueInference(inputSource, options); |
| 83 | + |
| 84 | + Thread.sleep((long) (options.getPollingOptions().getInitialDelaySec() * 1000)); |
| 85 | + JobResponse resp = job; |
| 86 | + int attempts = 0; |
| 87 | + int max = options.getPollingOptions().getMaxRetries(); |
| 88 | + while (attempts < max) { |
| 89 | + Thread.sleep((long) (options.getPollingOptions().getIntervalSec() * 1000)); |
| 90 | + resp = getJob(job.getJob().getId()); |
| 91 | + if (resp.getJob().getStatus().equals("Failed")) { |
| 92 | + break; |
| 93 | + } |
| 94 | + else if (resp.getJob().getStatus().equals("Processed")) { |
| 95 | + return getInference(resp.getJob().getId()); |
| 96 | + } |
| 97 | + attempts++; |
| 98 | + } |
| 99 | + ErrorResponse error = resp.getJob().getError(); |
| 100 | + if (error != null) { |
| 101 | + throw new MindeeHttpExceptionV2(error.getStatus(), error.getDetail()); |
| 102 | + } |
| 103 | + throw new RuntimeException("Max retries exceeded (" + max + ")."); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Deserialize a webhook payload (or any saved response) into an |
| 108 | + * {@link InferenceResponse}. |
| 109 | + */ |
| 110 | + public InferenceResponse loadInference(LocalResponse localResponse) throws IOException { |
| 111 | + ObjectMapper mapper = new ObjectMapper().findAndRegisterModules(); |
| 112 | + InferenceResponse model = |
| 113 | + mapper.readValue(localResponse.getFile(), InferenceResponse.class); |
| 114 | + model.setRawResponse(localResponse.toString()); |
| 115 | + return model; |
| 116 | + } |
| 117 | + |
| 118 | + private static MindeeApiV2 createDefaultApiV2(String apiKey) { |
| 119 | + MindeeSettingsV2 settings = apiKey == null || apiKey.trim().isEmpty() |
| 120 | + ? new MindeeSettingsV2() |
| 121 | + : new MindeeSettingsV2(apiKey); |
| 122 | + return MindeeHttpApiV2.builder() |
| 123 | + .mindeeSettings(settings) |
| 124 | + .build(); |
| 125 | + } |
| 126 | + |
| 127 | + private static void validatePollingOptions(AsyncPollingOptions p) { |
| 128 | + if (p.getInitialDelaySec() < 1) { |
| 129 | + throw new IllegalArgumentException("Initial delay must be ≥ 1 s"); |
| 130 | + } |
| 131 | + if (p.getIntervalSec() < 1) { |
| 132 | + throw new IllegalArgumentException("Interval must be ≥ 1 s"); |
| 133 | + } |
| 134 | + if (p.getMaxRetries() < 2) { |
| 135 | + throw new IllegalArgumentException("Max retries must be ≥ 2"); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments