Skip to content

Commit 7c29d7b

Browse files
committed
🥅 better checks for request parameters
1 parent 8c4f97d commit 7c29d7b

6 files changed

Lines changed: 67 additions & 35 deletions

File tree

src/main/java/com/mindee/v2/MindeeClient.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ public <TResponse extends CommonResponse> TResponse getResult(
8686
Class<TResponse> responseClass,
8787
String inferenceId
8888
) {
89-
if (inferenceId == null || inferenceId.trim().isEmpty()) {
90-
throw new IllegalArgumentException("inferenceId must not be null or blank.");
91-
}
9289
return mindeeApi.reqGetResultById(responseClass, inferenceId);
9390
}
9491

@@ -100,9 +97,6 @@ public <TResponse extends CommonResponse> TResponse getResultFromUrl(
10097
Class<TResponse> responseClass,
10198
String inferenceUrl
10299
) {
103-
if (inferenceUrl == null || inferenceUrl.trim().isEmpty()) {
104-
throw new IllegalArgumentException("inferenceUrl must not be null or blank.");
105-
}
106100
return mindeeApi.reqGetResultByUrl(responseClass, inferenceUrl);
107101
}
108102

src/main/java/com/mindee/v2/clientoptions/BaseProductParameters.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.mindee.v2.clientoptions;
22

3+
import java.util.Arrays;
34
import java.util.HashMap;
45
import java.util.Map;
5-
import java.util.Objects;
66
import lombok.Data;
77

