commit 3cb19f272b49399fd5283c0da6a5896e85ec220e
parent 3add557802b4a5c0a4707259176998d19a9ce7d1
Author: Marc Stibane <marc@taler.net>
Date: Fri, 17 Feb 2023 07:52:27 +0100
ported remaining sync wallet-core funcs to try await
Diffstat:
3 files changed, 48 insertions(+), 73 deletions(-)
diff --git a/TalerWallet1/Backend/WalletCore.swift b/TalerWallet1/Backend/WalletCore.swift
@@ -116,6 +116,7 @@ extension WalletCore {
symLog.log(payload)
Task {
do {
+ // automatically fetch balances after receiving these notifications
try await Controller.shared.balancesModel.fetchBalances()
} catch {
// TODO: show error
@@ -210,23 +211,6 @@ extension WalletCore {
completionHandler(id, nil, WalletCore.serializeRequestError());
}
}
-
- /// call this to send requests to wallet-core
- func sendFormattedRequest<T: WalletBackendFormattedRequest>
- (request: T, completionHandler: @escaping (T.Response?, WalletBackendResponseError?) -> Void)
- {
- let reqData = WalletBackendRequest(operation: request.operation(),
- args: AnyEncodable(request.args()))
- sendRequest(request: reqData) { (id: UInt, result: Data?, err: WalletBackendResponseError?) in
- guard let json = result else { completionHandler(nil, err); return }
- do {
- let decoded = try JSONDecoder().decode(T.Response.self, from: json)
- completionHandler(decoded, err)
- } catch {
- completionHandler(nil, WalletCore.parseResponseError())
- }
- }
- }
}
// MARK: - async / await function
extension WalletCore {
diff --git a/TalerWallet1/Model/ExchangeTestModel.swift b/TalerWallet1/Model/ExchangeTestModel.swift
@@ -16,6 +16,7 @@
import Foundation
import taler_swift
import SymLog
+fileprivate let ASYNCDELAY: UInt = 0 //set e.g to 6 or 9 seconds for debugging
fileprivate let EXCHANGEBASEURL = "https://exchange.demo.taler.net/"
fileprivate let BANKBASEURL = "https://bank.demo.taler.net/"
@@ -24,62 +25,38 @@ fileprivate let MERCHANTBASEURL = "https://backend.demo.taler.net/"
fileprivate let MERCHANTAUTHTOKEN = "secret-token:sandbox"
// MARK: -
-class ExchangeTestModel: ObservableObject {
- private let symLog = SymLogC(0)
-
- var walletCore: WalletCore
-
- @Published var loading: Bool = false
-
- init(walletCore: WalletCore) {
- self.walletCore = walletCore
- }
+class ExchangeTestModel: WalletModel {
}
// MARK: -
extension ExchangeTestModel {
- func loadTestKudos() {
- loading = true
-
- let amount = Amount(currency: "KUDOS", integer: 11, fraction: 0)
- let req = WalletBackendWithdrawTestBalance(amount: amount, bankBaseUrl: BANKBASEURL,
- exchangeBaseUrl: EXCHANGEBASEURL, bankAccessApiBaseUrl: BANKACCESSAPIBASEURL)
- symLog.log("sending: \(req)")
- walletCore.sendFormattedRequest(request: req) { response, err in
- DispatchQueue.main.async {
- self.loading = false
- if let res = response {
- // TODO: ?
- self.symLog.log("received: \(res)")
- } else {
- // TODO: Handle error
- }
- }
+ @MainActor func loadTestKudos() async throws {
+ do {
+ let amount = Amount(currency: "KUDOS", integer: 11, fraction: 0)
+ let request = WalletBackendWithdrawTestBalance(amount: amount,
+ bankBaseUrl: BANKBASEURL,
+ exchangeBaseUrl: EXCHANGEBASEURL,
+ bankAccessApiBaseUrl: BANKACCESSAPIBASEURL)
+ let response = try await sendRequest(request, ASYNCDELAY)
+ symLog?.log("received: \(response)")
+ } catch {
+ throw error
}
}
- func runIntegrationTest() {
- loading = true
-
- let amountW = Amount(currency: "KUDOS", integer: 3, fraction: 0)
- let amountS = Amount(currency: "KUDOS", integer: 1, fraction: 0)
- let req = WalletBackendRunIntegration(amountToWithdraw: amountW,
- amountToSpend: amountS,
- bankBaseUrl: BANKACCESSAPIBASEURL,
- exchangeBaseUrl: EXCHANGEBASEURL,
- merchantBaseUrl: MERCHANTBASEURL,
- merchantAuthToken: MERCHANTAUTHTOKEN
- )
- symLog.log("sending: \(req)")
- walletCore.sendFormattedRequest(request: req) { response, err in
- DispatchQueue.main.async {
- self.loading = false
- if let res = response {
- // TODO: ?
- self.symLog.log("received: \(res)")
- } else {
- // TODO: Handle error
- }
- }
+ @MainActor func runIntegrationTest() async throws {
+ do {
+ let amountW = Amount(currency: "KUDOS", integer: 3, fraction: 0)
+ let amountS = Amount(currency: "KUDOS", integer: 1, fraction: 0)
+ let request = WalletBackendRunIntegration(amountToWithdraw: amountW,
+ amountToSpend: amountS,
+ bankBaseUrl: BANKACCESSAPIBASEURL,
+ exchangeBaseUrl: EXCHANGEBASEURL,
+ merchantBaseUrl: MERCHANTBASEURL,
+ merchantAuthToken: MERCHANTAUTHTOKEN)
+ let response = try await sendRequest(request, ASYNCDELAY)
+ symLog?.log("received: \(response)")
+ } catch {
+ throw error
}
}
}
diff --git a/TalerWallet1/Views/Settings/SettingsView.swift b/TalerWallet1/Views/Settings/SettingsView.swift
@@ -66,9 +66,16 @@ struct SettingsView: View {
SettingsItem(name: "Withdraw KUDOS", description: "Get money for testing") {
Button("Withdraw") {
withDrawDisabled = true // don't run twice
- let testModel: ExchangeTestModel = ExchangeTestModel(walletCore: walletCore)
- symLog.log("Withdrawing ")
- testModel.loadTestKudos()
+ Task {
+ let testModel: ExchangeTestModel = ExchangeTestModel(walletCore: walletCore)
+ symLog.log("Withdrawing")
+ do {
+ try await testModel.loadTestKudos()
+ } catch {
+ // TODO: show error
+ symLog.log(error.localizedDescription)
+ }
+ }
}
.buttonStyle(.bordered)
.disabled(withDrawDisabled)
@@ -76,9 +83,16 @@ struct SettingsView: View {
SettingsItem(name: "Run Integration Test", description: "Check if wallet-core works") {
Button("Check") {
checkDisabled = true // don't run twice
- let testModel: ExchangeTestModel = ExchangeTestModel(walletCore: walletCore)
- symLog.log("running integration test ")
- testModel.runIntegrationTest()
+ Task {
+ let testModel: ExchangeTestModel = ExchangeTestModel(walletCore: walletCore)
+ symLog.log("running integration test")
+ do {
+ try await testModel.runIntegrationTest()
+ } catch {
+ // TODO: show error
+ symLog.log(error.localizedDescription)
+ }
+ }
}
.buttonStyle(.bordered)
.disabled(checkDisabled)