Creates an interface for the DualHttpClientManager

This commit is contained in:
Vitor Pamplona
2025-09-08 15:50:28 -04:00
parent 4c8456c4af
commit e0764da095

View File

@@ -30,13 +30,19 @@ import okhttp3.OkHttpClient
import java.net.InetSocketAddress import java.net.InetSocketAddress
import java.net.Proxy import java.net.Proxy
interface IHttpClientManager {
fun getHttpClient(useProxy: Boolean): OkHttpClient
fun getCurrentProxyPort(useProxy: Boolean): Int?
}
class DualHttpClientManager( class DualHttpClientManager(
userAgent: String, userAgent: String,
proxyPortProvider: StateFlow<Int?>, proxyPortProvider: StateFlow<Int?>,
isMobileDataProvider: StateFlow<Boolean?>, isMobileDataProvider: StateFlow<Boolean?>,
keyCache: EncryptionKeyCache, keyCache: EncryptionKeyCache,
scope: CoroutineScope, scope: CoroutineScope,
) { ) : IHttpClientManager {
val factory = OkHttpClientFactory(keyCache) val factory = OkHttpClientFactory(keyCache)
val defaultHttpClient: StateFlow<OkHttpClient> = val defaultHttpClient: StateFlow<OkHttpClient> =
@@ -60,17 +66,30 @@ class DualHttpClientManager(
fun getCurrentProxy(): Proxy? = defaultHttpClient.value.proxy fun getCurrentProxy(): Proxy? = defaultHttpClient.value.proxy
fun getCurrentProxyPort(useProxy: Boolean): Int? = override fun getCurrentProxyPort(useProxy: Boolean): Int? =
if (useProxy) { if (useProxy) {
(getCurrentProxy()?.address() as? InetSocketAddress)?.port (getCurrentProxy()?.address() as? InetSocketAddress)?.port
} else { } else {
null null
} }
fun getHttpClient(useProxy: Boolean): OkHttpClient = override fun getHttpClient(useProxy: Boolean): OkHttpClient =
if (useProxy) { if (useProxy) {
defaultHttpClient.value defaultHttpClient.value
} else { } else {
defaultHttpClientWithoutProxy.value defaultHttpClientWithoutProxy.value
} }
} }
object EmptyHttpClientManager : IHttpClientManager {
val rootOkHttpClient =
OkHttpClient
.Builder()
.followRedirects(true)
.followSslRedirects(true)
.build()
override fun getHttpClient(useProxy: Boolean) = rootOkHttpClient
override fun getCurrentProxyPort(useProxy: Boolean) = null
}