8+
/**
9+
* Base parameters for sending a file to a Mindee V2 product.
10+
*/
811
@Data
912
public abstract class BaseProductParameters {
1013
/**
@@ -23,15 +26,35 @@ public abstract class BaseProductParameters {
2326
*/
2427
protected final String[] webhookIds;
2528

29+
protected BaseProductParameters(String modelId, String alias, String[] webhookIds) {
30+
if (modelId == null || modelId.trim().isBlank()) {
31+
throw new IllegalArgumentException("modelId cannot be null or whitespace.");
32+
}
33+
if ("".equals(alias)) {
34+
throw new IllegalArgumentException("alias cannot be an empty string.");
35+
}
36+
if (
37+
webhookIds != null && Arrays.stream(webhookIds).anyMatch(id -> id == null || id.isBlank())
38+
) {
39+
throw new IllegalArgumentException(
40+
"WebhookIds cannot contain null, empty, or whitespace values."
41+
);
42+
}
43+
44+
this.modelId = modelId.trim();
45+
this.alias = alias;
46+
this.webhookIds = webhookIds != null ? webhookIds : new String[0];
47+
}
48+
2649
public Map<String, String> getRequestParameters() {
2750
var parameters = new HashMap<String, String>();
2851

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

31-
if (this.getAlias() != null && !this.getAlias().isBlank()) {
54+
if (this.getAlias() != null) {
3255
parameters.put("alias", getAlias());
3356
}
34-
if (this.getWebhookIds().length > 0) {
57+
if (this.getWebhookIds() != null && this.getWebhookIds().length > 0) {
3558
parameters.put("webhook_ids", String.join(",", this.getWebhookIds()));
3659
}
3760

@@ -49,8 +72,7 @@ protected T self() {
4972
}
5073

5174
protected BaseBuilder(String modelId) {
52-
this.modelId = Objects
53-
.requireNonNull(modelId, "The model ID is required in product parameters");
75+
this.modelId = modelId;
5476
}
5577

5678
/** Set an alias for the uploaded document. */
@@ -65,5 +87,4 @@ public T webhookIds(String[] webhookIds) {
6587
return self();
6688
}
6789
}
68-
6990
}

src/main/java/com/mindee/v2/clientoptions/BaseSearchParameters.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,22 @@ public abstract class BaseSearchParameters<TSearchResponse extends BaseSearchRes
2222
*/
2323
protected final Integer perPage;
2424

25+
/**
26+
* Base constructor.
27+
*/
2528
protected BaseSearchParameters(
2629
Class<TSearchResponse> responseClass,
2730
Integer page,
2831
Integer perPage
2932
) {
3033
this.responseClass = Objects.requireNonNull(responseClass, "responseClass cannot be null");
34+
if (page != null && page <= 0) {
35+
throw new IllegalArgumentException("page must be greater than 0");
36+
}
37+
if (perPage != null && perPage <= 0) {
38+
throw new IllegalArgumentException("perPage must be greater than 0");
39+
}
40+
3141
this.page = page;
3242
this.perPage = perPage;
3343
}
@@ -39,15 +49,9 @@ public Map<String, String> getRequestParameters() {
3949
var parameters = new HashMap<String, String>();
4050

4151
if (this.getPage() != null) {
42-
if (this.getPage() <= 0) {
43-
throw new IllegalArgumentException("page must be greater than 0");
44-
}
4552
parameters.put("page", String.valueOf(getPage()));
4653
}
4754
if (this.getPerPage() != null) {
48-
if (this.getPerPage() <= 0) {
49-
throw new IllegalArgumentException("perPage must be greater than 0");
50-
}
5155
parameters.put("per_page", String.valueOf(getPerPage()));
5256
}
5357

src/main/java/com/mindee/v2/http/MindeeHttpApiV2.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ public <TResponse extends CommonResponse> TResponse reqGetResultById(
133133
Class<TResponse> responseClass,
134134
String inferenceId
135135
) {
136+
if (inferenceId == null || inferenceId.trim().isEmpty()) {
137+
throw new IllegalArgumentException("inferenceId cannot be null or empty.");
138+
}
136139
var productInfo = getResponseProductAttributes(responseClass);
137140
var url = String
138141
.format(
@@ -150,7 +153,7 @@ public <TResponse extends CommonResponse> TResponse reqGetResultByUrl(
150153
String inferenceUrl
151154
) {
152155
if (inferenceUrl == null || inferenceUrl.trim().isEmpty()) {
153-
throw new IllegalArgumentException("inferenceUrl must not be null or blank.");
156+
throw new IllegalArgumentException("inferenceUrl cannot be null or empty.");
154157
}
155158
validateInferenceUrl(inferenceUrl);
156159
var get = new HttpGet(inferenceUrl);

src/main/java/com/mindee/v2/search/models/ModelSearchParameters.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,18 @@ public class ModelSearchParameters extends BaseSearchParameters<ModelSearchRespo
2222
*/
2323
private final String modelType;
2424

25+
/**
26+
* Default constructor.
27+
*/
2528
private ModelSearchParameters(String name, String modelType, Integer page, Integer perPage) {
2629
super(ModelSearchResponse.class, page, perPage);
30+
if ("".equals(name)) {
31+
throw new IllegalArgumentException("name cannot be an empty string.");
32+
}
33+
if (modelType != null && modelType.trim().isEmpty()) {
34+
throw new IllegalArgumentException("modelType cannot be whitespace");
35+
}
36+
2737
this.name = name;
2838
this.modelType = modelType;
2939
}
@@ -32,11 +42,11 @@ private ModelSearchParameters(String name, String modelType, Integer page, Integ
3242
public Map<String, String> getRequestParameters() {
3343
var parameters = new HashMap<>(super.getRequestParameters());
3444

35-
if (this.getName() != null && !this.getName().isEmpty()) {
36-
parameters.put("name", this.getName());
45+
if (getName() != null) {
46+
parameters.put("name", getName());
3747
}
38-
if (this.getModelType() != null && !this.getModelType().isEmpty()) {
39-
parameters.put("model_type", this.getModelType());
48+
if (getModelType() != null) {
49+
parameters.put("model_type", getModelType());
4050
}
4151

4252
return parameters;
@@ -65,19 +75,15 @@ public static final class Builder extends BaseSearchParameters.BaseBuilder<Build
6575
* Case-insensitive search term for the model name
6676
*/
6777
public Builder name(String name) {
68-
if (name != null && !name.isEmpty()) {
69-
this.name = name;
70-
}
78+
this.name = name;
7179
return this;
7280
}
7381

7482
/**
7583
* Case-insensitive search term for the model type
7684
*/
7785
public Builder modelType(String modelType) {
78-
if (modelType != null && !modelType.trim().isEmpty()) {
79-
this.modelType = modelType;
80-
}
86+
this.modelType = modelType;
8187
return this;
8288
}
8389

src/main/java/com/mindee/v2/search/ragdocuments/RagDocumentSearchParameters.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,24 @@ private RagDocumentSearchParameters(
3030
) {
3131
super(RagDocumentSearchResponse.class, page, perPage);
3232
if (modelId == null || modelId.trim().isEmpty()) {
33-
throw new IllegalArgumentException("ModelId is required in RagDocumentSearchParameters");
33+
throw new IllegalArgumentException("modelId cannot be null or whitespace.");
3434
}
35-
this.modelId = modelId;
35+
if ("".equals(filename)) {
36+
throw new IllegalArgumentException("filename cannot be an empty string.");
37+
}
38+
39+
this.modelId = modelId.trim();
3640
this.filename = filename;
3741
}
3842

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

43-
parameters.put("model_id", this.getModelId());
47+
parameters.put("model_id", getModelId());
4448

45-
if (this.getFilename() != null && !this.getFilename().isEmpty()) {
46-
parameters.put("filename", this.getFilename());
49+
if (getFilename() != null && !getFilename().isEmpty()) {
50+
parameters.put("filename", getFilename());
4751
}
4852

4953
return parameters;
@@ -84,7 +88,7 @@ public Builder filename(String filename) {
8488
* Build an immutable {@link RagDocumentSearchParameters} instance.
8589
*/
8690
public RagDocumentSearchParameters build() {
87-
return new RagDocumentSearchParameters(this.modelId, this.filename, this.page, this.perPage);
91+
return new RagDocumentSearchParameters(modelId, filename, page, perPage);
8892
}
8993
}
9094
}

0 commit comments

Comments
 (0)