-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix response body consumed on error, update dependencies
- Loading branch information
1 parent
b6d638c
commit 82bdfb7
Showing
9 changed files
with
107 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...wesome-logging/src/main/kotlin/com/linkedplanet/ktor/client/logging/CachedHttpResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.linkedplanet.ktor.client.logging | ||
|
||
import io.ktor.client.call.* | ||
import io.ktor.client.statement.* | ||
import io.ktor.http.* | ||
import io.ktor.util.* | ||
import io.ktor.util.date.* | ||
import io.ktor.utils.io.* | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
class 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() | ||
|
||
data class CachedHttpResponseData(val headers: CachedHttpHeaders, val body: String) { | ||
companion object { | ||
suspend fun create(response: HttpResponse): CachedHttpResponseData { | ||
val body = try { | ||
response.bodyAsText() | ||
} catch (e: Exception) { | ||
"" | ||
} | ||
return CachedHttpResponseData(CachedHttpHeaders(response), body) | ||
} | ||
} | ||
} | ||
|
||
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) |
25 changes: 25 additions & 0 deletions
25
ktor-client-awesome-logging/src/main/kotlin/com/linkedplanet/ktor/client/logging/Mdc.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.linkedplanet.ktor.client.logging | ||
|
||
import kotlinx.coroutines.slf4j.MDCContext | ||
import kotlinx.coroutines.withContext | ||
import org.slf4j.MDC | ||
|
||
suspend fun <T> withMdc(vararg infos: Pair<String, Any?>, func: suspend () -> T): T = | ||
withMdc(MDC.getCopyOfContextMap() ?: emptyMap(), infos.toMap(), func) | ||
|
||
private suspend fun <T> withMdc(oldState: Map<String, String>, newState: Map<String, Any?>, func: suspend () -> T): T { | ||
newState.entries.forEach { (key, value) -> | ||
MDC.put(key, value.toString()) | ||
} | ||
return try { | ||
withContext(MDCContext()) { | ||
func() | ||
} | ||
} finally { | ||
// the MDC context does not reliably capture the old context before switching into the new context | ||
// this leads to values getting lost when restoring the old context, so we take care of it ourselves | ||
oldState.entries.forEach { (key, value) -> | ||
MDC.put(key, value) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters