https://kotlinlang.org logo
#ktor
Title
# ktor
v

Vivek Modi

04/19/2022, 3:08 PM
Hey guys, I am reading the doc and I find
HttpResponseValidator
is used for api status, correct me If I am wrong. 1. I need to call api, if api returns 401 status, I need to call refershToken api. After getting new accessToken from refreshToken api, I need to send this to api call. 2. If refreshToken is giving 401 then I need to infrom my application to logout. How can I achieve through this Ktor?
androidMain
Copy code
actual fun httpClient(config: HttpClientConfig<*>.() -> Unit) = HttpClient(OkHttp) {
    config(this)
    install(ContentNegotiation) {
        json(Json {
            prettyPrint = true
            ignoreUnknownKeys = true
            explicitNulls = false
        })
    }
    engine {
        config {
            retryOnConnectionFailure(true)
            connectTimeout(30, TimeUnit.SECONDS)
            readTimeout(40, TimeUnit.SECONDS)
        }
    }
    defaultRequest {
        header("Client-Version", Platform().versionCode)
    }
    HttpResponseValidator {
        validateResponse { response ->
            when (response.status.value) {
                401 -> {

                }
            }
        }
    }
    install(Auth) {
        bearer {
            loadTokens {
                BearerTokens(tokenProvider.accessToken, "")
            }
        }
    }
}
commonMain
Copy code
expect fun httpClient(config: HttpClientConfig<*>.() -> Unit = {}): HttpClient