You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.7 KiB
61 lines
1.7 KiB
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 {
|
|
|
|
private 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<RateResponse>(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<String, String> {
|
|
val headers = HashMap<String, String>()
|
|
headers["apikey"] = "XnfeyID3PMKd3k4zTPW0XmZAbcZlZgqH"
|
|
return headers
|
|
}
|
|
|
|
}
|
|
queue.add(stringRequest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} |