Tuan Kiet
07/04/2019, 5:56 AMtseisel
07/04/2019, 6:57 AMHttpClient
with the OkHttp engine, you have the opportunity to configure OkHttp to use interceptors.Tuan Kiet
07/04/2019, 8:13 AMDias
07/04/2019, 8:57 AMtseisel
07/04/2019, 9:39 AMJsonFeature
, I noticed that it is possible to define interceptors with the following syntax :
// Transformation on the request
httpClient.requestPipeline.intercept(HttpRequestPipeline.Transform) { playload ->
// Do some things with the request, then send it to the next level
val result = transformMyRequest(payload)
proceedWith(result)
}
// Transformations on the response
httpClient.responsePipeline.intercept(HttpResponsePipeline.Transform) { (info, body) ->
// Do some things with the response, then send it to the next level
val result = transformMyResponse(info, body)
proceedWith(result)
}
More details on Ktor Pipelining : https://ktor.io/advanced/pipeline.html#interceptors-and-the-pipelinecontextTobi
07/04/2019, 2:22 PMprivate fun HttpClientConfig<*>.authenticatedRequestsEngine() {
engine {
this@authenticatedRequestsEngine.install(KEY_AUTH_INTERCEPTOR) {
this@authenticatedRequestsEngine.defaultRequest {
parameter(KEY_API_SECRET, BuildKonfig.API_KEY)
}
}
}
}
parameter
with an API_KEY
Installs an interceptor defined by block. The key parameter is used as a unique name, that also prevents installing duplicated interceptors.