Skip to content

Commit

Permalink
Allow debugging HTTP calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitjes committed Apr 26, 2024
1 parent 86d0a97 commit 8b8651b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
8 changes: 6 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ version.ref = "ktor"
module = "io.ktor:ktor-client-json"
version.ref = "ktor"

[libraries.ktor-client-logging]
module = "io.ktor:ktor-client-logging"
version.ref = "ktor"

[libraries.ktor-client-mock]
module = "io.ktor:ktor-client-mock"
version.ref = "ktor"
Expand Down Expand Up @@ -96,7 +100,7 @@ module = "org.jetbrains.kotlinx:kotlinx-io-core"
version.ref = "kotlinx-io"

[bundles]
ktor = ["ktor-client-core", "ktor-client-serialization", "ktor-client-json", "ktor-client-content-negotiation", "ktor-serialization-kotlinx-json", "ktor-network"]
ktor = ["ktor-client-core", "ktor-client-serialization", "ktor-client-json", "ktor-client-logging", "ktor-client-content-negotiation", "ktor-serialization-kotlinx-json", "ktor-network"]
ktx = ["ktx-coroutines-core", "ktx-serialization-core", "ktx-serialization-json"]

[plugins]
Expand All @@ -106,4 +110,4 @@ kotlinter = { id = "org.jmailen.kotlinter", version.ref = "plugin-kotlinter" }
publishOnCentral = { id = "org.danilopianini.publish-on-central", version.ref = "plugin-publishOnCentral" }
binaryCompatibilityValidator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "plugin-binaryCompatibilityValidator" }
kover = { id = "org.jetbrains.kotlinx.kover", version.ref = "plugin-kover" }
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "plugin-detekt" }
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "plugin-detekt" }
23 changes: 21 additions & 2 deletions src/commonMain/kotlin/me/devnatan/yoki/YokiConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ internal val DefaultYokiConfig = YokiConfig.builder().forCurrentPlatform().build
* @param apiVersion The version of the Docker API that will be used during communication.
* See more: [Versioned API and SDK](https://docs.docker.com/engine/api/#versioned-api-and-sdk).
*/
public class YokiConfig(public val socketPath: String, public val apiVersion: String) {
public class YokiConfig(
public val socketPath: String,
public val apiVersion: String,
public val debugHttpCalls: Boolean,
) {

init {
check(socketPath.isNotBlank()) { "Socket path must be provided and cannot be blank" }
Expand Down Expand Up @@ -49,6 +53,11 @@ public class YokiConfigBuilder {
prefix = null,
)

/**
* Whether to debug the HTTP calls to the Docker daemon.
*/
private var debugHttpCalls: Boolean = false

/**
* Sets the Docker socket path.
*
Expand All @@ -69,6 +78,16 @@ public class YokiConfigBuilder {
return this
}

/**
* Sets the debug logging of HTTP calls.
*
* @param debugHttpCalls whether to log the HTTP calls
*/
public fun debugHttpCalls(debugHttpCalls: Boolean = true): YokiConfigBuilder {
this.debugHttpCalls = debugHttpCalls
return this
}

/**
* Configures to use a Unix socket defaults common to the standard Docker configuration.
*
Expand Down Expand Up @@ -116,7 +135,7 @@ public class YokiConfigBuilder {
* Builds this class to a [YokiConfig].
*/
public fun build(): YokiConfig {
return YokiConfig(socketPath, apiVersion)
return YokiConfig(socketPath, apiVersion, debugHttpCalls)
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/commonMain/kotlin/me/devnatan/yoki/io/Http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.ktor.client.plugins.ResponseException
import io.ktor.client.plugins.UserAgent
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.defaultRequest
import io.ktor.client.plugins.logging.*
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.http.URLBuilder
Expand Down Expand Up @@ -37,6 +38,13 @@ internal fun createHttpClient(client: Yoki): HttpClient {
)
}

if (client.config.debugHttpCalls) {
install(Logging) {
logger = Logger.SIMPLE
level = LogLevel.ALL
}
}

install(UserAgent) { agent = "Yoki" }
configureHttpClient(client)

Expand Down
10 changes: 7 additions & 3 deletions src/commonTest/kotlin/me/devnatan/yoki/resource/ResourceIT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package me.devnatan.yoki.resource
import me.devnatan.yoki.Yoki
import me.devnatan.yoki.createTestYoki

open class ResourceIT {
open class ResourceIT(
private val debugHttpCalls: Boolean = false,
) {

companion object {
val testClient: Yoki by lazy(::createTestYoki)
val testClient: Yoki by lazy {
createTestYoki {
debugHttpCalls(this@ResourceIT.debugHttpCalls)
}
}
}

0 comments on commit 8b8651b

Please sign in to comment.