Skip to content

Instantly share code, notes, and snippets.

@amatkivskiy
Last active November 25, 2018 20:26
Show Gist options
  • Save amatkivskiy/1ca48f71956ba7dabd48a0b382d32db5 to your computer and use it in GitHub Desktop.
Save amatkivskiy/1ca48f71956ba7dabd48a0b382d32db5 to your computer and use it in GitHub Desktop.
medium_TokenDataProvider
class TokenDataProvider {
fun getToken(): Observable<Result<String>> {
// Just return sample string
return Observable.just("token")
.toResult()
}
}
/**
* Just an handful extension function which wraps any value or error produced by [Observable] into [Result].
*/
fun <T : Any> Observable<T>.toResult(): Observable<Result<T>> {
return compose(ObservableToResultTransformer())
}
class ObservableToResultTransformer<T : Any> : ObservableTransformer<T, Result<T>> {
override fun apply(upstream: Observable<T>): ObservableSource<Result<T>> {
return upstream.map { Result.success(it) }
.onErrorReturn { Result.failure(it) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment