commit
c0b022a553
@ -0,0 +1,61 @@ |
|||||||
|
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<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) |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -1,46 +0,0 @@ |
|||||||
package net.pokeranalytics.android.api |
|
||||||
|
|
||||||
import android.content.Context |
|
||||||
import com.android.volley.Request |
|
||||||
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=9b56e742a75392c8aeb7" |
|
||||||
|
|
||||||
// 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 -> |
|
||||||
|
|
||||||
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") |
|
||||||
} |
|
||||||
|
|
||||||
}, |
|
||||||
{ |
|
||||||
Timber.d("Api call failed: ${it.message}") |
|
||||||
}) |
|
||||||
|
|
||||||
queue.add(stringRequest) |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
Loading…
Reference in new issue