mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 16:00:31 +05:30
refactor: enable http logging in debug builds
This commit is contained in:
parent
c1c4a0218d
commit
e04b07d4ff
@ -126,6 +126,7 @@ dependencies {
|
||||
|
||||
/* Retrofit and Kotlinx Serialization */
|
||||
implementation(libs.square.retrofit)
|
||||
implementation(libs.logging.interceptor)
|
||||
implementation(libs.kotlinx.serialization)
|
||||
implementation(libs.kotlinx.datetime)
|
||||
implementation(libs.kotlinx.serialization.retrofit)
|
||||
|
@ -1,9 +1,12 @@
|
||||
package com.github.libretube.api
|
||||
|
||||
import com.github.libretube.BuildConfig
|
||||
import com.github.libretube.constants.PreferenceKeys
|
||||
import com.github.libretube.helpers.PreferenceHelper
|
||||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.create
|
||||
|
||||
@ -28,10 +31,13 @@ object RetrofitInstance {
|
||||
private val kotlinxConverterFactory = JsonHelper.json
|
||||
.asConverterFactory("application/json".toMediaType())
|
||||
|
||||
private val httpClient by lazy { buildClient() }
|
||||
|
||||
val api by resettableLazy(lazyMgr) {
|
||||
Retrofit.Builder()
|
||||
.baseUrl(apiUrl)
|
||||
.callFactory(CronetHelper.callFactory)
|
||||
.client(httpClient)
|
||||
.addConverterFactory(kotlinxConverterFactory)
|
||||
.build()
|
||||
.create<PipedApi>()
|
||||
@ -41,6 +47,7 @@ object RetrofitInstance {
|
||||
Retrofit.Builder()
|
||||
.baseUrl(authUrl)
|
||||
.callFactory(CronetHelper.callFactory)
|
||||
.client(httpClient)
|
||||
.addConverterFactory(kotlinxConverterFactory)
|
||||
.build()
|
||||
.create<PipedApi>()
|
||||
@ -50,8 +57,23 @@ object RetrofitInstance {
|
||||
Retrofit.Builder()
|
||||
.baseUrl(apiUrl)
|
||||
.callFactory(CronetHelper.callFactory)
|
||||
.client(httpClient)
|
||||
.addConverterFactory(kotlinxConverterFactory)
|
||||
.build()
|
||||
.create<ExternalApi>()
|
||||
}
|
||||
|
||||
private fun buildClient(): OkHttpClient {
|
||||
val httpClient = OkHttpClient().newBuilder()
|
||||
|
||||
if (BuildConfig.DEBUG) {
|
||||
val loggingInterceptor = HttpLoggingInterceptor().apply {
|
||||
level = HttpLoggingInterceptor.Level.BASIC
|
||||
}
|
||||
|
||||
httpClient.addInterceptor(loggingInterceptor)
|
||||
}
|
||||
|
||||
return httpClient.build()
|
||||
}
|
||||
}
|
||||
|
@ -11,15 +11,18 @@ import coil.ImageLoader
|
||||
import coil.disk.DiskCache
|
||||
import coil.request.CachePolicy
|
||||
import coil.request.ImageRequest
|
||||
import com.github.libretube.BuildConfig
|
||||
import com.github.libretube.api.CronetHelper
|
||||
import com.github.libretube.constants.PreferenceKeys
|
||||
import com.github.libretube.extensions.toAndroidUri
|
||||
import com.github.libretube.extensions.toAndroidUriOrNull
|
||||
import com.github.libretube.util.DataSaverMode
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
|
||||
object ImageHelper {
|
||||
lateinit var imageLoader: ImageLoader
|
||||
@ -32,9 +35,22 @@ object ImageHelper {
|
||||
fun initializeImageLoader(context: Context) {
|
||||
val maxCacheSize = PreferenceHelper.getString(PreferenceKeys.MAX_IMAGE_CACHE, "128")
|
||||
|
||||
val httpClient = OkHttpClient().newBuilder()
|
||||
|
||||
if (BuildConfig.DEBUG) {
|
||||
val loggingInterceptor = HttpLoggingInterceptor().apply {
|
||||
level = HttpLoggingInterceptor.Level.BASIC
|
||||
}
|
||||
|
||||
httpClient.addInterceptor(loggingInterceptor)
|
||||
}
|
||||
|
||||
imageLoader = ImageLoader.Builder(context)
|
||||
.callFactory(CronetHelper.callFactory)
|
||||
.crossfade(true)
|
||||
.okHttpClient {
|
||||
httpClient.build()
|
||||
}
|
||||
.apply {
|
||||
if (maxCacheSize.isEmpty()) {
|
||||
diskCachePolicy(CachePolicy.DISABLED)
|
||||
|
@ -6,6 +6,7 @@ kotlin = "1.9.23"
|
||||
ksp = "1.9.23-1.0.19"
|
||||
lifecycle = "2.7.0"
|
||||
constraintlayout = "2.1.4"
|
||||
loggingInterceptor = "4.12.0"
|
||||
material = "1.11.0"
|
||||
navigation = "2.7.7"
|
||||
preference = "1.2.1"
|
||||
@ -43,6 +44,7 @@ androidx-fragment = { group = "androidx.fragment", name = "fragment-ktx", versio
|
||||
gradle = { module = "com.android.tools.build:gradle", version.ref = "gradle" }
|
||||
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
|
||||
kotlin-serialization = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kotlin" }
|
||||
logging-interceptor = { module = "com.squareup.okhttp3:logging-interceptor", version.ref = "loggingInterceptor" }
|
||||
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
|
||||
androidx-navigation-fragment = { group = "androidx.navigation", name = "navigation-fragment-ktx", version.ref = "navigation" }
|
||||
androidx-navigation-ui = { group = "androidx.navigation", name = "navigation-ui-ktx", version.ref = "navigation" }
|
||||
|
Loading…
x
Reference in New Issue
Block a user