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
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.mindee.v2.clientoptions;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import lombok.Data;

/**
* Base parameters for sending a file to a Mindee V2 product.
*/
@Data
public abstract class BaseProductParameters {
/**
Expand All @@ -23,15 +26,35 @@ public abstract class BaseProductParameters {
*/
protected final String[] webhookIds;

protected BaseProductParameters(String modelId, String alias, String[] webhookIds) {
if (modelId == null || modelId.trim().isBlank()) {
throw new IllegalArgumentException("modelId cannot be null or whitespace.");
}
if ("".equals(alias)) {
throw new IllegalArgumentException("alias cannot be an empty string.");
}
if (
webhookIds != null && Arrays.stream(webhookIds).anyMatch(id -> id == null || id.isBlank())
) {
throw new IllegalArgumentException(
"WebhookIds cannot contain null, empty, or whitespace values."
);
}

this.modelId = modelId.trim();
this.alias = alias;
this.webhookIds = webhookIds != null ? webhookIds : new String[0];
}

public Map<String, String> getRequestParameters() {
var parameters = new HashMap<String, String>();

parameters.put("model_id", this.getModelId());

if (this.getAlias() != null && !this.getAlias().isBlank()) {
if (this.getAlias() != null) {
parameters.put("alias", getAlias());
}
Comment thread
ianardee marked this conversation as resolved.
if (this.getWebhookIds().length > 0) {
if (this.getWebhookIds() != null && this.getWebhookIds().length > 0) {
parameters.put("webhook_ids", String.join(",", this.getWebhookIds()));
}

Expand All @@ -49,8 +72,7 @@ protected T self() {
}

protected BaseBuilder(String modelId) {
this.modelId = Objects
.requireNonNull(modelId, "The model ID is required in product parameters");
this.modelId = modelId;
}

/** Set an alias for the uploaded document. */
Expand All @@ -65,5 +87,4 @@ public T webhookIds(String[] webhookIds) {
return self();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,22 @@ public abstract class BaseSearchParameters<TSearchResponse extends BaseSearchRes
*/
protected final Integer perPage;

/**
* Base constructor.
*/
protected BaseSearchParameters(
Class<TSearchResponse> responseClass,
Integer page,
Integer perPage
) {
this.responseClass = Objects.requireNonNull(responseClass, "responseClass cannot be null");
if (page != null && page <= 0) {
throw new IllegalArgumentException("page must be greater than 0");
}
if (perPage != null && perPage <= 0) {
throw new IllegalArgumentException("perPage must be greater than 0");
}

this.page = page;
this.perPage = perPage;
}
Expand All @@ -39,15 +49,9 @@ public Map<String, String> getRequestParameters() {
var parameters = new HashMap<String, String>();

if (this.getPage() != null) {
if (this.getPage() <= 0) {
throw new IllegalArgumentException("page must be greater than 0");
}
parameters.put("page", String.valueOf(getPage()));
}
if (this.getPerPage() != null) {
if (this.getPerPage() <= 0) {
throw new IllegalArgumentException("perPage must be greater than 0");
}
parameters.put("per_page", String.valueOf(getPerPage()));
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/mindee/v2/http/MindeeHttpApiV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ public <TResponse extends CommonResponse> TResponse reqGetResultById(
Class<TResponse> responseClass,
String inferenceId
) {
if (inferenceId == null || inferenceId.trim().isEmpty()) {
throw new IllegalArgumentException("inferenceId cannot be null or empty.");
}
Comment thread
ianardee marked this conversation as resolved.
var productInfo = getResponseProductAttributes(responseClass);
var url = String
.format(
Expand All @@ -150,7 +153,7 @@ public <TResponse extends CommonResponse> TResponse reqGetResultByUrl(
String inferenceUrl
) {
if (inferenceUrl == null || inferenceUrl.trim().isEmpty()) {
throw new IllegalArgumentException("inferenceUrl must not be null or blank.");
throw new IllegalArgumentException("inferenceUrl cannot be null or empty.");
}
validateInferenceUrl(inferenceUrl);
var get = new HttpGet(inferenceUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,18 @@ public class ModelSearchParameters extends BaseSearchParameters<ModelSearchRespo
*/
private final String modelType;

/**
* Default constructor.
*/
Comment thread
ianardee marked this conversation as resolved.
private ModelSearchParameters(String name, String modelType, Integer page, Integer perPage) {
super(ModelSearchResponse.class, page, perPage);
if ("".equals(name)) {
throw new IllegalArgumentException("name cannot be an empty string.");
}
if (modelType != null && modelType.trim().isEmpty()) {
throw new IllegalArgumentException("modelType cannot be whitespace");
}
Comment thread
ianardee marked this conversation as resolved.

this.name = name;
this.modelType = modelType;
}
Expand All @@ -32,11 +42,11 @@ private ModelSearchParameters(String name, String modelType, Integer page, Integ
public Map<String, String> getRequestParameters() {
var parameters = new HashMap<>(super.getRequestParameters());

if (this.getName() != null && !this.getName().isEmpty()) {
parameters.put("name", this.getName());
if (getName() != null) {
parameters.put("name", getName());
}
if (this.getModelType() != null && !this.getModelType().isEmpty()) {
parameters.put("model_type", this.getModelType());
if (getModelType() != null) {
parameters.put("model_type", getModelType());
}

return parameters;
Expand Down Expand Up @@ -65,19 +75,15 @@ public static final class Builder extends BaseSearchParameters.BaseBuilder<Build
* Case-insensitive search term for the model name
*/
public Builder name(String name) {
if (name != null && !name.isEmpty()) {
this.name = name;
}
this.name = name;
return this;
}

/**
* Case-insensitive search term for the model type
*/
public Builder modelType(String modelType) {
if (modelType != null && !modelType.trim().isEmpty()) {
this.modelType = modelType;
}
this.modelType = modelType;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,24 @@ private RagDocumentSearchParameters(
) {
super(RagDocumentSearchResponse.class, page, perPage);
if (modelId == null || modelId.trim().isEmpty()) {
throw new IllegalArgumentException("ModelId is required in RagDocumentSearchParameters");
throw new IllegalArgumentException("modelId cannot be null or whitespace.");
}
this.modelId = modelId;
if ("".equals(filename)) {
throw new IllegalArgumentException("filename cannot be an empty string.");
}

this.modelId = modelId.trim();
this.filename = filename;
}

@Override
public Map<String, String> getRequestParameters() {
var parameters = new HashMap<>(super.getRequestParameters());

parameters.put("model_id", this.getModelId());
parameters.put("model_id", getModelId());

if (this.getFilename() != null && !this.getFilename().isEmpty()) {
parameters.put("filename", this.getFilename());
if (getFilename() != null && !getFilename().isEmpty()) {
parameters.put("filename", getFilename());
}

return parameters;
Expand Down Expand Up @@ -74,17 +78,15 @@ public static final class Builder extends BaseSearchParameters.BaseBuilder<Build
* Case-insensitive substring search on filename.
*/
public Builder filename(String filename) {
if (filename != null && !filename.isEmpty()) {
this.filename = filename;
}
this.filename = filename;
return this;
}

/**
* Build an immutable {@link RagDocumentSearchParameters} instance.
*/
public RagDocumentSearchParameters build() {
return new RagDocumentSearchParameters(this.modelId, this.filename, this.page, this.perPage);
return new RagDocumentSearchParameters(modelId, filename, page, perPage);
}
Comment thread
ianardee marked this conversation as resolved.
}
}
Loading