Updated on 2026-08-14

This commit is contained in:
Tangem 2020-11-16 17:10:55 +03:00
parent 5968cf338f
commit 369ea273c2
13 changed files with 143 additions and 21 deletions

View file

@ -1,6 +1,5 @@
package com.tangem.tap.network.coinmarketcap
import com.tangem.tap.TapConfig
import com.tangem.tap.network.createRetrofitInstance
import okhttp3.Interceptor
import okhttp3.Response
@ -23,20 +22,20 @@ interface CoinMarketCapApi {
companion object {
private const val baseUrl = "https://pro-api.coinmarketcap.com/"
fun create(): CoinMarketCapApi {
fun create(apiKey: String): CoinMarketCapApi {
return createRetrofitInstance(
baseUrl,
listOf(createCoinMarketRequestInterceptor()),
listOf(createCoinMarketRequestInterceptor(apiKey)),
).create(CoinMarketCapApi::class.java)
}
}
}
private fun createCoinMarketRequestInterceptor(): Interceptor {
private fun createCoinMarketRequestInterceptor(apiKey: String): Interceptor {
return object : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val requestBuilder = chain.request().newBuilder()
requestBuilder.addHeader("X-CMC_PRO_API_KEY", TapConfig.coinMarketCapKey)
requestBuilder.addHeader("X-CMC_PRO_API_KEY", apiKey)
return chain.proceed(requestBuilder.build())
}
}

View file

@ -3,12 +3,18 @@ package com.tangem.tap.network.coinmarketcap
import com.tangem.commands.common.network.Result
import com.tangem.commands.common.network.performRequest
import com.tangem.tap.common.redux.global.FiatCurrencyName
import com.tangem.tap.domain.config.ConfigurationValue
import com.tangem.tap.store
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.math.BigDecimal
class CoinMarketCapService {
private val api: CoinMarketCapApi by lazy { CoinMarketCapApi.create() }
class CoinMarketCapService() {
private val api: CoinMarketCapApi by lazy { CoinMarketCapApi.create(getApiKey()) }
private fun getApiKey(): String {
return store.state.globalState.configManager?.getConfigValue(ConfigurationValue.coinMarketCapKey)?.value!!
}
suspend fun getRate(
currency: String, fiatCurrency: FiatCurrencyName? = null