Reputation: 147
I'm making a little application which tracks cryptocurrency values at the Bittrex exchange.
For this I'm using Bittrex' public api (https://bittrex.github.io/api/v3) Unfortunately the api doesn't provide the data I want with just one call, therefore I need to do two api calls.
What I want to achieve is to have one object containing all of the following values:
The bold variable is part of one api call, the other values are part of the other. 'Symbol' is part of both.
I'm using kotlin coroutines and I was hoping that I don't have to use something like RxJava to get this to work.
CoroutineScope(IO).launch {
val tickers = async {
api.getTickers()
}.await()
val markets = async {
api.getMarkets()
}.await()
val result = mutableListOf<Market>()
for (ticker in tickers.data) {
for (market in markets.data) {
if (ticker.symbol == market.symbol) {
result.add(
Market(
ticker.symbol,
ticker.lastTradeRate,
market.quoteVolume,
market.percentChange
)
)
}
}
}
}
Upvotes: 0
Views: 598
Reputation: 29260
You can make the 2 calls in parallel using coroutines.
Assuming firstApi
and secondApi
are suspend functions that return the data for each of the 2 blobs of info you need,
val data1Deferred = async { firstApi() }
val data2Deferred = async { secondApi() }
val data1 = data1Deferred.await()
val data2 = data2Deferred.await()
val result = Result(
// build result from data1 and data2
)
You would also need to add error handling.
Edit:
you can group your list by the symbol and generate a map:
val marketMap = markets.associateBy { it.symbol }
Then for each ticker you can get the corresponding market
for (ticker in tickers) {
val market = marketMap[ticker.symbol]
if (market != null) {
// join ticker and market
}
}
Upvotes: 1