Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WEB] rewrite http.Response by java 17 record #1742

Merged
merged 1 commit into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ private <T> CompletionStage<Response<T>> send(HttpRequest request, TypeRef<T> ty
if (r.statusCode() < 400) {
if (typeRef != null && r.body() == null)
throw new IllegalStateException("There is no body!!!");
if (typeRef == null) return Response.of(null, r.statusCode());
return Response.of(jsonConverter.fromJson(r.body(), typeRef), r.statusCode());
if (typeRef == null) return new Response<>(r.statusCode());
return new Response<>(
jsonConverter.fromJson(r.body(), typeRef), r.statusCode());
}
if (r.body() == null || r.body().isBlank())
throw new HttpRequestException(r.statusCode());
Expand Down
36 changes: 3 additions & 33 deletions common/src/main/java/org/astraea/common/http/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,9 @@
*/
package org.astraea.common.http;

public interface Response<T> {
public record Response<T>(T body, int statusCode) {

int statusCode();

T body();

static Response<Void> of(int code) {
return new Response<>() {

@Override
public int statusCode() {
return code;
}

@Override
public Void body() {
return null;
}
};
}

static <T> Response<T> of(T body, int code) {
return new Response<>() {

@Override
public int statusCode() {
return code;
}

@Override
public T body() {
return body;
}
};
public Response(int code) {
this(null, code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,7 @@ void testExecutor() throws MalformedURLException, ExecutionException, Interrupte
var httpExecutor = Mockito.mock(HttpExecutor.class);
Mockito.when(httpExecutor.get(Mockito.any(), Mockito.eq(TypeRef.set(String.class))))
.thenReturn(
CompletableFuture.completedFuture(
new Response<>() {
@Override
public int statusCode() {
return 200;
}

@Override
public Set<String> body() {
return Set.of("SpecialConnectorName");
}
}));
CompletableFuture.completedFuture(new Response<>(Set.of("SpecialConnectorName"), 200)));

var connectorClient =
ConnectorClient.builder()
Expand Down
54 changes: 2 additions & 52 deletions common/src/test/java/org/astraea/common/http/ResponseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,69 +18,19 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.net.URI;
import java.net.http.HttpClient.Version;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Optional;
import javax.net.ssl.SSLSession;
import org.junit.jupiter.api.Test;

class ResponseTest {

@Test
void testStatusCode() {
var response = Response.of("aa", 10);
var response = new Response<>("aa", 10);
assertEquals(10, response.statusCode());
}

@Test
void testBody() {
var response = Response.of("bodyValue", 10);
var response = new Response<>("bodyValue", 10);
assertEquals("bodyValue", response.body());
}

static class EmptyResponseImpl implements HttpResponse<String> {

@Override
public int statusCode() {
return 0;
}

@Override
public HttpRequest request() {
return null;
}

@Override
public Optional<HttpResponse<String>> previousResponse() {
return Optional.empty();
}

@Override
public HttpHeaders headers() {
return null;
}

@Override
public String body() {
return null;
}

@Override
public Optional<SSLSession> sslSession() {
return Optional.empty();
}

@Override
public URI uri() {
return null;
}

@Override
public Version version() {
return null;
}
}
}