Skip to content
Snippets Groups Projects
Unverified Commit 4e89fccc authored by Jakob Möller's avatar Jakob Möller Committed by GitHub
Browse files

Remove Offline Cache Interceptor (#368)


Signed-off-by: default avatard067928 <jakob.moeller@sap.com>
parent dc62af15
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,6 @@ import de.rki.coronawarnapp.BuildConfig
import de.rki.coronawarnapp.CoronaWarnApplication
import de.rki.coronawarnapp.exception.http.ServiceFactoryException
import de.rki.coronawarnapp.http.config.HTTPVariables
import de.rki.coronawarnapp.http.interceptor.OfflineCacheInterceptor
import de.rki.coronawarnapp.http.interceptor.RetryInterceptor
import de.rki.coronawarnapp.http.interceptor.WebSecurityVerificationInterceptor
import de.rki.coronawarnapp.http.service.DistributionService
......@@ -47,9 +46,6 @@ class ServiceFactory {
HttpLoggingInterceptor().also {
if (BuildConfig.DEBUG) it.setLevel(HttpLoggingInterceptor.Level.BODY)
},
OfflineCacheInterceptor(
CoronaWarnApplication.getAppContext()
),
RetryInterceptor(),
HttpErrorParser()
)
......
package de.rki.coronawarnapp.http.interceptor
import android.content.Context
import de.rki.coronawarnapp.util.ConnectivityHelper.isNetworkEnabled
import okhttp3.Interceptor
import okhttp3.Response
class OfflineCacheInterceptor(private val context: Context) : Interceptor {
companion object {
private const val MAX_AGE = 5
private const val MAX_STALE_DAYS = 1
private const val MAX_STALE = 60 * 60 * 24 * MAX_STALE_DAYS
}
override fun intercept(chain: Interceptor.Chain): Response {
// Get the request from the chain.
var request = chain.request()
/*
* Leveraging the advantage of using Kotlin,
* we initialize the request and change its header depending on whether
* the device is connected to Internet or not.
*/
request = if (isNetworkEnabled(context)) {
/*
* If there is Internet, get the cache that was stored 5 seconds ago.
* If the cache is older than 5 seconds, then discard it,
* and indicate an error in fetching the response.
* The 'max-age' attribute is responsible for this behavior.
*/
request.newBuilder().header(
"Cache-Control",
"public, max-age=$MAX_AGE"
).build()
} else {
/*
* If there is no Internet, get the cache that was stored 1 days ago.
* If the cache is older than 1 day, then discard it,
* and indicate an error in fetching the response.
* The 'max-stale' attribute is responsible for this behavior.
* The 'only-if-cached' attribute indicates to not retrieve new data; fetch the cache only instead.
*/
request.newBuilder().header(
"Cache-Control",
"public, only-if-cached, max-stale=$MAX_STALE"
).build()
}
return chain.proceed(request)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment