Alexander Weickmann
11/22/2023, 5:28 PMAleksei Tirman [JB]
11/22/2023, 6:05 PMSaveBodyPlugin
to make the subsequent calls to bodyAsText()
return the actual body as a String.Jaap Beetstra
11/22/2023, 6:09 PM...
client.receivePipeline.intercept(HttpReceivePipeline.State) { response -> interceptReceive(response) }
...
private suspend fun PipelineContext<HttpResponse, Unit>.interceptReceive(response: HttpResponse) {
val logIdValue = response.request.attributes[logId]
val responseData = CachedHttpResponseData.create(response)
fileLog.responseFile(logIdValue).writeText(response.createResponseText(responseData.body))
proceedWith(CachedHttpResponse(response.call, responseData, response.coroutineContext))
}
Alexander Weickmann
11/23/2023, 12:53 PMJaap Beetstra
11/23/2023, 6:36 PMdata class CachedHttpResponseData(val headers: CachedHttpHeaders, val body: String) {
companion object {
suspend fun create(response: HttpResponse) =
CachedHttpResponseData(CachedHttpHeaders(response), response.bodyAsText())
}
}
data class CachedHttpHeaders(val statusCode: Int, val timestamp: Long, val headers: List<CachedHttpHeaderValue>) {
constructor(response: HttpResponse) : this(
response.status.value, response.responseTime.timestamp,
response.headers.entries().flatMap { (name, values) ->
values.map { CachedHttpHeaderValue(name, it) }
}
)
}
data class CachedHttpHeaderValue(val name: String, val value: String)
Jaap Beetstra
11/23/2023, 6:38 PMclass CachedHttpResponse(
override val call: HttpClientCall,
response: CachedHttpResponseData,
override val coroutineContext: CoroutineContext
) : HttpResponse() {
@InternalAPI
override val content: ByteReadChannel = ByteReadChannel(response.body)
override val headers: Headers = response.headers.headers.filterNot { it.name == "content-encoding" }.toHeaders()
override val requestTime: GMTDate = GMTDate(response.headers.timestamp)
override val responseTime: GMTDate = GMTDate()
override val status: HttpStatusCode = HttpStatusCode.fromValue(response.headers.statusCode)
override val version: HttpProtocolVersion = HttpProtocolVersion.HTTP_1_1
}
private fun List<CachedHttpHeaderValue>.toHeaders(): Headers = HeadersBuilder().apply {
forEach { header -> append(header.name, header.value) }
}.build()
Alexander Weickmann
11/28/2023, 8:42 AMAlexander Weickmann
12/05/2023, 9:31 AM