diff --git a/src/main/java/store/reduct/client/ReductClient.java b/src/main/java/store/reduct/client/ReductClient.java index dc671e2..195b907 100644 --- a/src/main/java/store/reduct/client/ReductClient.java +++ b/src/main/java/store/reduct/client/ReductClient.java @@ -1,13 +1,6 @@ package store.reduct.client; -import static store.reduct.utils.Strings.isNotBlank; - import com.fasterxml.jackson.databind.ObjectMapper; -import java.io.IOException; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; import lombok.Getter; import lombok.RequiredArgsConstructor; import store.reduct.client.config.ServerProperties; @@ -25,14 +18,24 @@ import store.reduct.utils.JsonUtils; import store.reduct.utils.http.HttpStatus; +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +import static store.reduct.utils.Strings.isNotBlank; + /** * Base class for all clients. */ @RequiredArgsConstructor @Getter public class ReductClient { + public static final String BUCKET_NAME_CANNOT_BE_NULL_OR_EMPTY = "Bucket name cannot be null or empty."; private static final String REDUCT_ERROR_HEADER = "x-reduct-error"; public static final String S_S = "%s/%s"; + private final ServerProperties serverProperties; private final HttpClient httpClient; private final ObjectMapper objectMapper = new ObjectMapper(); @@ -229,4 +232,15 @@ public AccessToken createToken(String tokenName, TokenPermissions permissions) HttpResponse response = sendAndGetOnlySuccess(builder, HttpResponse.BodyHandlers.ofString()); return JsonUtils.parseObject(response.body(), AccessToken.class); } + + public Bucket getBucket(String bucketName) { + if (bucketName == null || bucketName.isBlank()) { + throw new IllegalArgumentException(BUCKET_NAME_CANNOT_BE_NULL_OR_EMPTY); + } + String createBucketPath = BucketURL.GET_BUCKET.getUrl().formatted(bucketName); + HttpRequest.Builder builder = HttpRequest.newBuilder() + .uri(URI.create(S_S.formatted(this.getServerProperties().url(), createBucketPath))).GET(); + HttpResponse httpResponse = this.sendAndGetOnlySuccess(builder, HttpResponse.BodyHandlers.ofString()); + return JsonUtils.parseObject(httpResponse.body(), Bucket.class); + } } diff --git a/src/main/java/store/reduct/model/bucket/Bucket.java b/src/main/java/store/reduct/model/bucket/Bucket.java index 7b0c0cb..183e5ad 100644 --- a/src/main/java/store/reduct/model/bucket/Bucket.java +++ b/src/main/java/store/reduct/model/bucket/Bucket.java @@ -1,17 +1,7 @@ package store.reduct.model.bucket; -import static store.reduct.utils.http.HttpHeaders.*; - import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; -import java.math.BigInteger; -import java.net.URI; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.nio.ByteBuffer; -import java.time.Instant; -import java.util.*; -import java.util.concurrent.PriorityBlockingQueue; import lombok.*; import org.apache.commons.lang3.ArrayUtils; import store.reduct.client.ReductClient; @@ -25,6 +15,17 @@ import store.reduct.utils.Strings; import store.reduct.utils.http.Queries; +import java.math.BigInteger; +import java.net.URI; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.ByteBuffer; +import java.time.Instant; +import java.util.*; +import java.util.concurrent.PriorityBlockingQueue; + +import static store.reduct.utils.http.HttpHeaders.*; + @NoArgsConstructor @EqualsAndHashCode @ToString @@ -34,7 +35,6 @@ public class Bucket { private static final String TS = "ts"; - public static final String BUCKET_NAME_CANNOT_BE_NULL_OR_EMPTY = "Bucket name cannot be null or empty."; public static final String X_REDUCT_TIME_IS_NOT_SUCH_LONG_FORMAT = "Received from server x-reduct-time is not such Long format, or empty."; public static final String CONTENT_TYPE_IS_NOT_SET_IN_THE_RECORD = "The Content-Type is not set in the record."; public static final String CONTENT_LENGTH_IS_NOT_SET_IN_THE_RECORD = "The Content-Length is not set in the record."; @@ -99,15 +99,7 @@ private void unpackInfo(Map info) { * @return Returns this Bucket object with updated fields */ public Bucket read() throws ReductException, IllegalArgumentException { - if (name == null || name.isBlank()) { - throw new IllegalArgumentException(BUCKET_NAME_CANNOT_BE_NULL_OR_EMPTY); - } - String createBucketPath = BucketURL.GET_BUCKET.getUrl().formatted(name); - HttpRequest.Builder builder = HttpRequest.newBuilder() - .uri(URI.create("%s/%s".formatted(reductClient.getServerProperties().url(), createBucketPath))).GET(); - HttpResponse httpResponse = reductClient.sendAndGetOnlySuccess(builder, - HttpResponse.BodyHandlers.ofString()); - BucketMapper.INSTANCE.copy(this, JsonUtils.parseObject(httpResponse.body(), Bucket.class)); + BucketMapper.INSTANCE.copy(this, reductClient.getBucket(name)); return this; }