Interface HttpClient

  • All Known Implementing Classes:
    NewHttpClientImpl

    public interface HttpClient

    The central interface for the execution of HTTP requests.

    An object implementing this interface allows the execution of asynchronous HTTP requests. The interface supports lambda expressions for defining the requests to be executed and consuming the responses. Requests are defined by Consumer objects, which are invoked with a concrete RequestBuilder to generate a proper request representation. This allows for a declarative approach when specifying the properties of requests as shown in the following example:

    
     httpClient.execute(builder -> builder.method(RequestBuilder.Method.POST)
                                     .uri(endpointUri())
                                     .body(body -> body.string(CONTENT, CONTENT_TEXT_PLAIN)),
                                     ...);
     
     

    When the response arrives it is passed to a ResponseProcessor, which can evaluate the data and generate a corresponding result object. Again, a lambda expression can be provided to process the response.

    Implementations of this interface are expected to be thread-safe. They are usually stored centrally and shared by multiple components.

    • Method Detail

      • execute

        <T> CompletableFuture<T> execute​(Consumer<? super RequestBuilder> producer,
                                         ResponseProcessor<? extends T> processor)
        Executes an HTTP request asynchronously and returns a future object with the result. The method first invokes the Consumer to generate the request to be executed. This request is then sent to the server. If sending fails, e.g. because no connection could be established, the resulting future is failed with the corresponding exception. Otherwise, the ResponseProcessor is invoked with a representation of the response; the outcome of this object is then used to complete the result future.
        Type Parameters:
        T - the type of the result produced by the ResponseProcessor
        Parameters:
        producer - the object to produce the request
        processor - the object to process the response
        Returns:
        a future with the result of the execution