From ac282634e084c8a026ddb506a0a81019028ff458 Mon Sep 17 00:00:00 2001 From: Gibran Chevalley Date: Thu, 23 May 2024 14:13:58 +0200 Subject: [PATCH 1/2] Fix missing custom interceptors and sentry interceptor in some http clients --- .../infomaniak/lib/core/auth/CredentialManager.kt | 8 +------- .../com/infomaniak/lib/core/networking/HttpClient.kt | 8 +------- .../lib/core/networking/HttpClientConfig.kt | 12 ++++++++++++ .../java/com/infomaniak/lib/core/utils/CoilUtils.kt | 9 ++------- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/infomaniak/lib/core/auth/CredentialManager.kt b/src/main/java/com/infomaniak/lib/core/auth/CredentialManager.kt index 0267a943..1f02e2a9 100644 --- a/src/main/java/com/infomaniak/lib/core/auth/CredentialManager.kt +++ b/src/main/java/com/infomaniak/lib/core/auth/CredentialManager.kt @@ -19,10 +19,7 @@ package com.infomaniak.lib.core.auth import androidx.collection.ArrayMap import androidx.lifecycle.LiveData -import com.facebook.stetho.okhttp3.StethoInterceptor -import com.infomaniak.lib.core.BuildConfig import com.infomaniak.lib.core.models.user.User -import com.infomaniak.lib.core.networking.GZipInterceptor import com.infomaniak.lib.core.networking.HttpClientConfig import com.infomaniak.lib.core.room.UserDatabase import com.infomaniak.lib.login.ApiToken @@ -79,9 +76,6 @@ abstract class CredentialManager { private suspend fun getHttpClientUser(userId: Int, timeout: Long?): OkHttpClient { return OkHttpClient.Builder().apply { - if (BuildConfig.DEBUG) { - addNetworkInterceptor(StethoInterceptor()) - } timeout?.let { callTimeout(timeout, TimeUnit.SECONDS) readTimeout(timeout, TimeUnit.SECONDS) @@ -106,8 +100,8 @@ abstract class CredentialManager { } HttpClientConfig.apply { cacheDir?.let { cache(Cache(it, CACHE_SIZE_BYTES)) } } + HttpClientConfig.addCommonInterceptors(this) - addInterceptor(GZipInterceptor()) addInterceptor(TokenInterceptor(tokenInterceptorListener)) authenticator(TokenAuthenticator(tokenInterceptorListener)) }.run { diff --git a/src/main/java/com/infomaniak/lib/core/networking/HttpClient.kt b/src/main/java/com/infomaniak/lib/core/networking/HttpClient.kt index b5a9be07..0507d828 100644 --- a/src/main/java/com/infomaniak/lib/core/networking/HttpClient.kt +++ b/src/main/java/com/infomaniak/lib/core/networking/HttpClient.kt @@ -17,12 +17,9 @@ */ package com.infomaniak.lib.core.networking -import com.facebook.stetho.okhttp3.StethoInterceptor -import com.infomaniak.lib.core.BuildConfig import com.infomaniak.lib.core.auth.TokenAuthenticator import com.infomaniak.lib.core.auth.TokenInterceptor import com.infomaniak.lib.core.auth.TokenInterceptorListener -import io.sentry.okhttp.SentryOkHttpInterceptor import okhttp3.Cache import okhttp3.OkHttpClient import java.util.concurrent.TimeUnit @@ -72,10 +69,7 @@ object HttpClient { } private fun OkHttpClient.Builder.addInterceptors() { - if (BuildConfig.DEBUG) addNetworkInterceptor(StethoInterceptor()) - addInterceptor(GZipInterceptor()) - addInterceptor(SentryOkHttpInterceptor(captureFailedRequests = true)) - HttpClientConfig.customInterceptors?.forEach(::addInterceptor) + HttpClientConfig.addCommonInterceptors(this) } private fun OkHttpClient.Builder.addTokenInterceptor() { diff --git a/src/main/java/com/infomaniak/lib/core/networking/HttpClientConfig.kt b/src/main/java/com/infomaniak/lib/core/networking/HttpClientConfig.kt index 3f9ca577..58cb78f5 100644 --- a/src/main/java/com/infomaniak/lib/core/networking/HttpClientConfig.kt +++ b/src/main/java/com/infomaniak/lib/core/networking/HttpClientConfig.kt @@ -17,7 +17,11 @@ */ package com.infomaniak.lib.core.networking +import com.facebook.stetho.okhttp3.StethoInterceptor +import com.infomaniak.lib.core.BuildConfig +import io.sentry.okhttp.SentryOkHttpInterceptor import okhttp3.Interceptor +import okhttp3.OkHttpClient import java.io.File object HttpClientConfig { @@ -27,4 +31,12 @@ object HttpClientConfig { var cacheDir: File? = null var customInterceptors: List? = null var customTimeoutMinutes = 2L + + fun addCommonInterceptors(builder: OkHttpClient.Builder) = with(builder) { + if (BuildConfig.DEBUG) addNetworkInterceptor(StethoInterceptor()) + addInterceptor(GZipInterceptor()) + addInterceptor(SentryOkHttpInterceptor(captureFailedRequests = true)) + customInterceptors?.forEach(::addInterceptor) + + } } diff --git a/src/main/java/com/infomaniak/lib/core/utils/CoilUtils.kt b/src/main/java/com/infomaniak/lib/core/utils/CoilUtils.kt index b8428000..139102b7 100644 --- a/src/main/java/com/infomaniak/lib/core/utils/CoilUtils.kt +++ b/src/main/java/com/infomaniak/lib/core/utils/CoilUtils.kt @@ -24,12 +24,9 @@ import coil.decode.GifDecoder import coil.decode.ImageDecoderDecoder import coil.disk.DiskCache import coil.memory.MemoryCache -import com.facebook.stetho.okhttp3.StethoInterceptor -import com.infomaniak.lib.core.BuildConfig import com.infomaniak.lib.core.auth.TokenAuthenticator import com.infomaniak.lib.core.auth.TokenInterceptor import com.infomaniak.lib.core.auth.TokenInterceptorListener -import com.infomaniak.lib.core.networking.GZipInterceptor import com.infomaniak.lib.core.networking.HttpClientConfig import com.infomaniak.lib.core.networking.HttpUtils import okhttp3.Cache @@ -74,6 +71,7 @@ object CoilUtils { OkHttpClient.Builder().apply { HttpClientConfig.apply { cacheDir?.let { cache(Cache(it, CACHE_SIZE_BYTES)) } } + HttpClientConfig.addCommonInterceptors(this) tokenInterceptorListener?.let { addInterceptor(Interceptor { chain -> @@ -82,13 +80,10 @@ object CoilUtils { .build() .let(chain::proceed) }) - addInterceptor(GZipInterceptor()) + addInterceptor(TokenInterceptor(it)) authenticator(TokenAuthenticator(it)) } - if (BuildConfig.DEBUG) { - addNetworkInterceptor(StethoInterceptor()) - } }.build() } .memoryCache { From ddec2524483a6444d25f8851f426aae399452bdf Mon Sep 17 00:00:00 2001 From: Gibran Chevalley Date: Mon, 27 May 2024 09:23:50 +0200 Subject: [PATCH 2/2] Remove useless new line --- .../java/com/infomaniak/lib/core/networking/HttpClientConfig.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/infomaniak/lib/core/networking/HttpClientConfig.kt b/src/main/java/com/infomaniak/lib/core/networking/HttpClientConfig.kt index 58cb78f5..33f9eec9 100644 --- a/src/main/java/com/infomaniak/lib/core/networking/HttpClientConfig.kt +++ b/src/main/java/com/infomaniak/lib/core/networking/HttpClientConfig.kt @@ -37,6 +37,5 @@ object HttpClientConfig { addInterceptor(GZipInterceptor()) addInterceptor(SentryOkHttpInterceptor(captureFailedRequests = true)) customInterceptors?.forEach(::addInterceptor) - } }