Skip to content

Instantly share code, notes, and snippets.

@erawhctim
Last active May 6, 2024 22:05
Show Gist options
  • Save erawhctim/f0ce324278aad6d4dd956dcdb1e90902 to your computer and use it in GitHub Desktop.
Save erawhctim/f0ce324278aad6d4dd956dcdb1e90902 to your computer and use it in GitHub Desktop.
OkHttp composite EventListener (based on OkHttp v4.9.2)
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
*/
package com.yourapp.here
import okhttp3.Call
import okhttp3.Connection
import okhttp3.EventListener
import okhttp3.Handshake
import okhttp3.HttpUrl
import okhttp3.Protocol
import okhttp3.Request
import okhttp3.Response
import java.io.IOException
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.Proxy
/**
* Allows for composing multiple [EventListener]s together.
*
* See https://github.com/square/okhttp/issues/6256
*/
class CompositeEventListener(
private val listeners: Set<EventListener>
): EventListener() {
override fun callStart(call: Call) {
listeners.forEach { it.callStart(call) }
}
override fun proxySelectStart(call: Call, url: HttpUrl, ) {
listeners.forEach { it.proxySelectStart(call, url) }
}
override fun proxySelectEnd(call: Call, url: HttpUrl, proxies: List<Proxy>
) {
listeners.forEach { it.proxySelectEnd(call, url, proxies) }
}
override fun dnsStart(call: Call, domainName: String) {
listeners.forEach { it.dnsStart(call, domainName) }
}
override fun dnsEnd(call: Call, domainName: String, inetAddressList: List<InetAddress>) {
listeners.forEach { it.dnsEnd(call, domainName, inetAddressList) }
}
override fun connectStart(call: Call, inetSocketAddress: InetSocketAddress, proxy: Proxy) {
listeners.forEach { it.connectStart(call, inetSocketAddress, proxy) }
}
override fun secureConnectStart(call: Call) {
listeners.forEach { it.secureConnectStart(call) }
}
override fun secureConnectEnd(call: Call, handshake: Handshake?) {
listeners.forEach { it.secureConnectEnd(call, handshake) }
}
override fun connectEnd(call: Call, inetSocketAddress: InetSocketAddress, proxy: Proxy, protocol: Protocol?) {
listeners.forEach { it.connectEnd(call, inetSocketAddress, proxy, protocol) }
}
override fun connectFailed(
call: Call,
inetSocketAddress: InetSocketAddress,
proxy: Proxy,
protocol: Protocol?,
ioe: IOException
) {
listeners.forEach { it.connectFailed(call, inetSocketAddress, proxy, protocol, ioe) }
}
override fun connectionAcquired(call: Call, connection: Connection) {
listeners.forEach { it.connectionAcquired(call, connection) }
}
override fun connectionReleased(call: Call, connection: Connection) {
listeners.forEach { it.connectionReleased(call, connection) }
}
override fun requestHeadersStart(call: Call) {
listeners.forEach { it.requestHeadersStart(call) }
}
override fun requestHeadersEnd(call: Call, request: Request) {
listeners.forEach { it.requestHeadersEnd(call, request) }
}
override fun requestBodyStart(call: Call) {
listeners.forEach { it.requestBodyStart(call) }
}
override fun requestBodyEnd(call: Call, byteCount: Long) {
listeners.forEach { it.requestBodyEnd(call, byteCount) }
}
override fun requestFailed(call: Call, ioe: IOException) {
listeners.forEach { it.requestFailed(call, ioe) }
}
override fun responseHeadersStart(call: Call) {
listeners.forEach { it.responseHeadersStart(call) }
}
override fun responseHeadersEnd(call: Call, response: Response) {
listeners.forEach { it.responseHeadersEnd(call, response) }
}
override fun responseBodyStart(call: Call) {
listeners.forEach { it.responseBodyStart(call) }
}
override fun responseBodyEnd(call: Call, byteCount: Long) {
listeners.forEach { it.responseBodyEnd(call, byteCount) }
}
override fun responseFailed(call: Call, ioe: IOException) {
listeners.forEach { it.responseFailed(call, ioe) }
}
override fun callEnd(call: Call) {
listeners.forEach { it.callEnd(call) }
}
override fun callFailed(call: Call, ioe: IOException) {
listeners.forEach { it.callFailed(call, ioe) }
}
override fun canceled(call: Call) {
listeners.forEach { it.canceled(call) }
}
override fun satisfactionFailure(call: Call, response: Response) {
listeners.forEach { it.satisfactionFailure(call, response) }
}
override fun cacheHit(call: Call, response: Response) {
listeners.forEach { it.cacheHit(call, response) }
}
override fun cacheMiss(call: Call) {
listeners.forEach { it.cacheMiss(call) }
}
override fun cacheConditionalHit(call: Call, cachedResponse: Response) {
listeners.forEach { it.cacheConditionalHit(call, cachedResponse) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment