io.vrap.rmf.base.client.ApiHttpClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rmf-java-base Show documentation
Show all versions of rmf-java-base Show documentation
The e-commerce SDK from commercetools Composable Commerce for Java
package io.vrap.rmf.base.client;
import static io.vrap.rmf.base.client.utils.ClientUtils.blockingWait;
import java.net.URI;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import io.vrap.rmf.base.client.http.HandlerStack;
/**
* Interface for an HTTP client accessing the API and mapping to a known output type
*/
public interface ApiHttpClient extends AutoCloseable, VrapHttpClient, CompletableFutureApiClient {
public String CLOSED_MESSAGE = "Client is already closed.";
static Duration DEFAULT_TIMEOUT = Duration.ofSeconds(120);
default public CompletableFuture> execute(final ApiHttpRequest request,
final Class outputType) {
return execute(request, apiHttpResponse -> getSerializerService().convertResponse(apiHttpResponse, outputType));
}
default public CompletableFuture> execute(final ApiHttpRequest request,
final TypeReference outputType) {
return execute(request, apiHttpResponse -> getSerializerService().convertResponse(apiHttpResponse, outputType));
}
default public CompletableFuture> execute(final ApiHttpRequest request,
final JavaType outputType) {
return execute(request, apiHttpResponse -> getSerializerService().convertResponse(apiHttpResponse, outputType));
}
default public CompletableFuture> execute(final ApiHttpRequest request,
final Function, ApiHttpResponse> mapper) {
return execute(request).thenApply(mapper);
}
default public CompletableFuture> execute(final HttpRequestCommand method) {
return execute(method.createHttpRequest(), method.resultType());
}
default public CompletableFuture> execute(final CreateHttpRequestCommand method,
final Class outputType) {
return execute(method, apiHttpResponse -> getSerializerService().convertResponse(apiHttpResponse, outputType));
}
default public CompletableFuture> execute(final CreateHttpRequestCommand method,
final JavaType outputType) {
return execute(method, apiHttpResponse -> getSerializerService().convertResponse(apiHttpResponse, outputType));
}
default public CompletableFuture> execute(final CreateHttpRequestCommand method,
final TypeReference outputType) {
return execute(method, apiHttpResponse -> getSerializerService().convertResponse(apiHttpResponse, outputType));
}
default public CompletableFuture> execute(final CreateHttpRequestCommand method,
final Function, ApiHttpResponse> mapper) {
return execute(method.createHttpRequest()).thenApply(mapper);
}
default public ApiHttpResponse executeBlocking(final CreateHttpRequestCommand method,
final Function, ApiHttpResponse> mapper, Duration timeout) {
ApiHttpRequest request = method.createHttpRequest();
return blockingWait(execute(request).thenApply(mapper), request, timeout);
}
default public ApiHttpResponse executeBlocking(final CreateHttpRequestCommand method,
final Class outputType, Duration timeout) {
return executeBlocking(method,
apiHttpResponse -> getSerializerService().convertResponse(apiHttpResponse, outputType), timeout);
}
default public ApiHttpResponse executeBlocking(final CreateHttpRequestCommand method,
final JavaType outputType, Duration timeout) {
return executeBlocking(method,
apiHttpResponse -> getSerializerService().convertResponse(apiHttpResponse, outputType), timeout);
}
default public ApiHttpResponse executeBlocking(final CreateHttpRequestCommand method,
final TypeReference outputType, Duration timeout) {
return executeBlocking(method,
apiHttpResponse -> getSerializerService().convertResponse(apiHttpResponse, outputType), timeout);
}
default public CompletableFuture> send(final CreateHttpRequestCommand method) {
return execute(method.createHttpRequest());
}
default public ApiHttpResponse sendBlocking(final CreateHttpRequestCommand method,
final Duration timeout) {
ApiHttpRequest request = method.createHttpRequest();
return blockingWait(execute(request), request, timeout);
}
public ResponseSerializer getSerializerService();
public URI getBaseUri();
public static ApiHttpClient of(final String baseUri, final HandlerStack stack) {
return of(URI.create(baseUri), stack, ResponseSerializer.of());
}
public static ApiHttpClient of(final URI baseUri, final HandlerStack stack) {
return of(baseUri, stack, ResponseSerializer.of());
}
public static ApiHttpClient of(final String baseUri, final HandlerStack stack,
final ResponseSerializer serializer) {
return of(URI.create(baseUri), stack, serializer);
}
public static ApiHttpClient of(final URI baseUri, final HandlerStack stack, final ResponseSerializer serializer) {
return new ApiHttpClientImpl(baseUri, stack, serializer);
}
}