Update sync headers

This commit is contained in:
Koitharu
2022-05-13 10:22:40 +03:00
parent 1a6b4ae795
commit 32836d05d8
3 changed files with 12 additions and 13 deletions

View File

@@ -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,

View File

@@ -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())
}
}

View File

@@ -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)