question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support returning OkHttpClient subclasses from OkHttpClient.Builder#build()

See original GitHub issue

Hello,

Here is a simplified view of my problem:

class MyOkHttpClient : OkHttpClient() {
  // override something here
}

val client = MyOkHttpClient()
val newClient = client.newBuilder().build()

Given the above code, I would like newClient to be an instance of MyOkHttpClient.

My use case is: I’m trying to inspect websocket network traffic on Android. This isn’t supported by Android Profiler as far as I can tell, but it seems it should be possible with Stetho with a bit of work: http://zenandroid.io/monitoring-websockets-with-stetho/

However, I’m using OkHttp via Ktor. I can provide a preconfigued instance of OkHttpClient to Ktor, but the Ktor OkHttpEngine calls newBuilder().build() and thus ends up using OkHttpClient, not MyOkHttpClient:

https://github.com/ktorio/ktor/blob/21bb0a7a05f135a5ffb312ef83149fbc25899b86/ktor-client/ktor-client-okhttp/jvm/src/io/ktor/client/engine/okhttp/OkHttpEngine.kt#L133-L144

It would seem the purpose of declaring OkHttpClient to be open is obviated somewhat by the fact that Builder#build() always returns OkHttpClient. Allowing users to override Builder#build() or pass a factory for creating instances of OkHttpClient would address this.

How do you feel about this?

Thanks

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
chrisfillmorecommented, Dec 30, 2020

Just to follow up, in case anyone has a similar use case, my pull request with Ktor was merged and it is now possible to pass a WebSocket.Factory to its configuration: https://github.com/ktorio/ktor/pull/2035

0reactions
chrisfillmorecommented, Aug 28, 2020

Calling the Stetho method NetworkEventReporter#webSocketFrameSent depends on overriding OkHttp method WebSocket#send(). For example:

class StethoWebSocket(
  private val wrappedSocket: WebSocket,
  private val requestId: String
 ) : WebSocket by wrappedSocket {
  private val reporter = NetworkEventReporterImpl.get()

  override fun send(text: String): Boolean {
    reporter.webSocketFrameSent(SimpleTextInspectorWebSocketFrame(requestId, text))
    return wrappedSocket.send(text)
  }

  override fun send(bytes: ByteString): Boolean {
    reporter.webSocketFrameSent(SimpleBinaryInspectorWebSocketFrame(requestId, bytes.toByteArray()))
    return wrappedSocket.send(bytes)
  }
}

Other reporting methods (webSocketFrameReceived, etc) depend on OkHttp WebSocketListener.

I have filed an issue with Ktor: https://youtrack.jetbrains.com/issue/KTOR-951

Read more comments on GitHub >

github_iconTop Results From Across the Web

OkHttpClient.Builder.<init> java code examples - Tabnine
Builder builder = new OkHttpClient.Builder(); builder.sslSocketFactory(sslSocketFactory); builder.hostnameVerifier(new HostnameVerifier() {.
Read more >
Retrofit 2/OkHttp: Cancel all running requests - Stack Overflow
The dispatcher is returned from OkHttpClient.dispatcher() . ... find) is to create a subclass of OkHttpClient and use that with Retrofit.
Read more >
Recipes - OkHttp
A collection of common/useful code examples for Kotlin and Java.
Read more >
How to handle RESTful web Services using Retrofit, OkHttp ...
For making HTTP requests Retrofit uses the OkHttp library. OkHttp is a pure ... NONE }) return builder.build() } @Provides @Singleton fun ...
Read more >
okhttp-android-support/src/main/java/com/squareup/okhttp ...
Helper methods that convert between Java and OkHttp representations. ... return certificates.size() > 0 ? certificates : null;.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found