httpclient: Configure basic caching using okhttp
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
@@ -171,8 +171,9 @@ class HttpClient @Inject constructor(private val okHttpClient: OkHttpClient): IH
|
||||
responseBytes = response.body.bytes(),
|
||||
errorString = if (!response.isSuccessful) response.message else String()
|
||||
).also {
|
||||
val isCached = if (response.cacheResponse != null) "CACHED" else "NETWORK"
|
||||
_responseCode.value = response.code
|
||||
Log.i(TAG, "OKHTTP [${response.code}] ${response.request.url}")
|
||||
Log.i(TAG, "OKHTTP [$isCached] [${response.code}] ${response.request.url}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,9 +34,11 @@ import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import kotlinx.serialization.json.Json
|
||||
import okhttp3.Cache
|
||||
import okhttp3.CertificatePinner
|
||||
import okhttp3.OkHttpClient
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import java.net.Authenticator
|
||||
import java.net.InetSocketAddress
|
||||
@@ -59,8 +61,13 @@ object OkHttpClientModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun providesOkHttpClientInstance(certPinner: CertificatePinner, proxy: Proxy?): OkHttpClient {
|
||||
fun providesOkHttpClientInstance(
|
||||
certificatePinner: CertificatePinner,
|
||||
proxy: Proxy?,
|
||||
cache: Cache
|
||||
): OkHttpClient {
|
||||
val okHttpClientBuilder = OkHttpClient().newBuilder()
|
||||
.cache(cache)
|
||||
.proxy(proxy)
|
||||
.connectTimeout(25, TimeUnit.SECONDS)
|
||||
.readTimeout(25, TimeUnit.SECONDS)
|
||||
@@ -70,7 +77,7 @@ object OkHttpClientModule {
|
||||
.followSslRedirects(true)
|
||||
|
||||
if (!BuildConfig.DEBUG) {
|
||||
okHttpClientBuilder.certificatePinner(certPinner)
|
||||
okHttpClientBuilder.certificatePinner(certificatePinner)
|
||||
}
|
||||
|
||||
return okHttpClientBuilder.build()
|
||||
@@ -122,6 +129,15 @@ object OkHttpClientModule {
|
||||
}
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun providesCacheDir(@ApplicationContext context: Context): Cache {
|
||||
return Cache(
|
||||
directory = File(context.cacheDir, "http_cache"),
|
||||
maxSize = 100L * 1024 * 1024
|
||||
)
|
||||
}
|
||||
|
||||
private fun getGoogleRootCertHashes(context: Context): List<String> {
|
||||
return try {
|
||||
val certs = getX509Certificates(context.resources.openRawResource(R.raw.google_roots_ca))
|
||||
|
||||
Reference in New Issue
Block a user