|
| 1 | +package com.mindee.v2.cli; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 5 | +import com.mindee.input.LocalInputSource; |
| 6 | +import com.mindee.v2.MindeeClient; |
| 7 | +import com.mindee.v2.parsing.CommonResponse; |
| 8 | +import java.io.File; |
| 9 | +import java.util.concurrent.Callable; |
| 10 | +import picocli.CommandLine.Option; |
| 11 | +import picocli.CommandLine.Parameters; |
| 12 | + |
| 13 | +/** |
| 14 | + * Abstract base class for V2 inference CLI commands. |
| 15 | + * Handles common options (path, model-id, api-key, alias, output) and output formatting. |
| 16 | + */ |
| 17 | +public abstract class BaseInferenceCommand implements Callable<Integer> { |
| 18 | + |
| 19 | + @Parameters(index = "0", paramLabel = "<path>", description = "The path of the file to parse") |
| 20 | + protected File file; |
| 21 | + |
| 22 | + @Option(names = { "-m", "--model-id" }, description = "ID of the model to use", required = true) |
| 23 | + protected String modelId; |
| 24 | + |
| 25 | + @Option(names = { "-k", "--api-key" }, description = "Mindee V2 API key.") |
| 26 | + protected String apiKey; |
| 27 | + |
| 28 | + @Option(names = { "-a", "--alias" }, description = "Alias for the file") |
| 29 | + protected String alias; |
| 30 | + |
| 31 | + /** Output format for the command. */ |
| 32 | + public enum OutputType { |
| 33 | + Summary, |
| 34 | + Full, |
| 35 | + Raw |
| 36 | + } |
| 37 | + |
| 38 | + @Option( |
| 39 | + names = { "-o", "--output" }, |
| 40 | + description = "Specify how to output the data.\n" |
| 41 | + + "- summary: a basic summary (default)\n" |
| 42 | + + "- full: detail extraction results, including options\n" |
| 43 | + + "- raw: full JSON object", |
| 44 | + defaultValue = "Summary" |
| 45 | + ) |
| 46 | + protected OutputType output; |
| 47 | + |
| 48 | + /** |
| 49 | + * Executes the inference request and returns the product response. |
| 50 | + * |
| 51 | + * @param client the V2 Mindee client |
| 52 | + * @param inputSource the prepared local input source |
| 53 | + * @return the product response |
| 54 | + * @throws Exception on IO or API error |
| 55 | + */ |
| 56 | + protected abstract CommonResponse executeRequest( |
| 57 | + MindeeClient client, |
| 58 | + LocalInputSource inputSource |
| 59 | + ) throws Exception; |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns the summary (result-only) string for the given response. |
| 63 | + * Override in each product command. |
| 64 | + * |
| 65 | + * @param response the product response |
| 66 | + * @return the summary string |
| 67 | + */ |
| 68 | + protected abstract String getSummary(CommonResponse response); |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns the full (inference + options + result) string for the given response. |
| 72 | + * Defaults to the same as {@link #getSummary}; override for richer output. |
| 73 | + * |
| 74 | + * @param response the product response |
| 75 | + * @return the full output string |
| 76 | + */ |
| 77 | + protected String getFullOutput(CommonResponse response) { |
| 78 | + return getSummary(response); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Integer call() throws Exception { |
| 83 | + var client = new MindeeClient(apiKey != null ? apiKey : ""); |
| 84 | + var inputSource = new LocalInputSource(file); |
| 85 | + var response = executeRequest(client, inputSource); |
| 86 | + printOutput(response); |
| 87 | + return 0; |
| 88 | + } |
| 89 | + |
| 90 | + private void printOutput(CommonResponse response) throws Exception { |
| 91 | + switch (output) { |
| 92 | + case Full: |
| 93 | + System.out.println(getFullOutput(response)); |
| 94 | + break; |
| 95 | + case Raw: |
| 96 | + var mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT); |
| 97 | + var jsonNode = mapper.readTree(response.getRawResponse()); |
| 98 | + System.out.println(mapper.writeValueAsString(jsonNode)); |
| 99 | + break; |
| 100 | + default: |
| 101 | + System.out.println(getSummary(response)); |
| 102 | + break; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments