Skip to content

OkHttp — API Reference

WiretapOkHttpInterceptor

class WiretapOkHttpInterceptor(
    configure: WiretapHttpConfig.() -> Unit = {},
) : Interceptor, KoinComponent

OkHttp Interceptor for HTTP request/response logging with mock/throttle rule support.

Constructor

Parameter Type Default Description
configure WiretapHttpConfig.() -> Unit {} Configuration builder lambda

Installation

OkHttpClient.Builder()
    .addInterceptor(WiretapOkHttpInterceptor {
        // WiretapHttpConfig properties — all optional
    })
    .build()

TLS Details Captured

Field Source
protocol response.protocol
remoteAddress chain.connection()?.route()?.socketAddress
tlsProtocol handshake.tlsVersion.javaName
cipherSuite handshake.cipherSuite.javaName
certificateCn Peer certificate subject CN
issuerCn Peer certificate issuer CN
certificateExpiry peerCert.notAfter

WiretapOkHttpWebSocketListener

class WiretapOkHttpWebSocketListener(
    private val delegate: WebSocketListener,
) : WebSocketListener(), KoinComponent

Wraps a consumer's WebSocketListener to log all WebSocket events.

Installation

// Extension function (recommended)
client.newWebSocket(request, myListener.wiretapped())

// Constructor
client.newWebSocket(request, WiretapOkHttpWebSocketListener(myListener))

Intercepted Callbacks

Callback Logged As
onOpen(webSocket, response) Connection opened. WebSocket wrapped for outgoing logging
onMessage(webSocket, text) Text message received
onMessage(webSocket, bytes) Binary message received
onClosing(webSocket, code, reason) Status → Closing
onClosed(webSocket, code, reason) Status → Closed
onFailure(webSocket, t, response) Status → Failed

wiretapped()

fun WebSocketListener.wiretapped(): WiretapOkHttpWebSocketListener

Extension function that wraps the receiver in a WiretapOkHttpWebSocketListener. Available in both the debug and noop modules.


WiretapHttpConfig

class WiretapHttpConfig {
    var enabled: Boolean = true
    var shouldLog: (url: String, method: String) -> Boolean = { _, _ -> true }
    var headerAction: (key: String) -> HeaderAction = { HeaderAction.Keep }
    var logRetention: LogRetention = LogRetention.Forever
    var maxContentLength: Int = MAX_CONTENT_LENGTH  // 500 * 1024
}

WiretapOkHttpEventSourceListener

class WiretapOkHttpEventSourceListener(
    private val delegate: EventSourceListener,
) : EventSourceListener(), KoinComponent

Wraps a consumer's EventSourceListener to log all SSE events.

Installation

// Extension function (recommended)
factory.newEventSource(request, myListener.wiretapped())

// Constructor
factory.newEventSource(request, WiretapOkHttpEventSourceListener(myListener))

Intercepted Callbacks

Callback Logged As
onOpen(eventSource, response) Connection opened with URL and request headers
onEvent(eventSource, id, type, data) Event logged with type, data, ID, and byte count
onClosed(eventSource) Status → Closed
onFailure(eventSource, t, response) Status → Failed

wiretapped() (SSE)

fun EventSourceListener.wiretapped(): WiretapOkHttpEventSourceListener

Extension function that wraps the receiver in a WiretapOkHttpEventSourceListener. Available in both the debug and noop modules.


No-op (wiretap-okhttp-noop)

Component Behavior
WiretapOkHttpInterceptor chain.proceed(chain.request())
WiretapOkHttpWebSocketListener Pure delegation to original listener
WiretapOkHttpEventSourceListener Pure delegation to original listener
wiretapModule Empty Koin module

Same constructor signatures — zero overhead, drop-in replacement.