Skip to content

Commit

Permalink
feature/#44 Add getBucket method to ReductClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rumpelshtinskiy committed Nov 10, 2024
1 parent b6d8f1b commit 72fbf3d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
28 changes: 21 additions & 7 deletions src/main/java/store/reduct/client/ReductClient.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -229,4 +232,15 @@ public AccessToken createToken(String tokenName, TokenPermissions permissions)
HttpResponse<String> 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<String> httpResponse = this.sendAndGetOnlySuccess(builder, HttpResponse.BodyHandlers.ofString());
return JsonUtils.parseObject(httpResponse.body(), Bucket.class);
}
}
32 changes: 12 additions & 20 deletions src/main/java/store/reduct/model/bucket/Bucket.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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.";
Expand Down Expand Up @@ -99,15 +99,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 72fbf3d

Please sign in to comment.