Skip to content

Commit

Permalink
Feature/44 Add getBucket method to ReductClient. (#64)
Browse files Browse the repository at this point in the history
feature/#44 Add getBucket method to ReductClient.
  • Loading branch information
Rumpelshtinskiy authored Dec 14, 2024
1 parent 2f4b26d commit c9cf3fb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
19 changes: 19 additions & 0 deletions src/main/java/store/reduct/client/ReductClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -229,4 +231,21 @@ public AccessToken createToken(String tokenName, TokenPermissions permissions)
HttpResponse<String> 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<String> httpResponse = this.sendAndGetOnlySuccess(builder, HttpResponse.BodyHandlers.ofString());
return JsonUtils.parseObject(httpResponse.body(), Bucket.class);
}
}
11 changes: 1 addition & 10 deletions src/main/java/store/reduct/model/bucket/Bucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down Expand Up @@ -99,15 +98,7 @@ private void unpackInfo(Map<String, Object> 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<String> 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;
}

Expand Down

0 comments on commit c9cf3fb

Please sign in to comment.