Last active
November 25, 2018 20:26
-
-
Save amatkivskiy/1ca48f71956ba7dabd48a0b382d32db5 to your computer and use it in GitHub Desktop.
medium_TokenDataProvider
This file contains 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
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