Add Currency Converter management

feature/top10
Aurelien Hubert 7 years ago
parent 88d5ad5200
commit 26c63cb658
  1. 80
      app/src/main/java/net/pokeranalytics/android/api/CurrencyConverterApi.kt
  2. 11
      app/src/main/java/net/pokeranalytics/android/model/retrofit/ConvertResult.kt
  3. 4
      app/src/main/java/net/pokeranalytics/android/util/URL.kt
  4. 1
      app/src/main/res/values/secrets.xml

@ -0,0 +1,80 @@
package net.pokeranalytics.android.api
import android.content.Context
import net.pokeranalytics.android.BuildConfig
import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.retrofit.CurrencyConverterValue
import net.pokeranalytics.android.util.URL
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Query
import java.util.concurrent.TimeUnit
/**
* Currency Converter API
*/
interface CurrencyConverterApi {
companion object {
private var currencyConverterApi: CurrencyConverterApi? = null
fun getApi(context: Context): CurrencyConverterApi? {
if (currencyConverterApi == null) {
var serviceEndpoint = URL.API_CURRENCY_CONVERTER
val httpClient = OkHttpClient.Builder()
// Logging interceptor
if (BuildConfig.DEBUG) {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BASIC
httpClient.addInterceptor(interceptor)
}
// Add headers
val interceptor = Interceptor { chain ->
val original = chain.request()
val originalHttpUrl = original.url()
val url = originalHttpUrl.newBuilder()
.addQueryParameter("apiKey", context.getString(R.string.currency_converter_api))
.build()
val requestBuilder = original.newBuilder()
.url(url)
chain.proceed(requestBuilder.build())
}
httpClient.addInterceptor(interceptor)
val client = httpClient
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.build()
val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(serviceEndpoint.value)
.client(client)
.build()
currencyConverterApi = retrofit.create(CurrencyConverterApi::class.java)
}
return currencyConverterApi
}
}
@GET("convert")
fun convert(@Query("q") currencies: String, @Query("compact") compact: String = "y"): Call<Map<String, CurrencyConverterValue>>
}

@ -0,0 +1,11 @@
package net.pokeranalytics.android.model.retrofit
import com.google.gson.annotations.SerializedName
/**
* Currency Converter mapping class
*/
class CurrencyConverterValue {
@SerializedName("val")
var value: Double? = 0.0
}

@ -16,8 +16,10 @@ enum class URL(var value: String) {
FACEBOOK("https://www.facebook.com/171053452998758"), FACEBOOK("https://www.facebook.com/171053452998758"),
// Support // Support
SUPPORT_EMAIL("support@pokeranalytics.net") SUPPORT_EMAIL("support@pokeranalytics.net"),
// Currency Converter API
API_CURRENCY_CONVERTER("https://free.currencyconverterapi.com/api/v5/")
} }

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="google_places_api" translatable="false">AIzaSyCg-vgW4YnFsQ_s1iWjgzSq2vT0te3R1Hw</string> <string name="google_places_api" translatable="false">AIzaSyCg-vgW4YnFsQ_s1iWjgzSq2vT0te3R1Hw</string>
<string name="currency_converter_api" translatable="false">5ba8d38995282fe8b1c8</string>
</resources> </resources>
Loading…
Cancel
Save