Updated on 2026-08-14
This commit is contained in:
parent
1a12b1bdf9
commit
2f7ed460ff
19 changed files with 160 additions and 252 deletions
|
|
@ -8,53 +8,43 @@ import java.math.BigDecimal
|
|||
interface HttpResponse
|
||||
sealed interface TangemTechResponse : HttpResponse
|
||||
|
||||
sealed class Coins : TangemTechResponse {
|
||||
data class PricesResponse(val prices: Map<String, Double>) : Coins()
|
||||
data class CoinsResponse(
|
||||
val imageHost: String,
|
||||
val coins: List<Coin>,
|
||||
val total: Int
|
||||
) : TangemTechResponse {
|
||||
|
||||
data class CheckAddressResponse(val imageHost: String?, val tokens: List<Token>, val total: Int) : Coins() {
|
||||
data class Token(
|
||||
val id: String,
|
||||
val name: String,
|
||||
val symbol: String,
|
||||
val active: Boolean,
|
||||
val contracts: List<Contract>
|
||||
) {
|
||||
data class Contract(
|
||||
val networkId: String,
|
||||
val address: String,
|
||||
val decimalCount: BigDecimal?,
|
||||
val active: Boolean
|
||||
)
|
||||
}
|
||||
data class Coin(
|
||||
val id: String,
|
||||
val name: String,
|
||||
val symbol: String,
|
||||
val active: Boolean,
|
||||
val networks: List<Network> = listOf()
|
||||
) : TangemTechResponse {
|
||||
|
||||
data class Network(
|
||||
val networkId: String,
|
||||
val address: String? = null,
|
||||
val decimalCount: BigDecimal? = null,
|
||||
) : TangemTechResponse
|
||||
}
|
||||
}
|
||||
|
||||
data class TokensResponse(val imageHost: String, val tokens: List<Token>, val total: Int) : Coins() {
|
||||
data class Token(
|
||||
val id: String,
|
||||
val name: String,
|
||||
val symbol: String,
|
||||
val contracts: List<Contract>?
|
||||
) {
|
||||
data class Contract(
|
||||
val networkId: String,
|
||||
val address: String,
|
||||
val decimalCount: BigDecimal?,
|
||||
)
|
||||
}
|
||||
}
|
||||
//rates.keys = networkId's
|
||||
data class RatesResponse(val rates: Map<String, Double>) : TangemTechResponse
|
||||
|
||||
data class CurrenciesResponse(val currencies: List<Currency>) {
|
||||
data class Currency(
|
||||
val id: String,
|
||||
val code: String, // this is an uppercase id
|
||||
val name: String,
|
||||
val rateBTC: String,
|
||||
val unit: String, // $, €, ₽
|
||||
val type: String,
|
||||
)
|
||||
data class CurrenciesResponse(val currencies: List<Currency>) {
|
||||
|
||||
enum class CurrencyType(val type: String) {
|
||||
Fiat("fiat"), Crypto("crypto")
|
||||
}
|
||||
data class Currency(
|
||||
val id: String,
|
||||
val code: String, // this is an uppercase id
|
||||
val name: String,
|
||||
val rateBTC: String,
|
||||
val unit: String, // $, €, ₽
|
||||
val type: CurrencyType,
|
||||
) : TangemTechResponse
|
||||
|
||||
enum class CurrencyType(val type: String) {
|
||||
Fiat("fiat"), Crypto("crypto")
|
||||
}
|
||||
}
|
||||
|
|
@ -8,22 +8,20 @@ import retrofit2.http.Query
|
|||
*/
|
||||
interface TangemTechApi {
|
||||
|
||||
@GET("coins/prices")
|
||||
suspend fun coinsPrices(
|
||||
@Query("currency") currency: String,
|
||||
@Query("ids") ids: String,
|
||||
): Coins.PricesResponse
|
||||
|
||||
@GET("coins/check-address")
|
||||
suspend fun coinsCheckAddress(
|
||||
@Query("contractAddress") contractAddress: String,
|
||||
@GET("coins")
|
||||
suspend fun coins(
|
||||
@Query("contractAddress") contractAddress: String? = null,
|
||||
@Query("networkId") networkId: String? = null,
|
||||
): Coins.CheckAddressResponse
|
||||
@Query("active") active: Boolean? = null,
|
||||
): CoinsResponse
|
||||
|
||||
@GET("coins/currencies")
|
||||
suspend fun coinsCurrencies(): Coins.CurrenciesResponse
|
||||
@GET("rates")
|
||||
suspend fun rates(
|
||||
@Query("currencyId") currencyId: String,
|
||||
@Query("coinIds") coinIds: String,
|
||||
): RatesResponse
|
||||
|
||||
@GET("coins/tokens")
|
||||
suspend fun coinsTokens(): Coins.TokensResponse
|
||||
@GET("currencies")
|
||||
suspend fun currencies(): CurrenciesResponse
|
||||
|
||||
}
|
||||
|
|
@ -12,19 +12,33 @@ import kotlinx.coroutines.withContext
|
|||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
class TangemTechService {
|
||||
|
||||
val coins: CoinsRoute = CoinsRoute()
|
||||
|
||||
private val techRoutes: List<TangemTechRoute> = listOf(
|
||||
coins
|
||||
)
|
||||
|
||||
private val headerInterceptors = mutableListOf<AddHeaderInterceptor>(
|
||||
CacheControlHttpInterceptor(cacheMaxAge)
|
||||
)
|
||||
|
||||
|
||||
private var api: TangemTechApi = createApi()
|
||||
|
||||
suspend fun coins(
|
||||
contractAddress: String? = null,
|
||||
networkId: String? = null,
|
||||
active: Boolean? = null,
|
||||
): Result<CoinsResponse> = withContext(Dispatchers.IO) {
|
||||
performRequest { api.coins(contractAddress, networkId, active) }
|
||||
}
|
||||
|
||||
suspend fun rates(
|
||||
currency: String,
|
||||
ids: List<String>
|
||||
): Result<RatesResponse> = withContext(Dispatchers.IO) {
|
||||
performRequest {
|
||||
api.rates(currency.lowercase(), ids.joinToString(","))
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun currencies(): Result<CurrenciesResponse> = withContext(Dispatchers.IO) {
|
||||
performRequest { api.currencies() }
|
||||
}
|
||||
|
||||
fun addHeaderInterceptors(interceptors: List<AddHeaderInterceptor>) {
|
||||
headerInterceptors.removeAll(interceptors)
|
||||
headerInterceptors.addAll(interceptors)
|
||||
|
|
@ -37,49 +51,11 @@ class TangemTechService {
|
|||
interceptors = headerInterceptors.toList(),
|
||||
// logEnabled = true,
|
||||
)
|
||||
return retrofit.create(TangemTechApi::class.java).apply {
|
||||
techRoutes.forEach { it.setApi(this) }
|
||||
}
|
||||
return retrofit.create(TangemTechApi::class.java)
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val baseUrl = "https://api.tangem-tech.com/"
|
||||
const val baseUrl = "https://api.tangem-tech.com/v1/"
|
||||
const val cacheMaxAge = 600
|
||||
}
|
||||
}
|
||||
|
||||
private interface TangemTechRoute {
|
||||
fun setApi(api: TangemTechApi)
|
||||
}
|
||||
|
||||
class CoinsRoute : TangemTechRoute {
|
||||
private lateinit var api: TangemTechApi
|
||||
|
||||
override fun setApi(api: TangemTechApi) {
|
||||
this.api = api
|
||||
}
|
||||
|
||||
suspend fun prices(
|
||||
currency: String,
|
||||
ids: List<String>
|
||||
): Result<Coins.PricesResponse> = withContext(Dispatchers.IO) {
|
||||
performRequest {
|
||||
api.coinsPrices(currency.lowercase(), ids.joinToString(","))
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun checkAddress(
|
||||
contractAddress: String,
|
||||
networkId: String? = null
|
||||
): Result<Coins.CheckAddressResponse> = withContext(Dispatchers.IO) {
|
||||
performRequest { api.coinsCheckAddress(contractAddress, networkId) }
|
||||
}
|
||||
|
||||
suspend fun currencies(): Result<Coins.CurrenciesResponse> = withContext(Dispatchers.IO) {
|
||||
performRequest { api.coinsCurrencies() }
|
||||
}
|
||||
|
||||
suspend fun tokens(): Result<Coins.TokensResponse> = withContext(Dispatchers.IO) {
|
||||
performRequest { api.coinsTokens() }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue