Module methanol

Interface ResponsePayload

  • All Superinterfaces:
    java.lang.AutoCloseable

    public interface ResponsePayload
    extends java.lang.AutoCloseable
    A response body that is yet to be handled into the desirable type. This can be useful when the response body differs in structure (and hence in high-level type) according to whether it has succeeded or failed. An implementation of ResponsePayload can be acquired as the response body through the basic decoder. See example below.

    To avoid resource leaks, use this type within a try-with-resources construct immediately after sending the response (regardless of whether the body is converted synchronously or asynchronously):

    
     var client =
         Methanol.newBuilder()
             .adapterCodec(AdapterCodec.newBuilder().basic().build()) // Install basic adapters.
             .build();
     var response = client.send(MutableRequest.GET("https://example.com"), ResponsePayload.class);
     try(var body = response.body()) {
       if (HttpStatus.isSuccessful(response) {
         System.out.println(body.to(SuccessType.class));
       } else if (HttpStatus.isClientError(response)) {
         System.out.println(body.to(ErrorType.class));
       } else {
         // Discard body.
       }
     }
     
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      void close()
      Makes sure the resources held by this payload are released.
      <T> T handleWith​(java.net.http.HttpResponse.BodyHandler<T> bodyHandler)
      Converts this payload using the given body handler.
      <T> java.util.concurrent.CompletableFuture<T> handleWithAsync​(java.net.http.HttpResponse.BodyHandler<T> bodyHandler)
      Asynchronously converts this payload using the given body handler.
      boolean is​(MediaType mediaType)
      Returns true if this payload has the given media type.
      default boolean isAnyOf​(MediaType... mediaTypes)
      Returns true if this payload has any of the given media types.
      <T> T to​(TypeRef<T> typeRef)
      Converts this payload into an object of (possibly generic) type T.
      default <T> T to​(java.lang.Class<T> type)
      Converts this payload into an object of type T.
      <T> java.util.concurrent.CompletableFuture<T> toAsync​(TypeRef<T> typeRef)
      Asynchronously converts this payload into an object of (possibly generic) type T.
      default <T> java.util.concurrent.CompletableFuture<T> toAsync​(java.lang.Class<T> type)
      Asynchronously converts this payload into an object of type T.
    • Method Detail

      • is

        boolean is​(MediaType mediaType)
        Returns true if this payload has the given media type.
      • isAnyOf

        default boolean isAnyOf​(MediaType... mediaTypes)
        Returns true if this payload has any of the given media types.
      • to

        default <T> T to​(java.lang.Class<T> type)
                  throws java.io.IOException,
                         java.lang.InterruptedException
        Converts this payload into an object of type T.
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • to

        <T> T to​(TypeRef<T> typeRef)
          throws java.io.IOException,
                 java.lang.InterruptedException
        Converts this payload into an object of (possibly generic) type T.
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • handleWith

        <T> T handleWith​(java.net.http.HttpResponse.BodyHandler<T> bodyHandler)
                  throws java.io.IOException,
                         java.lang.InterruptedException
        Converts this payload using the given body handler.
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • toAsync

        default <T> java.util.concurrent.CompletableFuture<T> toAsync​(java.lang.Class<T> type)
        Asynchronously converts this payload into an object of type T.
      • toAsync

        <T> java.util.concurrent.CompletableFuture<T> toAsync​(TypeRef<T> typeRef)
        Asynchronously converts this payload into an object of (possibly generic) type T.
      • handleWithAsync

        <T> java.util.concurrent.CompletableFuture<T> handleWithAsync​(java.net.http.HttpResponse.BodyHandler<T> bodyHandler)
        Asynchronously converts this payload using the given body handler.
      • close

        void close()
        Makes sure the resources held by this payload are released. If the payload has been consumed, this method does nothing.
        Specified by:
        close in interface java.lang.AutoCloseable