Skip to content

Commit

Permalink
[WEB] rewrite http.Response by java 17 record (#1742)
Browse files Browse the repository at this point in the history
  • Loading branch information
chia7712 authored May 13, 2023
1 parent 5a42227 commit a9ee445
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 99 deletions.
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;
}
}
}

0 comments on commit a9ee445

Please sign in to comment.