Skip to content

Commit

Permalink
Address code review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
  • Loading branch information
reta committed Apr 3, 2024
1 parent 62439b1 commit 5d97057
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,12 @@
public final class Requests {
private Requests() {}

/**
* Creates a new builder for bodyless requests
* @return a new builder for bodyless requests
*/
public static Builder builder() {
return new Builder();
}

/**
* Creates a new builder for requests with JSON body
* @return a new builder for requests with JSON body
*/
public static JsonBodyBuilder builder(JsonpMapper mapper) {
return new JsonBodyBuilder(mapper);
public static JsonBodyBuilder builder() {
return new JsonBodyBuilder();
}

/**
Expand All @@ -49,56 +41,17 @@ public static Request create(
return new GenericRequest(method, endpoint, headers, parameters, body);
}

/**
* A new builder for bodyless requests
*/
public static final class Builder {
private String method;
private String endpoint;
private Collection<Map.Entry<String, String>> headers = Collections.emptyList();
private Map<String, String> parameters = Collections.emptyMap();

private Builder() {}

public Builder endpoint(final String endpoint) {
this.endpoint = Objects.requireNonNull(endpoint, "endpoint cannot be null");
return this;
}

public Builder query(final Map<String, String> parameters) {
this.parameters = Objects.requireNonNull(parameters, "parameters cannot be null");
return this;
}

public Builder headers(final Collection<Map.Entry<String, String>> headers) {
this.headers = Objects.requireNonNull(headers, "headers cannot be null");
return this;
}

public Builder method(final String method) {
this.method = Objects.requireNonNull(method, "headers cannot be null");
return this;
}

public Request build() {
return new GenericRequest(method, endpoint, headers, parameters, null);
}
}

/**
* A new builder for requests with JSON body
*/
public static final class JsonBodyBuilder {
private final JsonpMapper mapper;
private String method;
private String endpoint;
private Collection<Map.Entry<String, String>> headers = Collections.emptyList();
private Map<String, String> parameters = Collections.emptyMap();
private Body body;

private JsonBodyBuilder(final JsonpMapper mapper) {
this.mapper = Objects.requireNonNull(mapper, "mapper cannot be null");
}
private JsonBodyBuilder() {}

public JsonBodyBuilder endpoint(final String endpoint) {
this.endpoint = Objects.requireNonNull(endpoint, "endpoint cannot be null");
Expand All @@ -115,7 +68,7 @@ public JsonBodyBuilder json(String str) {
return this;
}

public <C> JsonBodyBuilder json(C value) throws IOException {
public <C> JsonBodyBuilder json(C value, JsonpMapper mapper) throws IOException {
this.body = Bodies.json(value, mapper);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void shouldReturnSearchResults() throws Exception {
try (
Response response = javaClient().generic()
.execute(
Requests.builder(javaClient()._transport().jsonpMapper())
Requests.builder()
.endpoint("/" + index + "/_search")
.method("POST")
.json(
Expand Down Expand Up @@ -100,10 +100,10 @@ private void createTestDocument(String index, String id, ShopItem document) thro
try (
Response response = javaClient().generic()
.execute(
Requests.builder(javaClient()._transport().jsonpMapper())
Requests.builder()
.endpoint("/" + index + "/_doc/" + id)
.method("PUT")
.json(document)
.json(document, javaClient()._transport().jsonpMapper())
.build()
)
) {
Expand All @@ -116,7 +116,7 @@ private void createIndex(String index) throws IOException {
try (
Response response = javaClient().generic()
.execute(
Requests.builder(javaClient()._transport().jsonpMapper())
Requests.builder()
.endpoint("/" + index)
.method("PUT")
.json(
Expand Down Expand Up @@ -162,9 +162,7 @@ private void createIndex(String index) throws IOException {
private void refreshIndex(String index) throws IOException {
try (
Response response = javaClient().generic()
.execute(
Requests.builder(javaClient()._transport().jsonpMapper()).endpoint("/" + index + "/_refresh").method("POST").build()
)
.execute(Requests.builder().endpoint("/" + index + "/_refresh").method("POST").build())
) {
assertThat(response.getStatus(), equalTo(200));
assertThat(response.getBody().isPresent(), equalTo(true));
Expand Down

0 comments on commit 5d97057

Please sign in to comment.