antoniomarin
06/24/2020, 11:31 AMCompletable
flow for login, and at some point in the middle I need to call AppAuth
library to performTokenRequest
. That library uses callbacks for results, so I’m curious how to adapt it to my architecture? Idea is that when callback is successful I resume with doOnSuccess
or doNext
to the next Completable
function.
private fun login(
authorisationResponse: AuthorisationResponse
): Completable {
return Completable.defer {
// do some validations..
if (authorisationResponse == null) {
return@defer Completable.error(...)
}
// app auth stuff
val tokenRequest = authorizationResponse.createTokenExchangeRequest()
val clientAuthentication ....
// performing token request, AppAuth library works with callbacks, so I'm
// curious how to "pause" my Rx flow until I get result from that callback
// and then I need to continue completable
return@defer ???
authorisationService.performTokenRequest(tokenRequest,clientAuthentication)
{ response, ex ->
// callbacks
if (response != null) {
//... I'm going to save token here
authStateManager.saveToken(response)
}
.doOnSuccess or .doNext (continueLoginFlow)
}
}
private fun continueLoginFlow(): Completable {
return authStateManager.getToken()
.map { ... }
.flatMap { ... }
.doOnSuccess { ... }
...
...
}
gildor
06/24/2020, 12:21 PMantoniomarin
06/24/2020, 1:19 PMreturn@defer Single.just(
authorizationService.performTokenRequest(tokenRequest, clientAuthentication) { response, ex ->
if (response != null) {
authStateManager.updateAfterTokenResponse(response, null)
}
}).doOnSuccess {
continueLoginFlow()
}.ignoreElement()
val single: Single<TokenResponse> = Single.create { emitter ->
authorizationService.performTokenRequest(
tokenRequest,
clientAuthentication
) { response, ex ->
if (response != null) {
emitter.onSuccess(response)
} else {
emitter.onError("Errror")
}
}
}
return@defer single.doOnSuccess...
gildor
06/24/2020, 2:43 PMemitter.onError("Errror")
Probably it should be
emitter.onError(ex)
fun AuthorizationService.tokenRequestSingle(
tokenRequest: ???,
clientAuthentication: ???
): Single<TokenResponse> = Single.create {
...
}
So you getting actual Rx version of this callback, it just becomes API of this serviceantoniomarin
06/24/2020, 2:50 PMgildor
06/24/2020, 2:52 PMantoniomarin
06/24/2020, 2:53 PMgildor
06/24/2020, 2:55 PMantoniomarin
06/24/2020, 3:15 PM