Skip to content

Commit

Permalink
Bitso updated to latest API #46
Browse files Browse the repository at this point in the history
  • Loading branch information
aneonex committed Dec 12, 2020
1 parent 7048b7c commit 4e8459b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,60 @@ import com.aneonex.bitcoinchecker.datamodule.model.CheckerInfo
import com.aneonex.bitcoinchecker.datamodule.model.CurrencyPairInfo
import com.aneonex.bitcoinchecker.datamodule.model.Market
import com.aneonex.bitcoinchecker.datamodule.model.Ticker
import com.aneonex.bitcoinchecker.datamodule.model.currency.Currency
import com.aneonex.bitcoinchecker.datamodule.model.currency.VirtualCurrency
import com.aneonex.bitcoinchecker.datamodule.model.currency.CurrencyPairsMap
import com.aneonex.bitcoinchecker.datamodule.util.ParseUtils
import org.json.JSONObject
import java.time.ZonedDateTime
import java.util.*

class Bitso : Market(NAME, TTS_NAME, CURRENCY_PAIRS) {
class Bitso : Market(NAME, TTS_NAME, null) {
companion object {
private const val NAME = "Bitso"
private const val TTS_NAME = NAME
private const val URL = "https://api.bitso.com/public/info"
private val CURRENCY_PAIRS: CurrencyPairsMap = CurrencyPairsMap()
private const val URL = "https://api.bitso.com/v3/ticker?book=%1\$s"
private const val URL_CURRENCY_PAIRS = "https://api.bitso.com/v3/available_books/"
}

override fun getCurrencyPairsUrl(requestId: Int): String {
return URL_CURRENCY_PAIRS
}

init {
CURRENCY_PAIRS[VirtualCurrency.BTC] = arrayOf(
Currency.MXN
)
CURRENCY_PAIRS[VirtualCurrency.ETH] = arrayOf(
VirtualCurrency.BTC,
Currency.MXN
)
CURRENCY_PAIRS[VirtualCurrency.XRP] = arrayOf(
VirtualCurrency.BTC,
Currency.MXN
)
CURRENCY_PAIRS[VirtualCurrency.BCH] = arrayOf(
VirtualCurrency.BTC
)
CURRENCY_PAIRS[VirtualCurrency.LTC] = arrayOf(
VirtualCurrency.BTC,
Currency.MXN
)
@Throws(Exception::class)
override fun parseCurrencyPairsFromJsonObject(requestId: Int, jsonObject: JSONObject, pairs: MutableList<CurrencyPairInfo>) {
val books = jsonObject.getJSONArray("payload")
for (i in 0 until books.length()) {
val pairId = books.getJSONObject(i).getString("book")
val currencies = pairId.split("_".toRegex()).toTypedArray()
if (currencies.size != 2) continue
pairs.add(CurrencyPairInfo(
currencies[0].toUpperCase(Locale.ROOT),
currencies[1].toUpperCase(Locale.ROOT),
pairId))
}
}

override fun getUrl(requestId: Int, checkerInfo: CheckerInfo): String {
return URL
// Compatibility with previous version
val pairId: String = checkerInfo.currencyPairId ?: "${checkerInfo.currencyBaseLowerCase}_${checkerInfo.currencyCounterLowerCase}"
return String.format(URL, pairId)
}

@Throws(Exception::class)
override fun parseTickerFromJsonObject(requestId: Int, jsonObject: JSONObject, ticker: Ticker, checkerInfo: CheckerInfo) {
var pairId = checkerInfo.currencyPairId
if (pairId == null) {
pairId = checkerInfo.currencyBaseLowerCase + "_" + checkerInfo.currencyCounterLowerCase
}
val pairJsonObject = jsonObject.getJSONObject(pairId)
ticker.bid = ParseUtils.getDoubleFromString(pairJsonObject, "buy")
ticker.ask = ParseUtils.getDoubleFromString(pairJsonObject, "sell")
ticker.vol = ParseUtils.getDoubleFromString(pairJsonObject, "volume")
ticker.last = ParseUtils.getDoubleFromString(pairJsonObject, "rate")
}
jsonObject.getJSONObject("payload").let {
ticker.high = it.getDouble("high")
ticker.low = it.getDouble("low")

ticker.bid = it.getDouble("bid")
ticker.ask = it.getDouble("ask")

ticker.vol = it.getDouble("volume")
ticker.last = it.getDouble("last")

// ====================
// Get currency pairs
// ====================
override fun getCurrencyPairsUrl(requestId: Int): String? {
return URL
ticker.timestamp = ZonedDateTime.parse(it.getString("created_at")).toEpochSecond()
}
}

@Throws(Exception::class)
override fun parseCurrencyPairsFromJsonObject(requestId: Int, jsonObject: JSONObject, pairs: MutableList<CurrencyPairInfo>) {
val pairIds = jsonObject.names()!!
for (i in 0 until pairIds.length()) {
val pairId = pairIds.getString(i) ?: continue
val currencies = pairId.split("_".toRegex()).toTypedArray()
if (currencies.size != 2) continue
pairs.add(CurrencyPairInfo(
currencies[0].toUpperCase(Locale.US),
currencies[1].toUpperCase(Locale.US),
pairId))
}
override fun parseErrorFromJsonObject(requestId: Int, jsonObject: JSONObject, checkerInfo: CheckerInfo?): String? {
return jsonObject.getJSONObject("error").getString("message")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ import java.io.StringWriter

object CheckErrorsUtils {
private const val RAW_RESPONSE_CHARS_LIMIT = 5000

fun parseVolleyErrorMsg(context: Context, error: VolleyError?): String {
return if (error is NetworkError) context.getString(R.string.check_error_network) else if (error is TimeoutError) context.getString(R.string.check_error_timeout) else if (error is ServerError) context.getString(R.string.check_error_server) else if (error is ParseError) context.getString(R.string.check_error_parse) else context.getString(R.string.check_error_unknown)
return when (error) {
is NetworkError -> context.getString(R.string.check_error_network)
is TimeoutError -> context.getString(R.string.check_error_timeout)
is ClientError -> context.getString(R.string.check_error_client)
is ServerError -> context.getString(R.string.check_error_server)
is ParseError -> context.getString(R.string.check_error_parse)
else -> context.getString(R.string.check_error_unknown)
}
}

fun formatError(context: Context, errorMsg: String?): String {
Expand Down
1 change: 1 addition & 0 deletions dataModuleTester/src/main/res/values/check_errors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<string name="check_error_generic_prefix">Error: %1$s</string>
<string name="check_error_network">Network error</string>
<string name="check_error_timeout">Timeout error</string>
<string name="check_error_client">Client request error</string>
<string name="check_error_server">Cannot connect to server</string>
<string name="check_error_parse">Parsing error</string>
<string name="check_error_unknown">Unknown error</string>
Expand Down

0 comments on commit 4e8459b

Please sign in to comment.