avaje/avaje-http-client

ENH: Add HttpCall that allows client to choose async or sync processing of request

Closed this issue · 0 comments

Along the same lines as Retrofit Call, this allows the client code to choose if they wish to execute asynchronously (CompletableFuture) or synchronously.

package io.avaje.http.client;

import java.util.concurrent.CompletableFuture;

/**
 * Allows for executing the request asynchronously or synchronously.
 *
 * @param <E> The type of response
 */
public interface HttpCall<E> {

  /**
   * Execute the request returning the result.
   */
  E execute();

  /**
   * Execute the request asynchronously.
   */
  CompletableFuture<E> async();
}

Example

HttpCall<Stream<Customer>> call = clientContext.request()
      .path("customers/stream")
      .GET()
      .call()


// call synchronously
Stream<Customer> customers = call.execute();

// call asynchronously
call
  .async()
  .whenComplete((stream, throwable) -> {
      ...
    });