Updated on 2026-08-14

This commit is contained in:
Tangem 2023-01-10 17:32:11 +04:00
parent 7e5f956f5d
commit c1e167fe71
7 changed files with 51 additions and 81 deletions

View file

@ -1,30 +0,0 @@
package com.tangem.datasource.api.common
import okhttp3.Interceptor
import okhttp3.Response
/**
[REDACTED_AUTHOR]
*/
open class AddHeaderInterceptor(
private val headers: Map<String, String>,
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request().newBuilder().apply {
headers.forEach {
addHeader(it.key, it.value)
}
}.build()
return chain.proceed(request)
}
}
class CacheControlHttpInterceptor(maxAgeSeconds: Int) : AddHeaderInterceptor(
mapOf("Cache-Control" to "max-age=$maxAgeSeconds"),
)
class CardPublicKeyHttpInterceptor(cardPublicKeyHex: String) : AddHeaderInterceptor(
mapOf("card_public_key" to cardPublicKeyHex),
)

View file

@ -1,39 +1,42 @@
package com.tangem.datasource.api.tangemTech
import com.tangem.datasource.api.common.AddHeaderInterceptor
import com.tangem.datasource.api.common.CacheControlHttpInterceptor
import com.tangem.datasource.api.common.createRetrofitInstance
import com.tangem.datasource.api.common.MoshiConverter
import com.tangem.datasource.utils.RequestHeader
import com.tangem.datasource.utils.RequestHeader.AuthenticationHeader
import com.tangem.datasource.utils.RequestHeader.CacheControlHeader
import com.tangem.datasource.utils.addHeaders
import com.tangem.datasource.utils.allowLogging
import okhttp3.OkHttpClient
import retrofit2.Retrofit
/**
[REDACTED_AUTHOR]
*/
class TangemTechService(
private val logIsEnabled: Boolean = false,
) {
private val headerInterceptors = mutableListOf<AddHeaderInterceptor>(
CacheControlHttpInterceptor(cacheMaxAge),
)
//TODO("Remove after removing Redux")
@Deprecated("Use TangemTechApi")
object TangemTechService {
var api: TangemTechApi = createApi()
private set
fun addHeaderInterceptors(interceptors: List<AddHeaderInterceptor>) {
headerInterceptors.removeAll(interceptors)
headerInterceptors.addAll(interceptors)
api = createApi()
private const val TANGEM_TECH_BASE_URL = "https://api.tangem-tech.com/v1/"
fun addAuthenticationHeader(header: AuthenticationHeader) {
api = createApi(header)
}
private fun createApi(): TangemTechApi {
val retrofit = createRetrofitInstance(
baseUrl = baseUrl,
interceptors = headerInterceptors.toList(),
logEnabled = logIsEnabled,
)
return retrofit.create(TangemTechApi::class.java)
}
companion object {
const val baseUrl = "https://api.tangem-tech.com/v1/"
const val cacheMaxAge = 600
private fun createApi(header: RequestHeader? = null): TangemTechApi {
val headers = mutableListOf<RequestHeader>(CacheControlHeader).apply { header?.let(::add) }
return Retrofit.Builder()
.addConverterFactory(MoshiConverter.createFactory())
.baseUrl(TANGEM_TECH_BASE_URL)
.client(
OkHttpClient.Builder()
.addHeaders(*headers.toTypedArray())
.allowLogging()
.build(),
)
.build()
.create(TangemTechApi::class.java)
}
}

View file

@ -37,15 +37,15 @@ class NetworkModule {
fun provideTangemTechApi(authProvider: AuthProvider, moshi: Moshi): TangemTechApi {
return Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(DEV_TANGEM_TECH_BASE_URL)
.baseUrl(PROD_TANGEM_TECH_BASE_URL)
.client(
OkHttpClient.Builder()
.addHeaders(
CacheControlHeader,
AuthenticationHeader(authProvider)
AuthenticationHeader(authProvider),
)
.allowLogging()
.build()
.build(),
)
.build()
.create(TangemTechApi::class.java)
@ -58,12 +58,12 @@ class NetworkModule {
.addConverterFactory(
MoshiConverterFactory.create(moshi),
)
.baseUrl(DEV_TANGEM_TECH_BASE_URL)
.baseUrl(PROD_TANGEM_TECH_BASE_URL)
.client(
OkHttpClient.Builder()
.addHeaders(AuthenticationHeader(authProvider))
.allowLogging()
.build()
.build(),
)
.build()
.create(ReferralApi::class.java)

View file

@ -74,6 +74,7 @@ dependencies {
implementation(project(":core:datasource"))
implementation(project(":core:utils"))
implementation(project(":common"))
implementation(project(":libs:auth"))
/** Tangem libraries */
implementation(Tangem.blockchain) {

View file

@ -13,7 +13,6 @@ object LogConfig {
object NetworkLogConfig {
val mercuryoService: Boolean = false
val moonPayService: Boolean = false
val tangemTechService: Boolean = BuildConfig.DEBUG
val paymentologyApiService: Boolean = BuildConfig.DEBUG
val blockchainSdkNetwork: Boolean = BuildConfig.DEBUG
}

View file

@ -2,10 +2,11 @@ package com.tangem.domain.redux.global
import android.webkit.ValueCallback
import com.tangem.common.extensions.toHexString
import com.tangem.datasource.utils.RequestHeader
import com.tangem.domain.redux.BaseStoreHub
import com.tangem.domain.redux.DomainState
import com.tangem.domain.redux.ReStoreReducer
import com.tangem.datasource.api.common.CardPublicKeyHttpInterceptor
import com.tangem.lib.auth.AuthProvider
import org.rekotlin.Action
/**
@ -22,17 +23,8 @@ internal class DomainGlobalHub : BaseStoreHub<DomainGlobalState>("DomainGlobalHu
return storeState.copy(globalState = newHubState)
}
override suspend fun handleAction(
action: Action,
storeState: DomainState,
cancel: ValueCallback<Action>
) {
override suspend fun handleAction(action: Action, storeState: DomainState, cancel: ValueCallback<Action>) {
if (action !is DomainGlobalAction) return
val state = storeState.globalState
// when (action) {
// }
}
override fun getReducer(): ReStoreReducer<DomainGlobalState> = DomainGlobalReducer()
@ -43,9 +35,14 @@ private class DomainGlobalReducer : ReStoreReducer<DomainGlobalState> {
override fun reduceAction(action: Action, state: DomainGlobalState): DomainGlobalState {
return when (action) {
is DomainGlobalAction.SaveScanNoteResponse -> {
val cardPublicKeyHex = action.scanResponse.card.cardPublicKey.toHexString()
state.networkServices.tangemTechService.addHeaderInterceptors(
listOf(CardPublicKeyHttpInterceptor(cardPublicKeyHex))
val card = action.scanResponse.card
state.networkServices.tangemTechService.addAuthenticationHeader(
RequestHeader.AuthenticationHeader(
object : AuthProvider {
override fun getCardPublicKey(): String = card.cardPublicKey.toHexString()
override fun getCardId(): String = card.cardId
},
),
)
state.copy(scanResponse = action.scanResponse)
}

View file

@ -1,10 +1,10 @@
package com.tangem.domain.redux.global
import com.tangem.datasource.api.paymentology.PaymentologyApiService
import com.tangem.datasource.api.tangemTech.TangemTechService
import com.tangem.domain.DomainDialog
import com.tangem.domain.common.LogConfig
import com.tangem.domain.common.ScanResponse
import com.tangem.datasource.api.paymentology.PaymentologyApiService
import com.tangem.datasource.api.tangemTech.TangemTechService
/**
[REDACTED_AUTHOR]
@ -19,8 +19,8 @@ data class DomainGlobalState(
)
data class NetworkServices(
val tangemTechService: TangemTechService = TangemTechService(
LogConfig.network.tangemTechService),
val tangemTechService: TangemTechService = TangemTechService,
val paymentologyService: PaymentologyApiService = PaymentologyApiService(
LogConfig.network.paymentologyApiService),
LogConfig.network.paymentologyApiService,
),
)