aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostInterceptor.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostInterceptor.kt')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostInterceptor.kt29
1 files changed, 29 insertions, 0 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostInterceptor.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostInterceptor.kt
new file mode 100644
index 00000000..f745aedf
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostInterceptor.kt
@@ -0,0 +1,29 @@
+package com.pitchedapps.frost.facebook.retro
+
+import android.content.Context
+import com.pitchedapps.frost.facebook.token
+import com.pitchedapps.frost.utils.Utils
+import okhttp3.Interceptor
+import okhttp3.Response
+
+/**
+ * Created by Allan Wang on 2017-05-30.
+ */
+private val maxStale = 60 * 60 * 24 * 28 //maxAge to get from cache if online (4 weeks)
+const val ACCESS_TOKEN = "access_token"
+
+class FrostInterceptor(val context: Context) : Interceptor {
+
+ override fun intercept(chain: Interceptor.Chain): Response? {
+ val request = chain.request()
+ val requestBuilder = request.newBuilder()
+ val urlBase = request.url()
+ val urlWithToken = urlBase.newBuilder()
+ if (urlBase.queryParameter(ACCESS_TOKEN) == null && token != null)
+ urlWithToken.addQueryParameter(ACCESS_TOKEN, token)
+ requestBuilder.url(urlWithToken.build())
+ if (!Utils.isNetworkAvailable(context)) requestBuilder.addHeader("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
+ return chain.proceed(requestBuilder.build())
+ }
+
+} \ No newline at end of file