mirror of
https://github.com/mihonapp/mihon.git
synced 2024-12-26 10:58:24 +01:00
Clean up NetworkHelper
This commit is contained in:
parent
1cf7f9be54
commit
e0733c1a4c
@ -11,31 +11,26 @@ import java.io.File
|
|||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
class NetworkHelper(
|
class NetworkHelper(
|
||||||
context: Context,
|
private val context: Context,
|
||||||
private val preferences: NetworkPreferences,
|
private val preferences: NetworkPreferences,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
private val cacheDir = File(context.cacheDir, "network_cache")
|
val cookieJar by lazy { AndroidCookieJar() }
|
||||||
private val cacheSize = 5L * 1024 * 1024 // 5 MiB
|
|
||||||
|
|
||||||
val cookieJar = AndroidCookieJar()
|
val client by lazy {
|
||||||
|
|
||||||
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()
|
val builder = OkHttpClient.Builder()
|
||||||
.cookieJar(cookieJar)
|
.cookieJar(cookieJar)
|
||||||
.connectTimeout(30, TimeUnit.SECONDS)
|
.connectTimeout(30, TimeUnit.SECONDS)
|
||||||
.readTimeout(30, TimeUnit.SECONDS)
|
.readTimeout(30, TimeUnit.SECONDS)
|
||||||
.callTimeout(2, TimeUnit.MINUTES)
|
.callTimeout(2, TimeUnit.MINUTES)
|
||||||
|
.cache(
|
||||||
|
Cache(
|
||||||
|
directory = File(context.cacheDir, "network_cache"),
|
||||||
|
maxSize = 5L * 1024 * 1024, // 5 MiB
|
||||||
|
),
|
||||||
|
)
|
||||||
.addInterceptor(UncaughtExceptionInterceptor())
|
.addInterceptor(UncaughtExceptionInterceptor())
|
||||||
.addInterceptor(userAgentInterceptor)
|
.addInterceptor(UserAgentInterceptor(::defaultUserAgentProvider))
|
||||||
|
|
||||||
if (preferences.verboseLogging().get()) {
|
if (preferences.verboseLogging().get()) {
|
||||||
val httpLoggingInterceptor = HttpLoggingInterceptor().apply {
|
val httpLoggingInterceptor = HttpLoggingInterceptor().apply {
|
||||||
@ -44,7 +39,7 @@ class NetworkHelper(
|
|||||||
builder.addNetworkInterceptor(httpLoggingInterceptor)
|
builder.addNetworkInterceptor(httpLoggingInterceptor)
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.addInterceptor(cloudflareInterceptor)
|
builder.addInterceptor(CloudflareInterceptor(context, cookieJar, ::defaultUserAgentProvider))
|
||||||
|
|
||||||
when (preferences.dohProvider().get()) {
|
when (preferences.dohProvider().get()) {
|
||||||
PREF_DOH_CLOUDFLARE -> builder.dohCloudflare()
|
PREF_DOH_CLOUDFLARE -> builder.dohCloudflare()
|
||||||
@ -61,17 +56,15 @@ class NetworkHelper(
|
|||||||
PREF_DOH_SHECAN -> builder.dohShecan()
|
PREF_DOH_SHECAN -> builder.dohShecan()
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder
|
builder.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
val client by lazy { baseClientBuilder.cache(Cache(cacheDir, cacheSize)).build() }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Since extension-lib 1.5
|
* @deprecated Since extension-lib 1.5
|
||||||
*/
|
*/
|
||||||
@Deprecated("The regular client handles Cloudflare by default")
|
@Deprecated("The regular client handles Cloudflare by default")
|
||||||
@Suppress("UNUSED")
|
@Suppress("UNUSED")
|
||||||
val cloudflareClient = client
|
val cloudflareClient by lazy { client }
|
||||||
|
|
||||||
fun defaultUserAgentProvider() = preferences.defaultUserAgent().get().trim()
|
fun defaultUserAgentProvider() = preferences.defaultUserAgent().get().trim()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user