All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vrap.rmf.base.client.ApiHttpClient Maven / Gradle / Ivy

There is a newer version: 17.17.0
Show newest version

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 {
    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 ClientRequestCommand method) {
        return method.execute(this);
    }

    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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy