From e0733c1a4cef28cf413ca79597ba5ad739b5328a Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 15 Jul 2023 13:05:25 -0400 Subject: [PATCH] Clean up NetworkHelper --- .../kanade/tachiyomi/network/NetworkHelper.kt | 87 +++++++++---------- 1 file changed, 40 insertions(+), 47 deletions(-) diff --git a/core/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt b/core/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt index 54d1858839..8af3d45be5 100644 --- a/core/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt +++ b/core/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt @@ -11,67 +11,60 @@ import java.io.File import java.util.concurrent.TimeUnit class NetworkHelper( - context: Context, + private val context: Context, private val preferences: NetworkPreferences, ) { - private val cacheDir = File(context.cacheDir, "network_cache") - private val cacheSize = 5L * 1024 * 1024 // 5 MiB + val cookieJar by lazy { AndroidCookieJar() } - val cookieJar = AndroidCookieJar() + val client by lazy { + val builder = OkHttpClient.Builder() + .cookieJar(cookieJar) + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .callTimeout(2, TimeUnit.MINUTES) + .cache( + Cache( + directory = File(context.cacheDir, "network_cache"), + maxSize = 5L * 1024 * 1024, // 5 MiB + ), + ) + .addInterceptor(UncaughtExceptionInterceptor()) + .addInterceptor(UserAgentInterceptor(::defaultUserAgentProvider)) - private val userAgentInterceptor by lazy { - UserAgentInterceptor(::defaultUserAgentProvider) - } - private val cloudflareInterceptor by lazy { - CloudflareInterceptor(context, cookieJar, ::defaultUserAgentProvider) - } - - private val baseClientBuilder: OkHttpClient.Builder - get() { - val builder = OkHttpClient.Builder() - .cookieJar(cookieJar) - .connectTimeout(30, TimeUnit.SECONDS) - .readTimeout(30, TimeUnit.SECONDS) - .callTimeout(2, TimeUnit.MINUTES) - .addInterceptor(UncaughtExceptionInterceptor()) - .addInterceptor(userAgentInterceptor) - - if (preferences.verboseLogging().get()) { - val httpLoggingInterceptor = HttpLoggingInterceptor().apply { - level = HttpLoggingInterceptor.Level.HEADERS - } - builder.addNetworkInterceptor(httpLoggingInterceptor) + if (preferences.verboseLogging().get()) { + val httpLoggingInterceptor = HttpLoggingInterceptor().apply { + level = HttpLoggingInterceptor.Level.HEADERS } - - builder.addInterceptor(cloudflareInterceptor) - - when (preferences.dohProvider().get()) { - PREF_DOH_CLOUDFLARE -> builder.dohCloudflare() - PREF_DOH_GOOGLE -> builder.dohGoogle() - PREF_DOH_ADGUARD -> builder.dohAdGuard() - PREF_DOH_QUAD9 -> builder.dohQuad9() - PREF_DOH_ALIDNS -> builder.dohAliDNS() - PREF_DOH_DNSPOD -> builder.dohDNSPod() - PREF_DOH_360 -> builder.doh360() - PREF_DOH_QUAD101 -> builder.dohQuad101() - PREF_DOH_MULLVAD -> builder.dohMullvad() - PREF_DOH_CONTROLD -> builder.dohControlD() - PREF_DOH_NJALLA -> builder.dohNajalla() - PREF_DOH_SHECAN -> builder.dohShecan() - } - - return builder + builder.addNetworkInterceptor(httpLoggingInterceptor) } - val client by lazy { baseClientBuilder.cache(Cache(cacheDir, cacheSize)).build() } + builder.addInterceptor(CloudflareInterceptor(context, cookieJar, ::defaultUserAgentProvider)) + + when (preferences.dohProvider().get()) { + PREF_DOH_CLOUDFLARE -> builder.dohCloudflare() + PREF_DOH_GOOGLE -> builder.dohGoogle() + PREF_DOH_ADGUARD -> builder.dohAdGuard() + PREF_DOH_QUAD9 -> builder.dohQuad9() + PREF_DOH_ALIDNS -> builder.dohAliDNS() + PREF_DOH_DNSPOD -> builder.dohDNSPod() + PREF_DOH_360 -> builder.doh360() + PREF_DOH_QUAD101 -> builder.dohQuad101() + PREF_DOH_MULLVAD -> builder.dohMullvad() + PREF_DOH_CONTROLD -> builder.dohControlD() + PREF_DOH_NJALLA -> builder.dohNajalla() + PREF_DOH_SHECAN -> builder.dohShecan() + } + + builder.build() + } /** * @deprecated Since extension-lib 1.5 */ @Deprecated("The regular client handles Cloudflare by default") @Suppress("UNUSED") - val cloudflareClient = client + val cloudflareClient by lazy { client } fun defaultUserAgentProvider() = preferences.defaultUserAgent().get().trim() }