gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-wallet-kotlin] branch master updated: First prototype for getting


From: gnunet
Subject: [taler-wallet-kotlin] branch master updated: First prototype for getting withdrawal information from bank
Date: Fri, 03 Jul 2020 21:52:26 +0200

This is an automated email from the git hooks/post-receive script.

torsten-grote pushed a commit to branch master
in repository wallet-kotlin.

The following commit(s) were added to refs/heads/master by this push:
     new c879154  First prototype for getting withdrawal information from bank
c879154 is described below

commit c8791545fe56f84a54d6700e833000ec379b7ee0
Author: Torsten Grote <t@grobox.de>
AuthorDate: Fri Jul 3 16:50:48 2020 -0300

    First prototype for getting withdrawal information from bank
    
    This commit adds coroutines and serialization features to all platforms.
    The Ktor library is used for making HTTP requests on all platforms.
    
    The unit test prototype for withdrawal information uses a mocked http
    client.
---
 build.gradle                                       | 31 +++++++++
 .../kotlin/net/taler/wallet/kotlin/runCoroutine.kt | 22 +++++++
 .../net/taler/wallet/kotlin/operations/Withdraw.kt | 74 +++++++++++++++++++++
 .../kotlin/net/taler/wallet/kotlin/TestUtils.kt    | 24 +++++++
 .../taler/wallet/kotlin/operations/WithdrawTest.kt | 76 ++++++++++++++++++++++
 .../kotlin/net/taler/wallet/kotlin/runCoroutine.kt | 23 +++++++
 .../kotlin/net/taler/wallet/kotlin/runCoroutine.kt | 22 +++++++
 7 files changed, 272 insertions(+)

diff --git a/build.gradle b/build.gradle
index 0caa564..342690d 100644
--- a/build.gradle
+++ b/build.gradle
@@ -16,6 +16,7 @@
 
 plugins {
     id 'org.jetbrains.kotlin.multiplatform' version '1.3.72'
+    id 'org.jetbrains.kotlin.plugin.serialization' version '1.3.72'
 }
 repositories {
     mavenCentral()
@@ -47,6 +48,8 @@ kotlin {
         }
     }
     sourceSets {
+        def coroutines_version = "1.3.7"
+        def ktor_version = "1.3.2"
         all {
             languageSettings {
                 useExperimentalAnnotation('kotlin.ExperimentalStdlibApi')
@@ -55,6 +58,9 @@ kotlin {
         commonMain {
             dependencies {
                 implementation kotlin('stdlib-common')
+                implementation 
"org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version"
+                implementation "io.ktor:ktor-client-core:$ktor_version"
+                implementation 
"io.ktor:ktor-client-serialization:$ktor_version"
                 implementation "com.soywiz.korlibs.klock:klock:1.11.12"
             }
         }
@@ -62,11 +68,15 @@ kotlin {
             dependencies {
                 implementation kotlin('test-common')
                 implementation kotlin('test-annotations-common')
+                api "io.ktor:ktor-client-mock:$ktor_version"
             }
         }
         androidMain {
             dependencies {
                 implementation kotlin('stdlib-jdk8')
+                implementation 
"org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
+                implementation "io.ktor:ktor-client-android:$ktor_version"
+                implementation 
"io.ktor:ktor-client-serialization-jvm:$ktor_version"
                 // TODO Android
 //                implementation 
"com.goterl.lazycode:lazysodium-android:4.1.1@aar"
                 implementation "com.goterl.lazycode:lazysodium-java:4.2.6"
@@ -77,11 +87,23 @@ kotlin {
             dependencies {
                 implementation kotlin('test')
                 implementation kotlin('test-junit')
+                api "io.ktor:ktor-client-mock-jvm:$ktor_version"
             }
         }
         jsMain {
             dependencies {
                 implementation kotlin('stdlib-js')
+                implementation 
"org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$coroutines_version"
+                implementation "io.ktor:ktor-client-js:$ktor_version"
+                implementation 
"io.ktor:ktor-client-serialization-js:$ktor_version"
+                // bug: https://github.com/ktorio/ktor/issues/1822
+                implementation npm('abort-controller', '3.0.0') // work-around 
for above bug
+                implementation npm('utf-8-validate', '5.0.2') // work-around 
for above bug
+                implementation npm('text-encoding', '0.7.0') // work-around 
for above bug
+                implementation npm('node-fetch', '2.6.0') // work-around for 
above bug
+                implementation npm('bufferutil', '4.0.1') // work-around for 
above bug
+                implementation npm('fs', '*') // work-around for above bug
+
                 implementation npm('tweetnacl', '1.0.3')
                 implementation npm('ed2curve', '0.3.0')
                 implementation npm('hash.js', '1.1.7')
@@ -90,11 +112,20 @@ kotlin {
         jsTest {
             dependencies {
                 implementation kotlin('test-js')
+                api "io.ktor:ktor-client-mock-js:$ktor_version"
             }
         }
         linuxMain {
+            dependencies {
+                implementation 
"org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutines_version"
+                implementation "io.ktor:ktor-client-curl:$ktor_version"
+                
implementation("io.ktor:ktor-client-serialization-native:$ktor_version")
+            }
         }
         linuxTest {
+            dependencies {
+                api "io.ktor:ktor-client-mock-native:$ktor_version"
+            }
         }
     }
 }
diff --git a/src/androidTest/kotlin/net/taler/wallet/kotlin/runCoroutine.kt 
b/src/androidTest/kotlin/net/taler/wallet/kotlin/runCoroutine.kt
new file mode 100644
index 0000000..524da15
--- /dev/null
+++ b/src/androidTest/kotlin/net/taler/wallet/kotlin/runCoroutine.kt
@@ -0,0 +1,22 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2020 Taler Systems S.A.
+ *
+ * GNU Taler is free software; you can redistribute it and/or modify it under 
the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3, or (at your option) any later version.
+ *
+ * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
FOR
+ * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+ */
+
+package net.taler.wallet.kotlin
+
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.runBlocking
+
+actual fun runCoroutine(block: suspend (scope : CoroutineScope) -> Unit) = 
runBlocking { block(this) }
diff --git 
a/src/commonMain/kotlin/net/taler/wallet/kotlin/operations/Withdraw.kt 
b/src/commonMain/kotlin/net/taler/wallet/kotlin/operations/Withdraw.kt
new file mode 100644
index 0000000..3fc4321
--- /dev/null
+++ b/src/commonMain/kotlin/net/taler/wallet/kotlin/operations/Withdraw.kt
@@ -0,0 +1,74 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2020 Taler Systems S.A.
+ *
+ * GNU Taler is free software; you can redistribute it and/or modify it under 
the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3, or (at your option) any later version.
+ *
+ * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
FOR
+ * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+ */
+
+package net.taler.wallet.kotlin.operations
+import io.ktor.client.HttpClient
+import io.ktor.client.features.json.JsonFeature
+import io.ktor.client.features.json.serializer.KotlinxSerializer
+import io.ktor.client.request.get
+import kotlinx.serialization.SerialName
+import kotlinx.serialization.Serializable
+import net.taler.wallet.kotlin.Amount
+import net.taler.wallet.kotlin.TalerUri.parseWithdrawUri
+
+class Withdraw(
+    private val httpClient: HttpClient = HttpClient { install(JsonFeature) { 
serializer = KotlinxSerializer() } }
+) {
+
+    @Serializable
+    data class Response(
+        @SerialName("selection_done")
+        val selectionDone: Boolean,
+        @SerialName("transfer_done")
+        val transferDone: Boolean,
+        val amount: String, // TODO Amount
+        @SerialName("wire_types")
+        val wireTypes: List<String>,
+        @SerialName("sender_wire")
+        val senderWire: String?,
+        @SerialName("suggested_exchange")
+        val suggestedExchange: String?,
+        @SerialName("confirm_transfer_url")
+        val confirmTransferUrl: String?
+    )
+
+    data class BankDetails(
+        val selectionDone: Boolean,
+        val transferDone: Boolean,
+        val amount: Amount,
+        val senderWire: String?,
+        val suggestedExchange: String?,
+        val confirmTransferUrl: String?,
+        val wireTypes: List<String>,
+        val extractedStatusUrl: String
+    )
+
+    suspend fun getBankInfo(talerWithdrawUri: String): BankDetails {
+        val url = parseWithdrawUri(talerWithdrawUri) ?: throw Error("Can't 
parse URI $talerWithdrawUri")
+        val response: Response = httpClient.get(url)
+        return BankDetails(
+            amount = Amount.fromJSONString(response.amount),
+            confirmTransferUrl = response.confirmTransferUrl,
+            extractedStatusUrl = url,
+            selectionDone = response.selectionDone,
+            senderWire = response.senderWire,
+            suggestedExchange = response.suggestedExchange,
+            transferDone = response.transferDone,
+            wireTypes = response.wireTypes
+        )
+    }
+
+}
diff --git a/src/commonTest/kotlin/net/taler/wallet/kotlin/TestUtils.kt 
b/src/commonTest/kotlin/net/taler/wallet/kotlin/TestUtils.kt
new file mode 100644
index 0000000..ec12556
--- /dev/null
+++ b/src/commonTest/kotlin/net/taler/wallet/kotlin/TestUtils.kt
@@ -0,0 +1,24 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2020 Taler Systems S.A.
+ *
+ * GNU Taler is free software; you can redistribute it and/or modify it under 
the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3, or (at your option) any later version.
+ *
+ * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
FOR
+ * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+ */
+
+package net.taler.wallet.kotlin
+
+import kotlinx.coroutines.CoroutineScope
+
+/**
+ * Workaround to use suspending functions in unit tests
+ */
+expect fun runCoroutine(block: suspend (scope : CoroutineScope) -> Unit)
diff --git 
a/src/commonTest/kotlin/net/taler/wallet/kotlin/operations/WithdrawTest.kt 
b/src/commonTest/kotlin/net/taler/wallet/kotlin/operations/WithdrawTest.kt
new file mode 100644
index 0000000..83c55a6
--- /dev/null
+++ b/src/commonTest/kotlin/net/taler/wallet/kotlin/operations/WithdrawTest.kt
@@ -0,0 +1,76 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2020 Taler Systems S.A.
+ *
+ * GNU Taler is free software; you can redistribute it and/or modify it under 
the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3, or (at your option) any later version.
+ *
+ * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
FOR
+ * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+ */
+
+package net.taler.wallet.kotlin.operations
+
+import io.ktor.client.HttpClient
+import io.ktor.client.engine.mock.MockEngine
+import io.ktor.client.engine.mock.MockEngineConfig
+import io.ktor.client.engine.mock.respond
+import io.ktor.client.features.json.JsonFeature
+import io.ktor.http.ContentType
+import io.ktor.http.headersOf
+import net.taler.wallet.kotlin.Amount
+import net.taler.wallet.kotlin.operations.Withdraw.BankDetails
+import net.taler.wallet.kotlin.runCoroutine
+import kotlin.test.Test
+import kotlin.test.assertEquals
+
+internal class WithdrawTest {
+
+    private val httpClient = HttpClient(MockEngine) {
+        install(JsonFeature)
+        engine {
+            addHandler { error("No test handler added") }
+        }
+    }
+    private val httpConfig = httpClient.engineConfig as MockEngineConfig
+    private val withdraw = Withdraw(httpClient)
+
+    @Test
+    fun getBankWithdrawalInfo() {
+        val bankDetails = BankDetails(
+            amount = Amount.fromJSONString("TESTKUDOS:5"),
+            confirmTransferUrl = 
"https://bank.test.taler.net/confirm-withdrawal/9b51c1dd-db41-4b5f-97d9-1071d5dd8091";,
+            extractedStatusUrl = 
"https://bank.test.taler.net/api/withdraw-operation/9b51c1dd-db41-4b5f-97d9-1071d5dd8091";,
+            selectionDone = false,
+            senderWire = "payto://x-taler-bank/bank.test.taler.net/test",
+            suggestedExchange = "https://exchange.test.taler.net/";,
+            transferDone = false,
+            wireTypes = listOf("x-taler-bank")
+        )
+        val json = """{
+            "selection_done": ${bankDetails.selectionDone},
+            "transfer_done": ${bankDetails.transferDone},
+            "amount": "${bankDetails.amount.toJSONString()}",
+            "wire_types": ["${bankDetails.wireTypes[0]}"],
+            "sender_wire": "${bankDetails.senderWire}",
+            "suggested_exchange": "${bankDetails.suggestedExchange}",
+            "confirm_transfer_url": "${bankDetails.confirmTransferUrl}"
+        }""".trimIndent()
+        httpConfig.requestHandlers.removeAt(0)
+        httpConfig.requestHandlers.add {
+            val headers = headersOf("Content-Type" to 
listOf(ContentType.Application.Json.toString()))
+            respond(json, headers = headers)
+        }
+        runCoroutine {
+            val details =
+                
withdraw.getBankInfo("taler://withdraw/bank.test.taler.net/-/9b51c1dd-db41-4b5f-97d9-1071d5dd8091")
+            assertEquals(bankDetails, details)
+        }
+    }
+
+}
diff --git a/src/jsTest/kotlin/net/taler/wallet/kotlin/runCoroutine.kt 
b/src/jsTest/kotlin/net/taler/wallet/kotlin/runCoroutine.kt
new file mode 100644
index 0000000..da5a183
--- /dev/null
+++ b/src/jsTest/kotlin/net/taler/wallet/kotlin/runCoroutine.kt
@@ -0,0 +1,23 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2020 Taler Systems S.A.
+ *
+ * GNU Taler is free software; you can redistribute it and/or modify it under 
the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3, or (at your option) any later version.
+ *
+ * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
FOR
+ * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+ */
+
+package net.taler.wallet.kotlin
+
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.promise
+
+actual fun runCoroutine(block: suspend (scope : CoroutineScope) -> Unit): 
dynamic = GlobalScope.promise { block(this) }
diff --git a/src/linuxTest/kotlin/net/taler/wallet/kotlin/runCoroutine.kt 
b/src/linuxTest/kotlin/net/taler/wallet/kotlin/runCoroutine.kt
new file mode 100644
index 0000000..524da15
--- /dev/null
+++ b/src/linuxTest/kotlin/net/taler/wallet/kotlin/runCoroutine.kt
@@ -0,0 +1,22 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2020 Taler Systems S.A.
+ *
+ * GNU Taler is free software; you can redistribute it and/or modify it under 
the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3, or (at your option) any later version.
+ *
+ * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
FOR
+ * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+ */
+
+package net.taler.wallet.kotlin
+
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.runBlocking
+
+actual fun runCoroutine(block: suspend (scope : CoroutineScope) -> Unit) = 
runBlocking { block(this) }

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]