Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: enable http logging in debug builds #5849

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/github/libretube/api/RetrofitInstance.kt
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -28,10 +31,13 @@
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>()
Expand All @@ -41,6 +47,7 @@
Retrofit.Builder()
.baseUrl(authUrl)
.callFactory(CronetHelper.callFactory)
.client(httpClient)
.addConverterFactory(kotlinxConverterFactory)
.build()
.create<PipedApi>()
Expand All @@ -50,8 +57,23 @@
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 {

Check failure on line 70 in app/src/main/java/com/github/libretube/api/RetrofitInstance.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 A multiline expression should start on a new line Raw Output: app/src/main/java/com/github/libretube/api/RetrofitInstance.kt:70:38: error: A multiline expression should start on a new line (standard:multiline-expression-wrapping)
level = HttpLoggingInterceptor.Level.BASIC
}

httpClient.addInterceptor(loggingInterceptor)
}

return httpClient.build()
}
}
20 changes: 18 additions & 2 deletions app/src/main/java/com/github/libretube/helpers/ImageHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
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
Expand All @@ -32,9 +35,22 @@
fun initializeImageLoader(context: Context) {
val maxCacheSize = PreferenceHelper.getString(PreferenceKeys.MAX_IMAGE_CACHE, "128")

val httpClient = OkHttpClient().newBuilder()

if (BuildConfig.DEBUG) {
val loggingInterceptor = HttpLoggingInterceptor().apply {

Check failure on line 41 in app/src/main/java/com/github/libretube/helpers/ImageHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 A multiline expression should start on a new line Raw Output: app/src/main/java/com/github/libretube/helpers/ImageHelper.kt:41:38: error: A multiline expression should start on a new line (standard:multiline-expression-wrapping)
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)
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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" }
Expand Down
Loading