Skip to content

Commit

Permalink
Merge pull request #180 from Infomaniak/fix-network-interceptors
Browse files Browse the repository at this point in the history
Fix missing custom interceptors and sentry interceptor in some http clients
  • Loading branch information
tevincent authored May 27, 2024
2 parents b51db12 + ddec252 commit 9feb3e8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -27,4 +31,11 @@ object HttpClientConfig {
var cacheDir: File? = null
var customInterceptors: List<Interceptor>? = 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)
}
}
9 changes: 2 additions & 7 deletions src/main/java/com/infomaniak/lib/core/utils/CoilUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand All @@ -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 {
Expand Down

0 comments on commit 9feb3e8

Please sign in to comment.