|
|
|
@ -18,6 +18,7 @@ import java.util.Optional; |
|
|
|
|
import java.util.concurrent.CompletableFuture; |
|
|
|
|
import java.util.concurrent.ExecutionException; |
|
|
|
|
|
|
|
|
|
import io.netty.handler.codec.http.HttpHeaderNames; |
|
|
|
|
import io.vertx.core.http.HttpClient; |
|
|
|
|
import io.vertx.core.http.HttpClientRequest; |
|
|
|
|
import io.vertx.core.http.HttpClientResponse; |
|
|
|
@ -26,6 +27,7 @@ import io.vertx.core.http.HttpMethod; |
|
|
|
|
|
|
|
|
|
public class VertxRequestTransmitter implements RequestTransmitter { |
|
|
|
|
|
|
|
|
|
private static final String APPLICATION_JSON = "application/json"; |
|
|
|
|
private final HttpClient client; |
|
|
|
|
private static final long REQUEST_TIMEOUT_MS = 5000L; |
|
|
|
|
|
|
|
|
@ -40,7 +42,12 @@ public class VertxRequestTransmitter implements RequestTransmitter { |
|
|
|
|
final String endpoint, |
|
|
|
|
final ResponseBodyHandler<T> responseHandler) { |
|
|
|
|
return sendRequest( |
|
|
|
|
HttpMethod.POST, Optional.of(contentType), Optional.of(content), endpoint, responseHandler); |
|
|
|
|
HttpMethod.POST, |
|
|
|
|
Optional.of(contentType), |
|
|
|
|
Optional.of(content), |
|
|
|
|
endpoint, |
|
|
|
|
responseHandler, |
|
|
|
|
false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@ -48,13 +55,15 @@ public class VertxRequestTransmitter implements RequestTransmitter { |
|
|
|
|
final String contentType, |
|
|
|
|
final String content, |
|
|
|
|
final String endpoint, |
|
|
|
|
final ResponseBodyHandler<T> responseHandler) { |
|
|
|
|
final ResponseBodyHandler<T> responseHandler, |
|
|
|
|
final boolean withAcceptJsonHeader) { |
|
|
|
|
return sendRequest( |
|
|
|
|
HttpMethod.GET, |
|
|
|
|
Optional.ofNullable(contentType), |
|
|
|
|
Optional.ofNullable(content), |
|
|
|
|
endpoint, |
|
|
|
|
responseHandler); |
|
|
|
|
responseHandler, |
|
|
|
|
withAcceptJsonHeader); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected <T> T sendRequest( |
|
|
|
@ -62,7 +71,8 @@ public class VertxRequestTransmitter implements RequestTransmitter { |
|
|
|
|
final Optional<String> contentType, |
|
|
|
|
final Optional<String> content, |
|
|
|
|
final String endpoint, |
|
|
|
|
final ResponseBodyHandler<T> responseHandler) { |
|
|
|
|
final ResponseBodyHandler<T> responseHandler, |
|
|
|
|
final boolean withAcceptJsonHeader) { |
|
|
|
|
try { |
|
|
|
|
final CompletableFuture<T> result = new CompletableFuture<>(); |
|
|
|
|
final HttpClientRequest request = |
|
|
|
@ -72,6 +82,10 @@ public class VertxRequestTransmitter implements RequestTransmitter { |
|
|
|
|
.setTimeout(REQUEST_TIMEOUT_MS) |
|
|
|
|
.exceptionHandler(result::completeExceptionally) |
|
|
|
|
.setChunked(false); |
|
|
|
|
if (withAcceptJsonHeader) { |
|
|
|
|
// this is needed when using Tessera GET /transaction/{hash} to choose the right RPC
|
|
|
|
|
request.putHeader(HttpHeaderNames.ACCEPT, APPLICATION_JSON); |
|
|
|
|
} |
|
|
|
|
contentType.ifPresent(ct -> request.putHeader(HttpHeaders.CONTENT_TYPE, ct)); |
|
|
|
|
if (content.isPresent()) { |
|
|
|
|
request.end(content.get()); |
|
|
|
|