From 59bd140bfd842b70696a43e780c5bcf7ceac0686 Mon Sep 17 00:00:00 2001 From: Rustam Galikhanov Date: Sun, 10 Nov 2024 20:04:41 +0500 Subject: [PATCH] feature/#44 Add getBucket method to ReductClient. --- CHANGELOG.md | 1 + .../store/reduct/client/ReductClient.java | 19 +++++++++++++++++++ .../store/reduct/model/bucket/Bucket.java | 11 +---------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efc63af..4251e59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Improve ReductClient initialization. [PR-41](https://github.com/reductstore/reduct-java/issues/41) - Add exists option to BucketSettings. [PR-42](https://github.com/reductstore/reduct-java/issues/42) - Bucket.writeRecord receives entry name and bucket. [PR-43](https://github.com/reductstore/reduct-java/issues/43) +- Add getBucket method to ReductClient. [PR-44](https://github.com/reductstore/reduct-java/issues/44) ### Infrastructure: - Added GitHub Actions for CI/CD [PR-35](https://github.com/reductstore/reduct-java/pull/35) diff --git a/src/main/java/store/reduct/client/ReductClient.java b/src/main/java/store/reduct/client/ReductClient.java index dc671e2..0e54687 100644 --- a/src/main/java/store/reduct/client/ReductClient.java +++ b/src/main/java/store/reduct/client/ReductClient.java @@ -31,8 +31,10 @@ @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 +231,21 @@ public AccessToken createToken(String tokenName, TokenPermissions permissions) HttpResponse response = sendAndGetOnlySuccess(builder, HttpResponse.BodyHandlers.ofString()); return JsonUtils.parseObject(response.body(), AccessToken.class); } + + /** + * Get Information about a Bucket + * + * @param bucketName + * @return + */ + 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..c9c9f15 100644 --- a/src/main/java/store/reduct/model/bucket/Bucket.java +++ b/src/main/java/store/reduct/model/bucket/Bucket.java @@ -34,7 +34,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 +98,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; }