Data classes and other retrofit logic added
This commit is contained in:
@ -0,0 +1,22 @@
|
||||
package ru.vendetti.bitcoin_summarizer
|
||||
|
||||
import retrofit2.Response
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Query
|
||||
|
||||
// Retrofit-API-интерфейс
|
||||
interface CryptoApiService {
|
||||
// Получение индекса страха и жадности (по умолчанию за 30 дней)
|
||||
@GET("fng/")
|
||||
suspend fun getFearAndGreedIndex(
|
||||
@Query("limit") limit: Int = 30
|
||||
): Response<FearAndGreedResponse>
|
||||
|
||||
// Получение глобальной информации
|
||||
@GET("v1/global/")
|
||||
suspend fun getGlobalData(): Response<GlobalResponse>
|
||||
|
||||
// Получение данных Bitcoin
|
||||
@GET("v1/ticker/bitcoin/")
|
||||
suspend fun getBitcoinTicker(): Response<TickerResponse>
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package ru.vendetti.bitcoin_summarizer
|
||||
|
||||
//Репозиторий для обработки запросов
|
||||
class CryptoRepository(private val apiService: CryptoApiService) {
|
||||
/**
|
||||
* Запрашивает данные для Bitcoin-тикера
|
||||
*/
|
||||
suspend fun fetchBitcoinTicker(): TickerResponse? {
|
||||
return try {
|
||||
val response = apiService.getBitcoinTicker()
|
||||
if (response.isSuccessful) {
|
||||
response.body()
|
||||
} else {
|
||||
println("Ошибка запроса Bitcoin Ticker: ${response.code()}")
|
||||
null
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
println("Exception при получении Bitcoin Ticker: ${e.localizedMessage}")
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Запрашивает глобальные данные крипторынка
|
||||
*/
|
||||
suspend fun fetchGlobalData(): GlobalResponse? {
|
||||
return try {
|
||||
val response = apiService.getGlobalData()
|
||||
if (response.isSuccessful) {
|
||||
response.body()
|
||||
} else {
|
||||
println("Ошибка запроса Global Data: ${response.code()}")
|
||||
null
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
println("Exception при получении Global Data: ${e.localizedMessage}")
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Запрашивает индекс страха и жадности за указанный период (по умолчанию 30 дней)
|
||||
*/
|
||||
suspend fun fetchFearAndGreedData(limit: Int = 30): FearAndGreedResponse? {
|
||||
return try {
|
||||
val response = apiService.getFearAndGreedIndex(limit)
|
||||
if (response.isSuccessful) {
|
||||
response.body()
|
||||
} else {
|
||||
println("Ошибка запроса Fear & Greed Index: ${response.code()}")
|
||||
null
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
println("Exception при получении Fear & Greed Index: ${e.localizedMessage}")
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package ru.vendetti.bitcoin_summarizer
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
// Обёртка для данных (тут вложенность есть и это API v2, а не v1, поэтому чуть сложнее структура, чем в GlobalResponse.kt)
|
||||
data class FearAndGreedResponse(
|
||||
@SerializedName("name")
|
||||
val name: String,
|
||||
@SerializedName("data")
|
||||
val dataList: List<FearAndGreedData>
|
||||
)
|
||||
|
||||
// Сами данные
|
||||
data class FearAndGreedData(
|
||||
@SerializedName("value")
|
||||
val value: String,
|
||||
@SerializedName("value_classification")
|
||||
val valueClassification: String,
|
||||
@SerializedName("timestamp")
|
||||
val timestamp: String,
|
||||
@SerializedName("time_until_update")
|
||||
val timeUntilUpdate: String
|
||||
)
|
@ -0,0 +1,15 @@
|
||||
package ru.vendetti.bitcoin_summarizer
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
// Обёртка для данных
|
||||
data class GlobalResponse(
|
||||
val data: GlobalData
|
||||
)
|
||||
|
||||
// Сами данные
|
||||
data class GlobalData(
|
||||
@SerializedName("active_cryptocurrencies") val activeCryptocurrencies: String,
|
||||
@SerializedName("total_market_cap_usd") val totalMarketCapUsd: String,
|
||||
@SerializedName("total_24h_volume_usd") val total24hVolumeUsd: String
|
||||
)
|
@ -0,0 +1,16 @@
|
||||
package ru.vendetti.bitcoin_summarizer
|
||||
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
|
||||
object RetrofitClient {
|
||||
private const val BASE_URL = "https://api.alternative.me/"
|
||||
|
||||
val apiService: CryptoApiService by lazy {
|
||||
Retrofit.Builder()
|
||||
.baseUrl(BASE_URL)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
.create(CryptoApiService::class.java)
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package ru.vendetti.bitcoin_summarizer
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
// Обёртка для данных (тут json-массив с одним элементом, поэтому выглядит так)
|
||||
typealias TickerResponse = List<TickerData>
|
||||
|
||||
// Сами данные
|
||||
data class TickerData(
|
||||
val id: String,
|
||||
val name: String,
|
||||
val symbol: String,
|
||||
val rank: String,
|
||||
@SerializedName("price_usd") val priceUsd: String,
|
||||
@SerializedName("price_btc") val priceBtc: String,
|
||||
@SerializedName("24h_volume_usd") val volume24hUsd: String,
|
||||
@SerializedName("market_cap_usd") val marketCapUsd: String,
|
||||
@SerializedName("available_supply") val availableSupply: String,
|
||||
@SerializedName("total_supply") val totalSupply: String,
|
||||
@SerializedName("max_supply") val maxSupply: String?,
|
||||
@SerializedName("percent_change_1h") val percentChange1h: String,
|
||||
@SerializedName("percent_change_24h") val percentChange24h: String,
|
||||
@SerializedName("percent_change_7d") val percentChange7d: String,
|
||||
@SerializedName("last_updated") val lastUpdated: String
|
||||
)
|
Reference in New Issue
Block a user