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

Fix missing custom interceptors and sentry interceptor in some http clients #180

Merged
merged 2 commits into from
May 27, 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
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
Loading