Created
December 22, 2019 12:46
-
-
Save BoD/e1119ac84e2a21bd246c1bbc7b9ed62f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// klibqonto demo app | |
// | |
// Created by Benoit Lubek on 04/11/2019. | |
// Copyright © 2019 BoD. All rights reserved. | |
// | |
import UIKit | |
import klibqonto | |
class ViewController: UIViewController { | |
var callbackClient: CallbackQontoClient | |
var blockingClient: BlockingQontoClient | |
required init?(coder aDecoder: NSCoder) { | |
let client = QontoClientCompanion().doNewInstance( | |
configuration: ClientConfiguration( | |
authentication: Authentication( | |
login: "qonto-test-9312", | |
secretKey: "f5f9539662ee" | |
), | |
httpConfiguration: HttpConfiguration( | |
loggingLevel: HttpLoggingLevel.none, | |
mockServerBaserUri: nil, | |
httpProxy: nil | |
), | |
userAgent: "klibqonto/iOS" | |
) | |
) | |
callbackClient = CallbackQontoClientKt.asCallbackQontoClient(client) | |
blockingClient = BlockingQontoClientKt.asBlockingQontoClient(client) // <- doesn't work :( | |
super.init(coder: aDecoder) | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let button = UIButton(type: .system) | |
button.frame = CGRect(x: 16, y: 48, width: 160, height: 48) | |
button.backgroundColor = UIColor.lightGray | |
button.tintColor = UIColor.black | |
button.addTarget(self, action: #selector(ViewController.onButtonClick(_:)), for: .touchUpInside) | |
button.setTitle("Hello, World", for: .normal) | |
view.addSubview(button) | |
} | |
@objc | |
func onButtonClick(_ sender: UIButton!) { | |
print("onButtonClick") | |
// getOrganizationBlocking() // <- doesn't work :( | |
getMembershipList() | |
getLabelList() | |
getOrganization() | |
} | |
private func getOrganizationBlocking() { | |
print("Hello, ") | |
print(self.blockingClient.organizations.getOrganization()) // <- freezes :( | |
print("World!") | |
} | |
private func getMembershipList() { | |
callbackClient.memberships.getMembershipList( | |
pagination: Pagination(pageIndex: 1, itemsPerPage: 35) | |
) { onResult in | |
onResult.fold(onSuccess: { membershipPage in | |
print("\nMembers:\n") | |
print(membershipPage.items.map({"\($0)"}).joined(separator: "\n")) | |
return nil | |
}, onFailure: { throwable in | |
print(throwable) | |
}) | |
} | |
} | |
private func getLabelList() { | |
callbackClient.labels.getLabelList( | |
pagination: Pagination(pageIndex: 1, itemsPerPage: 35) | |
) { onResult in | |
onResult.fold(onSuccess: { labelPage in | |
self.printLabels(labelList: labelPage.items) | |
return nil | |
}, onFailure: { throwable in | |
print(throwable) | |
}) | |
} | |
} | |
private func printLabels(labelList: Array<Label>) { | |
print("\nLabels:\n") | |
print(labelList.map({"\($0)"}).joined(separator: "\n")) | |
} | |
private func getOrganization() { | |
callbackClient.organizations.getOrganization { onResult in | |
onResult.fold(onSuccess: { organization in | |
print("\nOrganization:\n") | |
print(organization) | |
print(#"The organization has this slug: \#(organization.slug)"#) | |
self.getTransactionList(organization: organization) | |
return nil | |
}, onFailure: { throwable in | |
print(throwable) | |
}) | |
} | |
} | |
private func getTransactionList(organization: Organization) { | |
let bankAccountSlug = organization.bankAccounts[0].slug | |
callbackClient.transactions.getTransactionList( | |
bankAccountSlug: bankAccountSlug, | |
status: [TransactionStatus.completed, TransactionStatus.declined], | |
updatedDateRange: DateRange(from: date("2018-01-01"), to: date("2019-12-31")), | |
settledDateRange: nil, | |
sortField: QontoClientTransactionsSortField.updatedDate, | |
sortOrder: QontoClientTransactionsSortOrder.descending, | |
pagination: Pagination(pageIndex: 1, itemsPerPage: 10) | |
) { onResult in | |
onResult.fold(onSuccess: { transactionPage in | |
print("\nTransactions:\n") | |
print(transactionPage) | |
self.getAttachment(transactionList: transactionPage.items) | |
return nil | |
}, onFailure: { throwable in | |
print(throwable) | |
}) | |
} | |
} | |
private func getAttachment(transactionList: Array<Transaction>) { | |
// TODO | |
} | |
private func date(_ dateStr : String) -> Date { | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateFormat = "yyyy-MM-dd" | |
return dateFormatter.date(from: dateStr)! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment