Skip to content

Commit

Permalink
weaviategh-175 address review comments for constructor and http error…
Browse files Browse the repository at this point in the history
… code
  • Loading branch information
HeliosX7 committed Apr 14, 2023
1 parent 1ea2774 commit 7cf2452
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/weaviate/client/v1/schema/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ClassGetter classGetter() {
}

public ClassExists exists() {
return new ClassExists(httpClient, config);
return new ClassExists(new ClassGetter(httpClient, config));
}

public ClassCreator classCreator() {
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/io/weaviate/client/v1/schema/api/ClassExists.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
package io.weaviate.client.v1.schema.api;

import io.weaviate.client.Config;
import io.weaviate.client.base.ClientResult;
import io.weaviate.client.base.Result;
import io.weaviate.client.base.WeaviateError;
import io.weaviate.client.base.WeaviateErrorMessage;
import io.weaviate.client.base.WeaviateErrorResponse;
import io.weaviate.client.base.http.HttpClient;
import io.weaviate.client.v1.schema.model.WeaviateClass;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;

import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Collections;

public class ClassExists implements ClientResult<Boolean> {
private final ClassGetter classGetter;
private String className;

public ClassExists(HttpClient httpClient, Config config) {
this.classGetter = new ClassGetter(httpClient, config);
public ClassExists(ClassGetter classGetter) {
this.classGetter = classGetter;
}

public ClassExists withClassName(String className) {
Expand All @@ -33,17 +30,18 @@ private Result<Boolean> toResult(WeaviateError error) {

@Override
public Result<Boolean> run() {
if (StringUtils.isEmpty(this.className)) {
if (StringUtils.isEmpty(className)) {
WeaviateErrorMessage errorMessage = WeaviateErrorMessage.builder()
.message("classname cannot be empty").build();
WeaviateErrorResponse errors = WeaviateErrorResponse.builder()
.error(Stream.of(errorMessage).collect(Collectors.toList())).build();
return new Result<>(500, null, errors);
.error(Collections.singletonList(errorMessage)).build();
return new Result<>(HttpStatus.SC_UNPROCESSABLE_ENTITY, null, errors);
}

Result<WeaviateClass> getterClass = this.classGetter.withClassName(this.className).run();
Result<WeaviateClass> getterClass = classGetter.withClassName(className).run();
if (getterClass.hasErrors()) {
return toResult(getterClass.getError());
WeaviateError error = getterClass.getError();
return new Result<>(error.getStatusCode(), null, WeaviateErrorResponse.builder().error(error.getMessages()).build());
}
return new Result<>(HttpStatus.SC_OK, getterClass.getResult() != null, null);
}
Expand Down

0 comments on commit 7cf2452

Please sign in to comment.