Skip to content

Conversation

@yschimke
Copy link
Collaborator

@yschimke yschimke commented Oct 12, 2025

A take on Call.Decorator as an Interceptor #8800

Demonstrated with an Android Network Pinning Interceptor from #8376

class AndroidNetworkPinning : ClientForkingInterceptor<Network>() {
  override fun OkHttpClient.Builder.buildForKey(key: Network): OkHttpClient =
    dns(AndroidDns(key))
      .socketFactory(key.socketFactory)
      .apply {
        // Keep interceptors after this one in the new client
        interceptors.subList(interceptors.indexOf(this@AndroidNetworkPinning) + 1, interceptors.size).clear()
      }.build()

  override fun clientKey(request: Request): Network? = request.tag<Network>()
}

Introduces a ClientForkingInterceptor, which hides the nastiness of accessing the client

  override fun intercept(chain: Interceptor.Chain): Response {
    val client =
      (chain.call() as? RealCall)?.client ?: throw IOException("unable to access OkHttpClient")

    val key = clientKey(chain.request())
@yschimke
Copy link
Collaborator Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant