Interface CallX<T>

  • Type Parameters:
    T - Successful response body type.
    All Superinterfaces:
    retrofit2.Call<T>, java.lang.Cloneable

    public interface CallX<T>
    extends retrofit2.Call<T>
    An extension of Call that uses Java lambdas for the callbacks.

    Callbacks are created using Consumer, BiConsumer, BiFunction, which are usable with Java 8+ lambdas and also have interoperability with Kotlin.

    JVM

    By convention, atomic callbacks are those that would shut down the client when the call is done. This is useful for JVM only platforms, because OkHttp by default uses a non-daemon thread, this will prevent the JVM from exiting until they time out. see here:

    Dispatcher.executorService()

    Android

    However, for Android, default executor should be MainThreadExecutor, which is tied to the UI thread.

    android.os.Handler

    android.os.Looper

    So in conclusion, for Android it's better to use enqueue callbacks.

    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      default void async​(boolean isShutdownNeeded, java.util.function.BiConsumer<@Nullable retrofit2.Response<T>,​@Nullable java.lang.Throwable> callback)
      Calls async(BiFunction) but instead of determining whether to shut down the client or not based on the return value of the callback function, it will be determined by the given boolean value.
      void async​(boolean isShutdownNeeded, java.util.function.Consumer<@Nullable retrofit2.Response<T>> onSuccess, java.util.function.Consumer<java.lang.Throwable> onFailure)
      General purpose async call method with two callbacks
      void async​(java.util.function.BiFunction<@Nullable retrofit2.Response<T>,​@Nullable java.lang.Throwable,​@NotNull java.lang.Boolean> callback)
      General purpose async call method.
      default void asyncBody​(boolean isShutdownNeeded, @NotNull java.util.function.Consumer<@Nullable T> onSuccess, @NotNull java.util.function.Consumer<@Nullable java.lang.Throwable> onFailure)
      Calls async(boolean, Consumer, Consumer) and returns the response body.
      default void asyncBody​(boolean isShutdownNeeded, java.util.function.BiConsumer<@Nullable T,​@Nullable java.lang.Throwable> callback)
      Calls async(boolean, BiConsumer) but instead of determining whether to shut down the client or not based on the return value of the callback function, it will be determined by the given boolean value.
      default void atomicAsync​(java.util.function.BiConsumer<@Nullable retrofit2.Response<T>,​@Nullable java.lang.Throwable> callback)
      Calls async(BiFunction) and automatically shuts down the client.
      default void atomicAsync​(java.util.function.Consumer<@Nullable retrofit2.Response<T>> onSuccess, java.util.function.Consumer<java.lang.Throwable> onFailure)
      Calls async(boolean, Consumer, Consumer) and automatically shuts down the client.
      default void atomicAsyncBody​(java.util.function.BiConsumer<@Nullable T,​@Nullable java.lang.Throwable> callback)
      Calls asyncBody(boolean, BiConsumer) with true parameter and returns the response body.
      void enqueue​(@NotNull java.util.function.BiConsumer<retrofit2.Call<T>,​retrofit2.Response<T>> onResponse, @NotNull java.util.function.BiConsumer<retrofit2.Call<T>,​java.lang.Throwable> onFailure)
      A lambda wrapper around Call.enqueue(Callback)
      default void enqueueAsync​(java.util.function.BiConsumer<@Nullable retrofit2.Response<T>,​@Nullable java.lang.Throwable> callback)
      Calls async(BiFunction) and doesn't care whether to shut down the client or not.
      default void enqueueAsync​(java.util.function.Consumer<@Nullable retrofit2.Response<T>> onSuccess, java.util.function.Consumer<java.lang.Throwable> onFailure)
      Calls async(boolean, Consumer, Consumer) and doesn't care whether to shut down the client or not.
      default void enqueueAsyncBody​(@NotNull java.util.function.Consumer<@NotNull T> onSuccess, @NotNull java.util.function.Consumer<@Nullable java.lang.Throwable> onFailure, @NotNull java.util.function.Supplier<@NotNull T> defaultValueSupplier)
      Calls enqueueAsync(Consumer, Consumer) and definitely returns a Non-Null response body, Actually if response body is null then default value provided by defaultValueSupplier is returned.
      default void enqueueAsyncBody​(@NotNull java.util.function.Consumer<@Nullable T> onSuccess, @NotNull java.util.function.Consumer<@Nullable java.lang.Throwable> onFailure)
      Calls asyncBody(boolean, Consumer, Consumer) with false parameter so it doesn't care about whether to shut down client or not
      default void enqueueAsyncBody​(java.util.function.BiConsumer<@Nullable T,​@Nullable java.lang.Throwable> callback)
      Calls asyncBody(boolean, BiConsumer) with false parameter and returns the response body.
      • Methods inherited from interface retrofit2.Call

        cancel, clone, enqueue, execute, isCanceled, isExecuted, request, timeout
    • Method Detail

      • enqueue

        void enqueue​(@NotNull
                     @NotNull java.util.function.BiConsumer<retrofit2.Call<T>,​retrofit2.Response<T>> onResponse,
                     @NotNull
                     @NotNull java.util.function.BiConsumer<retrofit2.Call<T>,​java.lang.Throwable> onFailure)
        A lambda wrapper around Call.enqueue(Callback)
        Parameters:
        onResponse - Callback.onResponse(Call, Response)
        onFailure - Callback.onFailure(Call, Throwable)
      • async

        void async​(java.util.function.BiFunction<@Nullable retrofit2.Response<T>,​@Nullable java.lang.Throwable,​@NotNull java.lang.Boolean> callback)
        General purpose async call method.

        CallX is a type parameter that is used to show the type of the response body.

        Parameters:
        callback - Represents the callback function which will be called when the request is finished or when an error occurs. The callback function will be called with two parameters:
        • The first parameter is the generic retrofit response object. Response
        • The second parameter is the throwable object. Throwable
        The callback function will/shall return a boolean value which indicates whether the shutdown of the client is needed or not:
        • If the return value is true, the client will be shutdown.
        • If the return value is false, the client will not be shutdown.
        • If the return value is null, the client will not be shutdown.
        The default value is false.

        More info about why auto shutdown is needed:

        By default, OkHttp uses non-daemon thread, this will prevent the JVM from exiting until they time out. however, if you are using OkHttp in a non-daemon thread, like Android, this is unnecessary.

      • async

        default void async​(boolean isShutdownNeeded,
                           java.util.function.BiConsumer<@Nullable retrofit2.Response<T>,​@Nullable java.lang.Throwable> callback)
        Calls async(BiFunction) but instead of determining whether to shut down the client or not based on the return value of the callback function, it will be determined by the given boolean value.
        Parameters:
        isShutdownNeeded - The boolean value which determines whether to shut down the client or not.
        callback - The callback function.
      • atomicAsync

        default void atomicAsync​(java.util.function.BiConsumer<@Nullable retrofit2.Response<T>,​@Nullable java.lang.Throwable> callback)
        Calls async(BiFunction) and automatically shuts down the client.
        Parameters:
        callback - The callback function.
      • enqueueAsync

        default void enqueueAsync​(java.util.function.BiConsumer<@Nullable retrofit2.Response<T>,​@Nullable java.lang.Throwable> callback)
        Calls async(BiFunction) and doesn't care whether to shut down the client or not.
        Parameters:
        callback - The callback function.
      • asyncBody

        default void asyncBody​(boolean isShutdownNeeded,
                               java.util.function.BiConsumer<@Nullable T,​@Nullable java.lang.Throwable> callback)
        Calls async(boolean, BiConsumer) but instead of determining whether to shut down the client or not based on the return value of the callback function, it will be determined by the given boolean value.

        However, this method only returns the response body.

        This method is useful when you want to get the response body without caring about the response status code or headers.

        Parameters:
        isShutdownNeeded - The boolean value which determines whether to shut down the client or not.
        callback - The callback function.
      • enqueueAsyncBody

        default void enqueueAsyncBody​(java.util.function.BiConsumer<@Nullable T,​@Nullable java.lang.Throwable> callback)
        Calls asyncBody(boolean, BiConsumer) with false parameter and returns the response body.

        This method is useful when you want to get the response body without caring about the response status code.

        This method don't care whether to shut down the client or not.

        Parameters:
        callback - The callback function.
      • atomicAsyncBody

        default void atomicAsyncBody​(java.util.function.BiConsumer<@Nullable T,​@Nullable java.lang.Throwable> callback)
        Calls asyncBody(boolean, BiConsumer) with true parameter and returns the response body.

        This method is useful when you want to get the response body without caring about the response status code.

        This method will shut down the client after the callback function is called.

        Parameters:
        callback - The callback function.
      • async

        void async​(boolean isShutdownNeeded,
                   java.util.function.Consumer<@Nullable retrofit2.Response<T>> onSuccess,
                   java.util.function.Consumer<java.lang.Throwable> onFailure)
        General purpose async call method with two callbacks
        Parameters:
        isShutdownNeeded - The boolean value which determines whether to shut down the client or not.
        onSuccess - The callback function which is called when an HTTP response is received and is not canceled.
        onFailure - The callback function which is called when a network exception occurred talking to the server or when an unexpected exception occurred creating the request or processing the response.
      • atomicAsync

        default void atomicAsync​(java.util.function.Consumer<@Nullable retrofit2.Response<T>> onSuccess,
                                 java.util.function.Consumer<java.lang.Throwable> onFailure)
        Calls async(boolean, Consumer, Consumer) and automatically shuts down the client.
        Parameters:
        onSuccess - The callback function which is called when an HTTP response is received and is not canceled.
        onFailure - The callback function which is called when a network exception occurred talking to the server or when an unexpected exception occurred creating the request or processing the response.
      • enqueueAsync

        default void enqueueAsync​(java.util.function.Consumer<@Nullable retrofit2.Response<T>> onSuccess,
                                  java.util.function.Consumer<java.lang.Throwable> onFailure)
        Calls async(boolean, Consumer, Consumer) and doesn't care whether to shut down the client or not.
        Parameters:
        onSuccess - The callback function which is called when an HTTP response is received and is not canceled.
        onFailure - The callback function which is called when a network exception occurred talking to the server or
      • asyncBody

        default void asyncBody​(boolean isShutdownNeeded,
                               @NotNull
                               @NotNull java.util.function.Consumer<@Nullable T> onSuccess,
                               @NotNull
                               @NotNull java.util.function.Consumer<@Nullable java.lang.Throwable> onFailure)
        Calls async(boolean, Consumer, Consumer) and returns the response body. *

        * This method is useful when you want to get the response body without caring about the response status code or headers.

        Parameters:
        isShutdownNeeded - The boolean value which determines whether to shut down the client or not.
        onSuccess - The callback function which is called when an HTTP response is received and is not canceled.
        onFailure - The callback function which is called when a network exception occurred talking to the server or when an unexpected exception occurred creating the request or processing the response.
      • enqueueAsyncBody

        default void enqueueAsyncBody​(@NotNull
                                      @NotNull java.util.function.Consumer<@Nullable T> onSuccess,
                                      @NotNull
                                      @NotNull java.util.function.Consumer<@Nullable java.lang.Throwable> onFailure)
        Calls asyncBody(boolean, Consumer, Consumer) with false parameter so it doesn't care about whether to shut down client or not
        Parameters:
        onSuccess - The callback function which is called when an HTTP response is received and is not canceled.
        onFailure - The callback function which is called when a network exception occurred talking to the server or
      • enqueueAsyncBody

        default void enqueueAsyncBody​(@NotNull
                                      @NotNull java.util.function.Consumer<@NotNull T> onSuccess,
                                      @NotNull
                                      @NotNull java.util.function.Consumer<@Nullable java.lang.Throwable> onFailure,
                                      @NotNull
                                      @NotNull java.util.function.Supplier<@NotNull T> defaultValueSupplier)
        Calls enqueueAsync(Consumer, Consumer) and definitely returns a Non-Null response body, Actually if response body is null then default value provided by defaultValueSupplier is returned.
        Parameters:
        onSuccess - The callback function which is called when an HTTP response is received and is not canceled.
        onFailure - The callback function which is called when a network exception occurred talking to the server or
        defaultValueSupplier - a supplier for supplying a default value in case response body is null