Last active
January 9, 2019 18:58
-
-
Save naturalwarren/557f86f5f7859d80a221f906320960f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A [CallAdapter.Factory] which allows [CoinbaseResponse] objects to be | |
* returned from RxJava streams. For the type | |
* [Observable<CoinbaseResponse<SuccessBody, ErrorBody>>], the following | |
* semantics are provided: | |
* | |
* 1. 2xx responses call onNext with the deserialized body set as | |
* [CoinbaseResponse.body] | |
* 2. non-2xx responses call onNext with the deserialized error body set as | |
* [CoinbaseResponse.errorBody] | |
* 3. Calls that fail due to network issues call onNext with network errors set as | |
* [CoinbaseResponse.networkError] | |
*/ | |
class CoinbaseRxJava2CallAdapterFactory : CallAdapter.Factory() { | |
override fun get( | |
returnType: Type, | |
annotations: Array<Annotation>, | |
retrofit: Retrofit | |
): CallAdapter<*, *>? { | |
val rawType = getRawType(returnType) // ie Single | |
// Check if we support this type, must be Rx stream of CoinbaseResponse. | |
val supportedType = SupportedType.create(rawType) ?: return null | |
val observableEmissionType = getParameterUpperBound( // ie CoinbaseResponse | |
0, | |
returnType as ParameterizedType | |
) | |
val successBodyType = getParameterUpperBound( // ie AccessToken | |
0, observableEmissionType as ParameterizedType | |
) | |
val delegateType = Types.newParameterizedType( // ie Single<Response<AccessToken>> | |
getRawType(returnType), | |
Types.newParameterizedType(Response::class.java, successBodyType) | |
) | |
val delegateAdapter = retrofit.nextCallAdapter( | |
this, | |
delegateType, | |
annotations | |
) | |
val errorBodyType = getParameterUpperBound( // ie OAuthError | |
1, | |
observableEmissionType) | |
val errorBodyConverter = retrofit.nextResponseBodyConverter<Any?>( | |
null, | |
errorBodyType, | |
annotations | |
) | |
return CoinbaseRxJava2CallAdapter( | |
successBodyType, | |
delegateAdapter as CallAdapter<Any, Any>, | |
errorBodyConverter, | |
supportedType as SupportedType<Any, Any> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment