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
2 changes: 2 additions & 0 deletions src/main/java/com/mindee/CommandLineInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.mindee.v2.cli.ExtractionCommand;
import com.mindee.v2.cli.OcrCommand;
import com.mindee.v2.cli.SearchModelsCommand;
import com.mindee.v2.cli.SearchRagDocumentsCommand;
import com.mindee.v2.cli.SplitCommand;
import java.lang.reflect.Method;
import picocli.CommandLine;
Expand Down Expand Up @@ -42,6 +43,7 @@ public static void main(String[] args) {

// V2 commands at root
rootCmd.addSubcommand("search-models", new CommandLine(new SearchModelsCommand()));
rootCmd.addSubcommand("search-rag-docs", new CommandLine(new SearchRagDocumentsCommand()));
rootCmd.addSubcommand("classification", new CommandLine(new ClassificationCommand()));
rootCmd.addSubcommand("crop", new CommandLine(new CropCommand()));
rootCmd.addSubcommand("extraction", new CommandLine(new ExtractionCommand()));
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/mindee/MindeeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/
public class MindeeException extends RuntimeException {

public MindeeException(String errorMessage, Throwable err) {
super(errorMessage, err);
public MindeeException(String message, Throwable cause) {
super(message, cause);
}

public MindeeException(String errorMessage) {
super(errorMessage);
public MindeeException(String message) {
super(message);
}
}
58 changes: 42 additions & 16 deletions src/main/java/com/mindee/v2/MindeeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

import com.mindee.input.LocalInputSource;
import com.mindee.input.URLInputSource;
import com.mindee.v2.clientoptions.BaseParameters;
import com.mindee.v2.clientoptions.BaseProductParameters;
import com.mindee.v2.clientoptions.BaseSearchParameters;
import com.mindee.v2.clientoptions.PollingOptions;
import com.mindee.v2.http.MindeeApiV2;
import com.mindee.v2.http.MindeeHttpApiV2;
import com.mindee.v2.http.MindeeHttpExceptionV2;
import com.mindee.v2.parsing.CommonResponse;
import com.mindee.v2.parsing.JobResponse;
import com.mindee.v2.parsing.error.ErrorResponse;
import com.mindee.v2.parsing.search.BaseSearchResponse;
import com.mindee.v2.parsing.search.SearchResponse;
import com.mindee.v2.product.extraction.ExtractionResponse;
import com.mindee.v2.search.models.ModelSearchParameters;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.CancellationException;

/**
Expand Down Expand Up @@ -44,7 +48,7 @@ public MindeeClient(MindeeApiV2 mindeeApi) {
*/
public JobResponse enqueue(
LocalInputSource inputSource,
BaseParameters params
BaseProductParameters params
) throws IOException {
return mindeeApi.reqPostEnqueue(inputSource, params);
Comment thread
ianardee marked this conversation as resolved.
}
Expand All @@ -55,7 +59,10 @@ public JobResponse enqueue(
* @param inputSource The URL input source to send.
* @param params The parameters to send along with the file.
*/
public JobResponse enqueue(URLInputSource inputSource, BaseParameters params) throws IOException {
public JobResponse enqueue(
URLInputSource inputSource,
BaseProductParameters params
) throws IOException {
inputSource.validateSecure();
return mindeeApi.reqPostEnqueue(inputSource, params);
}
Expand All @@ -68,7 +75,7 @@ public JobResponse getJob(String jobId) {
if (jobId == null || jobId.trim().isEmpty()) {
throw new IllegalArgumentException("jobId must not be null or blank.");
}
return mindeeApi.reqGetJob(jobId);
return mindeeApi.reqGetJobById(jobId);
}

/**
Expand All @@ -82,7 +89,7 @@ public <TResponse extends CommonResponse> TResponse getResult(
if (inferenceId == null || inferenceId.trim().isEmpty()) {
throw new IllegalArgumentException("inferenceId must not be null or blank.");
}
return mindeeApi.reqGetResult(responseClass, inferenceId);
return mindeeApi.reqGetResultById(responseClass, inferenceId);
}

/**
Expand All @@ -96,7 +103,7 @@ public <TResponse extends CommonResponse> TResponse getResultFromUrl(
if (inferenceUrl == null || inferenceUrl.trim().isEmpty()) {
throw new IllegalArgumentException("inferenceUrl must not be null or blank.");
}
return mindeeApi.reqGetResultFromUrl(responseClass, inferenceUrl);
return mindeeApi.reqGetResultByUrl(responseClass, inferenceUrl);
}

/**
Expand All @@ -112,7 +119,7 @@ public <TResponse extends CommonResponse> TResponse getResultFromUrl(
public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
Class<TResponse> responseClass,
LocalInputSource inputSource,
BaseParameters params
BaseProductParameters params
) throws IOException, InterruptedException {
return enqueueAndGetResult(
responseClass,
Expand All @@ -136,7 +143,7 @@ public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
Class<TResponse> responseClass,
LocalInputSource inputSource,
BaseParameters params,
BaseProductParameters params,
PollingOptions pollingOptions
) throws IOException, InterruptedException {
JobResponse job = enqueue(inputSource, params);
Expand All @@ -156,7 +163,7 @@ public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
Class<TResponse> responseClass,
URLInputSource inputSource,
BaseParameters params
BaseProductParameters params
) throws IOException, InterruptedException {
return enqueueAndGetResult(
responseClass,
Expand All @@ -180,31 +187,47 @@ public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
public <TResponse extends CommonResponse> TResponse enqueueAndGetResult(
Class<TResponse> responseClass,
URLInputSource inputSource,
BaseParameters params,
BaseProductParameters params,
PollingOptions pollingOptions
) throws IOException, InterruptedException {
inputSource.validateSecure();
JobResponse job = enqueue(inputSource, params);
return pollAndFetch(responseClass, job, pollingOptions);
}

/**
* Search for resources matching the given criteria.
*
* @param searchParameters Search parameters
*/
public <TSearchResponse extends BaseSearchResponse> TSearchResponse search(
BaseSearchParameters<TSearchResponse> searchParameters
) {
Objects.requireNonNull(searchParameters);
return mindeeApi.reqGetSearch(searchParameters);
}

/**
* Return all models.
*
* @return an instance of {@link SearchResponse}
* @deprecated Use {@link #search} instead.
*/
@Deprecated
public SearchResponse searchModels() {
return searchModels(null, null);
return mindeeApi.reqGetSearch(ModelSearchParameters.builder().build());
}
Comment thread
ianardee marked this conversation as resolved.
Comment thread
ianardee marked this conversation as resolved.

/**
* Search for models by name.
*
* @param modelName name of the model to search for
* @return an instance of {@link SearchResponse}
* @deprecated Use {@link #search} instead.
*/
@Deprecated
public SearchResponse searchModels(String modelName) {
return searchModels(modelName, null);
return mindeeApi.reqGetSearch(ModelSearchParameters.builder().name(modelName).build());
}

/**
Expand All @@ -213,9 +236,12 @@ public SearchResponse searchModels(String modelName) {
* @param modelName name of the model to search for
* @param modelType type of the model to search for
* @return an instance of {@link SearchResponse}
* @deprecated Use {@link #search} instead.
*/
@Deprecated
public SearchResponse searchModels(String modelName, String modelType) {
return mindeeApi.reqGetSearchModels(modelName, modelType);
return mindeeApi
.reqGetSearch(ModelSearchParameters.builder().name(modelName).modelType(modelType).build());
}

/**
Expand Down Expand Up @@ -253,9 +279,9 @@ private <TResponse extends CommonResponse> TResponse pollAndFetch(
attempts++;
}

ErrorResponse error = resp.getJob().getError();
if (error != null) {
throw new MindeeHttpExceptionV2(error.getStatus(), error.getDetail());
ErrorResponse errorResponse = resp.getJob().getError();
if (errorResponse != null) {
throw new MindeeHttpExceptionV2(errorResponse);
}
throw new RuntimeException("Max retries exceeded (" + max + ").");
}
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/com/mindee/v2/cli/BaseCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.mindee.v2.cli;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.mindee.v2.parsing.CommonResponse;
import java.util.concurrent.Callable;
import picocli.CommandLine;

/**
* Abstract base class for all V2 CLI commands.
* Handles common options and output formatting.
*/
public abstract class BaseCommand implements Callable<Integer> {
@CommandLine.Option(
names = { "-k", "--api-key" },
description = "Mindee V2 API key.",
defaultValue = ""
)
protected String apiKey;

/** Output format for the command. */
public enum OutputType {
summary,
full,
raw
}

@CommandLine.Option(
names = { "-o", "--output" },
description = "Specify how to output the data.\n"
+ "- summary: basic formatted response (default)\n"
+ "- full: complete formatted response\n"
+ "- raw: full JSON object",
defaultValue = "summary"
)
protected OutputType output;

/**
* Returns the summary string for the given response.
* Override in each command.
*
* @param response the response
* @return the summary string
*/
protected abstract String getSummaryOutput(CommonResponse response);

/**
* Returns the full string for the given response.
*
* @param response the product response
* @return the full output string
*/
protected abstract String getFullOutput(CommonResponse response);

/**
* Prints the output to the console, taking into account the output type.
*/
protected void printOutput(CommonResponse response) throws Exception {
switch (output) {
case full:
System.out.println(getFullOutput(response));
break;
case raw:
var mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
var jsonNode = mapper.readTree(response.getRawResponse());
System.out.println(mapper.writeValueAsString(jsonNode));
break;
default:
System.out.println(getSummaryOutput(response));
break;
}
}
}
70 changes: 7 additions & 63 deletions src/main/java/com/mindee/v2/cli/BaseInferenceCommand.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
package com.mindee.v2.cli;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.mindee.input.LocalInputSource;
import com.mindee.v2.MindeeClient;
import com.mindee.v2.parsing.CommonResponse;
import java.io.File;
import java.util.List;
import java.util.concurrent.Callable;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

/**
* Abstract base class for V2 inference CLI commands.
* Handles common options (path, model-id, api-key, alias, output) and output formatting.
* Handles common enqueue options.
*/
public abstract class BaseInferenceCommand implements Callable<Integer> {
public abstract class BaseInferenceCommand extends BaseCommand {

@Parameters(index = "0", paramLabel = "<path>", description = "The path of the file to parse")
protected File file;

@Option(names = { "-m", "--model-id" }, description = "ID of the model to use", required = true)
protected String modelId;

@Option(names = { "-k", "--api-key" }, description = "Mindee V2 API key.")
protected String apiKey;

@Option(names = { "-a", "--alias" }, description = "Alias for the file")
@Option(
names = { "-a", "--alias" },
description = "A free-form string to tag the request with your own identifier."
)
protected String alias;

@Option(
Expand All @@ -35,23 +32,6 @@ public abstract class BaseInferenceCommand implements Callable<Integer> {
)
private List<String> webhookIds;

/** Output format for the command. */
public enum OutputType {
summary,
full,
raw
}

@Option(
names = { "-o", "--output" },
description = "Specify how to output the data.\n"
+ "- summary: a basic summary (default)\n"
+ "- full: detail extraction results, including options\n"
+ "- raw: full JSON object",
defaultValue = "summary"
)
protected OutputType output;

/**
* @return The properly formatted webhook IDs.
*/
Expand All @@ -72,48 +52,12 @@ protected abstract CommonResponse executeRequest(
LocalInputSource inputSource
) throws Exception;

/**
* Returns the summary (result-only) string for the given response.
* Override in each product command.
*
* @param response the product response
* @return the summary string
*/
protected abstract String getSummary(CommonResponse response);

/**
* Returns the full (inference + options + result) string for the given response.
* Defaults to the same as {@link #getSummary}; override for richer output.
*
* @param response the product response
* @return the full output string
*/
protected String getFullOutput(CommonResponse response) {
return getSummary(response);
}

@Override
public Integer call() throws Exception {
var client = new MindeeClient(apiKey != null ? apiKey : "");
var client = new MindeeClient(apiKey);
var inputSource = new LocalInputSource(file);
var response = executeRequest(client, inputSource);
printOutput(response);
return 0;
}

private void printOutput(CommonResponse response) throws Exception {
switch (output) {
case full:
System.out.println(getFullOutput(response));
break;
case raw:
var mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
var jsonNode = mapper.readTree(response.getRawResponse());
System.out.println(mapper.writeValueAsString(jsonNode));
break;
default:
System.out.println(getSummary(response));
break;
}
}
}
Loading
Loading