kluck
05/09/2019, 7:57 AMfun <DTO> IO<Response<DTO>>.withToken(accessToken: IO<AccessToken>): IO<Response<DTO>>
.
This is my dirty content for now, which doesn't work because it returns an IO<Any?>
and not an IO<Response<DTO>>
. This is due to my handleErrorWith {}
, which I can't get to return the correct type...
fun <DTO> IO<Response<DTO>>.withToken(accessToken: IO<AccessToken>): IO<Response<DTO>> =
fx {
val response = this@withToken.bind()
val unwrapped = response.unwrapBody(IO.applicativeError()).fix()
unwrapped.handleErrorWith { t ->
if (t is HttpException && t.code() == 401) {
accessToken.flatMap { this@withToken.withToken(accessToken) }
} else {
raiseError<Response<DTO>>(IllegalArgumentException()).fix()
}
}.fix().bind()
}
My goal is whenever I get a 401 response code, I'd like to retrieve a new accessToken before attempting again the call. I know this can loop indefinitely if I keep getting a 401 afterwards, but I need to address my current problem first 😉