All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.truelayer.java.http.TrueLayerApiAdapterFactory Maven / Gradle / Ivy

There is a newer version: 16.0.0
Show newest version
package com.truelayer.java.http;

import com.truelayer.java.http.entities.ApiResponse;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.concurrent.CompletableFuture;
import retrofit2.CallAdapter;
import retrofit2.Retrofit;

/**
 * This class is extends Retrofit's Java8CallAdapterFactory
 * to support TrueLayer ApiResponse wrapper object.
 *
 * @see ApiResponse
 */
final class TrueLayerApiAdapterFactory extends CallAdapter.Factory {
    @Override
    public CallAdapter get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
        if (getRawType(returnType) != CompletableFuture.class) {
            return null;
        }
        if (!(returnType instanceof ParameterizedType)) {
            throw new IllegalStateException("CompletableFuture return type must be parameterized"
                    + " as CompletableFuture or CompletableFuture");
        }
        Type innerType = getParameterUpperBound(0, (ParameterizedType) returnType);

        // Generic type is Response. Extract T and create the Response version of the adapter.
        if (!(innerType instanceof ParameterizedType)) {
            throw new IllegalStateException(
                    "Response must be parameterized" + " as Response or Response");
        }
        Type responseType = getParameterUpperBound(0, (ParameterizedType) innerType);
        return new TrueLayerResponseCallAdapter<>(responseType);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy