package net.pokeranalytics.android.api import android.content.Context import com.android.volley.VolleyError import com.android.volley.toolbox.StringRequest import com.android.volley.toolbox.Volley import kotlinx.serialization.Serializable import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json import timber.log.Timber @Serializable data class RateResponse(var info: RateInfo) @Serializable data class RateInfo(var rate: Double) class CurrencyConverterApi { companion object { val json = Json { ignoreUnknownKeys = true } fun currencyRate(fromCurrency: String, toCurrency: String, context: Context, callback: (Double?, VolleyError?) -> (Unit)) { val queue = Volley.newRequestQueue(context) // val url = "https://free.currconv.com/api/v7/convert?q=${pair}&compact=ultra&apiKey=9b56e742a75392c8aeb7" val url = "https://api.apilayer.com/exchangerates_data/convert?to=$toCurrency&from=$fromCurrency&amount=1" // https://free.currconv.com/api/v7/convert?q=GBP_USD&compact=ultra&apiKey=5ba8d38995282fe8b1c8 // { "USD_PHP": 44.1105, "PHP_USD": 0.0227 } Timber.d("Api call = $url") val stringRequest = object : StringRequest( Method.GET, url, { response -> val o = json.decodeFromString(response) Timber.d("rate = ${o.info.rate}") callback(o.info.rate, null) }, { Timber.d("Api call failed: ${it.message}") callback(null, it) }) { override fun getHeaders(): MutableMap { val headers = HashMap() headers["apikey"] = "XnfeyID3PMKd3k4zTPW0XmZAbcZlZgqH" return headers } } queue.add(stringRequest) } } }