taler-ios

iOS apps for GNU Taler (wallet)
Log | Files | Refs | README | LICENSE

Model+Settings.swift (5270B)


      1 /*
      2  * This file is part of GNU Taler, ©2022-25 Taler Systems S.A.
      3  * See LICENSE.md
      4  */
      5 /**
      6  * @author Marc Stibane
      7  */
      8 import Foundation
      9 import taler_swift
     10 import SymLog
     11 
     12 fileprivate let MERCHANTAUTHTOKEN = "secret-token:sandbox"
     13 
     14 // MARK: -
     15 /// A request to add a test balance to the wallet.
     16 fileprivate struct WithdrawTestBalanceRequest: WalletBackendFormattedRequest {
     17     struct Response: Decodable {}   // no result - getting no error back means success
     18     var operation: String { "withdrawTestBalance" }
     19     func args() -> Args { Args(amount: amount,
     20                    corebankApiBaseUrl: bankBaseUrl,
     21                     useForeignAccount: true,
     22                       exchangeBaseUrl: exchangeBaseUrl,
     23                         progressToken: operation + bankBaseUrl)
     24                         }
     25 
     26     var amount: Amount
     27     var bankBaseUrl: String
     28     var exchangeBaseUrl: String
     29 
     30     struct Args: Encodable {
     31         var amount: Amount
     32 //        var bankBaseUrl: String                         // <= this should be the correct parameter name
     33         var corebankApiBaseUrl: String                // <= but this is used by wallet-core
     34         var useForeignAccount: Bool
     35         var exchangeBaseUrl: String
     36         var progressToken: String?
     37     }
     38 }
     39 extension WalletModel {
     40     nonisolated func loadTestKudos(_ test: Int, amount: Amount, viewHandles: Bool = false)
     41       async throws {
     42         let request = WithdrawTestBalanceRequest(amount: amount,
     43                                             bankBaseUrl: test == 2 ? HEADBANK
     44                                                        : test == 1 ? TESTBANK : DEMOBANK,
     45                                         exchangeBaseUrl: test == 2 ? HEADEXCHANGE
     46                                                        : test == 1 ? TESTEXCHANGE : DEMOEXCHANGE)
     47           let controller = Controller.shared
     48           controller.progressOperation = request.operation
     49           controller.progressToken = request.operation + request.bankBaseUrl
     50         _ = try await sendRequest(request, viewHandles: viewHandles)
     51     }
     52 } // loadTestKudos
     53 // MARK: -
     54 /// A request to add a test balance to the wallet.
     55 fileprivate struct RunIntegrationTest: WalletBackendFormattedRequest {
     56     struct Response: Decodable {}   // no result - getting no error back means success
     57     var operation: String { newVersion ? "runIntegrationTestV2" : "runIntegrationTest" }
     58     func args() -> Args { Args(exchangeBaseUrl: exchangeBaseUrl,
     59                             corebankApiBaseUrl: bankBaseUrl,
     60                                merchantBaseUrl: merchantBaseUrl,
     61                              merchantAuthToken: merchantAuthToken,
     62                               amountToWithdraw: amountToWithdraw,
     63                                  amountToSpend: amountToSpend)
     64                         }
     65 
     66     let newVersion: Bool
     67 
     68     var exchangeBaseUrl: String
     69     var bankBaseUrl: String
     70     var merchantBaseUrl: String
     71     var merchantAuthToken: String
     72     var amountToWithdraw: Amount
     73     var amountToSpend: Amount
     74 
     75     struct Args: Encodable {
     76         var exchangeBaseUrl: String
     77 //        var bankBaseUrl: String                         // <= this should be the correct parameter name
     78         var corebankApiBaseUrl: String                // <= but this is used by wallet-core
     79         var merchantBaseUrl: String
     80         var merchantAuthToken: String
     81         var amountToWithdraw: Amount
     82         var amountToSpend: Amount
     83     }
     84 }
     85 extension WalletModel {
     86     nonisolated  func runIntegrationTest(newVersion: Bool, test: Bool, viewHandles: Bool = false)
     87       async throws {
     88         let amountW = Amount(currency: test ? TESTCURRENCY : DEMOCURRENCY, cent: 300)
     89         let amountS = Amount(currency: test ? TESTCURRENCY : DEMOCURRENCY, cent: 100)
     90         let request = RunIntegrationTest(newVersion: newVersion,
     91                                     exchangeBaseUrl: test ? TESTEXCHANGE : DEMOEXCHANGE,
     92                                         bankBaseUrl: (test ? TESTBANK : DEMOBANK),
     93                                     merchantBaseUrl: test ? TESTBACKEND : DEMOBACKEND,
     94                                   merchantAuthToken: MERCHANTAUTHTOKEN,
     95                                    amountToWithdraw: amountW,
     96                                       amountToSpend: amountS)
     97         _ = try await sendRequest(request, viewHandles: viewHandles)
     98     }
     99 } // runIntegrationTest
    100   // MARK: -
    101 /// A request to add a test balance to the wallet.
    102 fileprivate struct InfiniteTransactionLoop: WalletBackendFormattedRequest {
    103     struct Response: Decodable {}   // no result - getting no error back means success
    104     var operation: String { "testingInfiniteTransactionLoop" }
    105     func args() -> Args { Args(delayMs: delayMs,
    106                                shouldFetch: shouldFetch)
    107     }
    108 
    109     let delayMs: Int32
    110     let shouldFetch: Bool
    111 
    112 
    113     struct Args: Encodable {
    114         let delayMs: Int32
    115         let shouldFetch: Bool
    116     }
    117 }
    118 extension WalletModel {
    119     nonisolated func testingInfiniteTransaction(delayMs: Int32, shouldFetch: Bool, viewHandles: Bool = false)
    120       async throws {
    121         let request = InfiniteTransactionLoop(delayMs: delayMs, shouldFetch: shouldFetch)
    122         _ = try await sendRequest(request, viewHandles: viewHandles)
    123     }
    124 } // testingInfiniteTransaction