Interface HttpAsyncResponse


  • public interface HttpAsyncResponse
    Async processing of the request with responses as CompletableFuture.
    • Method Detail

      • asVoid

        CompletableFuture<HttpResponse<Void>> asVoid()
        Process the response with check for 200 range status code returning as HttpResponse<Void>.

        Unlike asDiscarding() this request will read any response content as bytes with the view that the response content can be an error message that could be read via for example HttpException.bean(Class).

        Will throw an HttpException if the status code is in the error range allowing the caller to access the error message body via for example HttpException.bean(Class)

        This is intended to be used for POST, PUT, DELETE requests where the caller is only interested in the response body when an error occurs (status code not in 200 range).

        
        
           clientContext.request()
               .path("hello/world")
               .GET()
               .async().asVoid()
               .whenComplete((hres, throwable) -> {
        
                 if (throwable != null) {
        
                   // if throwable.getCause() is a HttpException for status code >= 300
                   HttpException httpException = (HttpException) throwable.getCause();
                   int status = httpException.statusCode();
        
                   // convert json error response body to a bean
                   ErrorResponse errorResponse = httpException.bean(ErrorResponse.class);
                   ...
                 } else {
                   int statusCode = hres.statusCode();
                   ...
                 }
               });
        
         
      • asDiscarding

        CompletableFuture<HttpResponse<Void>> asDiscarding()
        Process discarding response body as HttpResponse<Void>.

        Unlike asVoid() this will discard any response body including any error response body. We should instead use asVoid() if we might get an error response body that we want to read via for example HttpException.bean(Class).

        
        
           clientContext.request()
               .path("hello/world")
               .GET()
               .async().asDiscarding()
               .whenComplete((hres, throwable) -> {
        
                 if (throwable != null) {
                   ...
                 } else {
                   int statusCode = hres.statusCode();
                   ...
                 }
               });
        
         
        Returns:
        The CompletableFuture of the response
      • asString

        CompletableFuture<HttpResponse<String>> asString()
        Process as String response body HttpResponse<String>.
        
        
           clientContext.request()
               .path("hello/world")
               .GET()
               .async().asString()
               .whenComplete((hres, throwable) -> {
        
                 if (throwable != null) {
                   ...
                 } else {
                   int statusCode = hres.statusCode();
                   String body = hres.body();
                   ...
                 }
               });
        
         
        Returns:
        The CompletableFuture of the response
      • asByteArray

        CompletableFuture<HttpResponse<byte[]>> asByteArray()
        Process as response HttpResponse<byte[]>.
        Returns:
        The CompletableFuture of the response
      • withHandler

        <E> CompletableFuture<HttpResponse<E>> withHandler​(HttpResponse.BodyHandler<E> bodyHandler)
        Process with any given HttpResponse.BodyHandler.

        Example: line subscriber

        Subscribe line by line to the response.

        {@code
        
            CompletableFuture> future = clientContext.request()
               .path("hello/lineStream")
               .GET().async()
               .withHandler(HttpResponse.BodyHandlers.fromLineSubscriber(new Flow.Subscriber<>() {
        Parameters:
        bodyHandler - The body handler to use to process the response
        Returns:
        The CompletableFuture of the response
      • bean

        <E> CompletableFuture<E> bean​(Class<E> type)
        Process expecting a bean response body (typically from json content).

        If the HTTP statusCode is not in the 2XX range a HttpException is throw which contains the HttpResponse. This is the cause in the CompletionException.

        
        
            clientContext.request()
               ...
               .POST().async()
               .bean(HelloDto.class)
               .whenComplete((helloDto, throwable) -> {
        
                 if (throwable != null) {
                   HttpException httpException = (HttpException) throwable.getCause();
                   int statusCode = httpException.statusCode();
        
                   // maybe convert json error response body to a bean (using Jackson/Gson)
                   MyErrorBean errorResponse = httpException.bean(MyErrorBean.class);
                   ..
        
                 } else {
                   // process helloDto
                   ...
                 }
               });
         
        Parameters:
        type - The bean type to convert the content to
        Returns:
        The CompletableFuture of the response
      • list

        <E> CompletableFuture<List<E>> list​(Class<E> type)
        Process expecting a list of beans response body (typically from json content).

        If the HTTP statusCode is not in the 2XX range a HttpException is throw which contains the HttpResponse. This is the cause in the CompletionException.

        
        
            clientContext.request()
               ...
               .GET().async()
               .list(HelloDto.class)
               .whenComplete((helloDtos, throwable) -> {
        
                 if (throwable != null) {
                   HttpException httpException = (HttpException) throwable.getCause();
                   int statusCode = httpException.statusCode();
                   ...
        
                 } else {
                   // process list of helloDto
                   ...
                 }
               });
         
        Parameters:
        type - The bean type to convert the content to
        Returns:
        The CompletableFuture of the response
      • stream

        <E> CompletableFuture<Stream<E>> stream​(Class<E> type)
        Process response as a stream of beans (x-json-stream).

        Typically the response is expected to be application/x-json-stream newline delimited json payload.

        Note that for this stream request the response content is not deemed 'loggable' by avaje-http-client. This is because the entire response may not be available at the time of the callback. As such RequestLogger will not include response content in logging stream request/response.

        If the HTTP statusCode is not in the 2XX range a HttpException is throw which contains the HttpResponse. This is the cause in the CompletionException.

        
        
           CompletableFuture<Stream<Customer>> future = clientContext.request()
               .path("customers/stream")
               .GET().async()
               .stream(Customer.class);
        
           future.whenComplete((stream, throwable) -> {
               // if throwable != null ... handle error
        
               // else process Stream<Customer> ...
               try (stream) {
                 stream.forEach(customer -> {
                   ...
                 });
               }
             });
        
         
        Parameters:
        type - The bean type to convert the content to
        Returns:
        The CompletableFuture of the response