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.
47 lines
1.2 KiB
47 lines
1.2 KiB
package net.pokeranalytics.android.api
|
|
|
|
import android.content.Context
|
|
import com.android.volley.Request
|
|
import com.android.volley.Response
|
|
import com.android.volley.toolbox.StringRequest
|
|
import com.android.volley.toolbox.Volley
|
|
import kotlinx.serialization.json.Json
|
|
import kotlinx.serialization.json.JsonConfiguration
|
|
import timber.log.Timber
|
|
|
|
class FreeConverterApi {
|
|
|
|
companion object {
|
|
|
|
fun currencyRate(pair: String, context: Context, callback: (Double) -> (Unit)) {
|
|
|
|
val queue = Volley.newRequestQueue(context)
|
|
val url = "https://free.currconv.com/api/v7/convert?q=${pair}&compact=ultra&apiKey=5ba8d38995282fe8b1c8"
|
|
|
|
// https://free.currconv.com/api/v7/convert?q=GBP_USD&compact=ultra&apiKey=5ba8d38995282fe8b1c8
|
|
// { "USD_PHP": 44.1105, "PHP_USD": 0.0227 }
|
|
|
|
val stringRequest = StringRequest(
|
|
Request.Method.GET, url,
|
|
Response.Listener { response ->
|
|
|
|
val json = Json(JsonConfiguration.Stable)
|
|
val f = json.parseJson(response)
|
|
f.jsonObject[pair]?.primitive?.double?.let { rate ->
|
|
callback(rate)
|
|
} ?: run {
|
|
Timber.d("no rate: $response")
|
|
}
|
|
|
|
},
|
|
Response.ErrorListener {
|
|
Timber.d("Api call failed: ${it.message}")
|
|
})
|
|
|
|
queue.add(stringRequest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} |