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

com.deque.networking.models.ResultCallAdapterFactory.kt Maven / Gradle / Ivy

There is a newer version: 5.5.2
Show newest version
package com.deque.networking.models

import retrofit2.Call
import retrofit2.CallAdapter
import retrofit2.Retrofit
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type

@Suppress("UNCHECKED_CAST")
class ResultCallAdapterFactory : CallAdapter.Factory() {
    override fun get(
        returnType: Type,
        annotations: Array,
        retrofit: Retrofit
    ): CallAdapter<*, *>? {
        if (getRawType(returnType) != Call::class.java || returnType !is ParameterizedType) {
            return null
        }
        val upperBound = getParameterUpperBound(0, returnType)

        return if (upperBound is ParameterizedType && upperBound.rawType == Result::class.java) {
            object : CallAdapter>> {
                override fun responseType(): Type = getParameterUpperBound(0, upperBound)

                override fun adapt(call: Call): Call> =
                    ResultCall(call) as Call>
            }
        } else {
            null
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy