From c9fb036798fc533a07b4b75386b51151b31f8be0 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Mon, 20 Jul 2020 16:37:46 -0300 Subject: [pos] create merchant-lib and move first v1 API endpoint there --- .../net/taler/merchantpos/config/ConfigManager.kt | 31 +++++++++++++--------- 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'merchant-terminal/src/main/java/net/taler/merchantpos/config/ConfigManager.kt') diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/config/ConfigManager.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/config/ConfigManager.kt index 171cf28..eee7905 100644 --- a/merchant-terminal/src/main/java/net/taler/merchantpos/config/ConfigManager.kt +++ b/merchant-terminal/src/main/java/net/taler/merchantpos/config/ConfigManager.kt @@ -34,10 +34,14 @@ import com.fasterxml.jackson.module.kotlin.readValue import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import net.taler.merchantlib.ConfigResponse +import net.taler.merchantlib.MerchantApi import net.taler.merchantpos.LogErrorListener import net.taler.merchantpos.R import org.json.JSONObject +private const val VERSION = "0:0:0" + private const val SETTINGS_NAME = "taler-merchant-terminal" private const val SETTINGS_CONFIG_URL = "configUrl" @@ -60,6 +64,7 @@ interface ConfigurationReceiver { class ConfigManager( private val context: Context, private val scope: CoroutineScope, + private val api: MerchantApi, private val mapper: ObjectMapper, private val queue: RequestQueue ) { @@ -114,25 +119,27 @@ class ConfigManager( return } - val params = mapOf("instance" to merchantConfig.instance) - val req = MerchantRequest(GET, merchantConfig, "config", params, null, - Listener { onMerchantConfigReceived(config, json, merchantConfig, it) }, - LogErrorListener { onNetworkError(it) } - ) - queue.add(req) + scope.launch(Dispatchers.IO) { + val configResponse = api.getConfig(merchantConfig.baseUrl, merchantConfig.apiKey) + onMerchantConfigReceived(config, json, merchantConfig, configResponse) + } } private fun onMerchantConfigReceived( newConfig: Config?, configJson: JSONObject, merchantConfig: MerchantConfig, - json: JSONObject + configResponse: ConfigResponse ) = scope.launch(Dispatchers.Default) { - val currency = json.getString("currency") - + // TODO do real matching + if (VERSION != configResponse.version) { + val str = context.getString(R.string.config_error_version) + mConfigUpdateResult.postValue(ConfigUpdateResult.Error(str)) + return@launch + } for (receiver in configurationReceivers) { val result = try { - receiver.onConfigurationReceived(configJson, currency) + receiver.onConfigurationReceived(configJson, configResponse.currency) } catch (e: Exception) { Log.e(TAG, "Error handling configuration by ${receiver::class.java.simpleName}", e) context.getString(R.string.config_error_unknown) @@ -146,8 +153,8 @@ class ConfigManager( config = it saveConfig(it) } - this@ConfigManager.merchantConfig = merchantConfig.copy(currency = currency) - mConfigUpdateResult.postValue(ConfigUpdateResult.Success(currency)) + this@ConfigManager.merchantConfig = merchantConfig.copy(currency = configResponse.currency) + mConfigUpdateResult.postValue(ConfigUpdateResult.Success(configResponse.currency)) } fun forgetPassword() { -- cgit v1.2.3