0

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:

  • symbol (This is a shared value between both api calls, so this needs to match)
  • quoteVolume
  • percentChange
  • lastTradeRate

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
                        )
                    )
                }
            }
        }
    }

1 Answer 1

1

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
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

But then I would still need some way to check whether the 'symbol' is the same for both data objects, preferably without iterating over both lists every time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.