Skip to content

Commit

Permalink
Implement HttpRequestLogger client plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
IRus committed Jan 17, 2025
1 parent ccd2095 commit 1d04943
Showing 1 changed file with 3 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import io.ktor.client.statement.*
import io.ktor.util.*

class HttpRequestLogger(
config: Config,
private val config: Config,
) {
class Config {
// Configuration options if needed
var saveFunction: suspend (HttpResponse) -> Unit = {}
}

companion object Plugin : HttpClientPlugin<Config, HttpRequestLogger> {
Expand All @@ -23,12 +23,11 @@ class HttpRequestLogger(

override fun install(plugin: HttpRequestLogger, scope: HttpClient) {
scope.requestPipeline.intercept(HttpRequestPipeline.Before) {
println("Request: ${context.url}")
proceed()
}

scope.receivePipeline.intercept(HttpReceivePipeline.After) { response ->
println("Response: ${response.status.value}")
plugin.config.saveFunction(response)
proceedWith(response)
}
}
Expand Down

0 comments on commit 1d04943

Please sign in to comment.