Update sync headers

pull/163/head
Koitharu 4 years ago
parent 1a6b4ae795
commit 32836d05d8
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -10,7 +10,7 @@ import okhttp3.Response
import okhttp3.Route
import org.koitharu.kotatsu.R
class AccountAuthenticator(
class SyncAuthenticator(
context: Context,
private val account: Account,
private val authApi: SyncAuthApi,

@ -5,9 +5,10 @@ import android.accounts.AccountManager
import android.content.Context
import okhttp3.Interceptor
import okhttp3.Response
import org.koitharu.kotatsu.BuildConfig
import org.koitharu.kotatsu.R
class AccountInterceptor(
class SyncInterceptor(
context: Context,
private val account: Account,
) : Interceptor {
@ -17,13 +18,11 @@ class AccountInterceptor(
override fun intercept(chain: Interceptor.Chain): Response {
val token = accountManager.peekAuthToken(account, tokenType)
val request = if (token != null) {
chain.request().newBuilder()
.header("Authorization", "Bearer $token")
.build()
} else {
chain.request()
val requestBuilder = chain.request().newBuilder()
if (token != null) {
requestBuilder.header("Authorization", "Bearer $token")
}
return chain.proceed(request)
requestBuilder.header("X-App-Version", BuildConfig.VERSION_CODE.toString())
return chain.proceed(requestBuilder.build())
}
}

@ -13,8 +13,8 @@ import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.db.*
import org.koitharu.kotatsu.parsers.util.json.mapJSONTo
import org.koitharu.kotatsu.parsers.util.parseJson
import org.koitharu.kotatsu.sync.data.AccountAuthenticator
import org.koitharu.kotatsu.sync.data.AccountInterceptor
import org.koitharu.kotatsu.sync.data.SyncAuthenticator
import org.koitharu.kotatsu.sync.data.SyncInterceptor
import org.koitharu.kotatsu.sync.data.SyncAuthApi
import org.koitharu.kotatsu.utils.GZipInterceptor
import org.koitharu.kotatsu.utils.ext.toContentValues
@ -37,8 +37,8 @@ class SyncHelper(
) {
private val httpClient = OkHttpClient.Builder()
.authenticator(AccountAuthenticator(context, account, SyncAuthApi(context, OkHttpClient())))
.addInterceptor(AccountInterceptor(context, account))
.authenticator(SyncAuthenticator(context, account, SyncAuthApi(context, OkHttpClient())))
.addInterceptor(SyncInterceptor(context, account))
.addInterceptor(GZipInterceptor())
.build()
private val baseUrl = context.getString(R.string.url_sync_server)

Loading…
Cancel
Save