Skip to content

Instantly share code, notes, and snippets.

@lukaszkalnik
Created June 18, 2020 16:14
Show Gist options
  • Save lukaszkalnik/7d39c4020e9e0e11949015991ea5d748 to your computer and use it in GitHub Desktop.
Save lukaszkalnik/7d39c4020e9e0e11949015991ea5d748 to your computer and use it in GitHub Desktop.
internal class EitherCallAdapterFactory : CallAdapter.Factory() {
override fun get(
returnType: Type,
annotations: Array<Annotation>,
retrofit: Retrofit
): CallAdapter<*, *>? {
if (getRawType(returnType) != Call::class.java) return null
check(returnType is ParameterizedType) { "Return type must be a parameterized type." }
val responseType = getParameterUpperBound(0, returnType)
if (getRawType(responseType) != Either::class.java) return null
check(responseType is ParameterizedType) { "Response type must be a parameterized type." }
val leftType = getParameterUpperBound(0, responseType)
if (getRawType(leftType) != ApiError::class.java) return null
val rightType = getParameterUpperBound(1, responseType)
return EitherCallAdapter<Any>(rightType)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment