Hello, I'm trying to achieve some OAuth2 authoriza...
# arrow
k
Hello, I'm trying to achieve some OAuth2 authorization flow with IO. Here's what I'd like my signature to be:
fun <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...
Copy code
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 😉