To work with Foresight Analytics Platform create the HHFWFap application, which uses framework methods:
Method name | Brief description |
initWithCredentials (_: host: environment: project: application: device:) | The method initializes the framework. |
auth (_: password:) | The method authenticates the user by login and password. |
openBase (_: key:) | The method opens connection with database at the specified path. |
resources (_: handler:) | The method gets schemes of available resources and automatically creates corresponding tables in the database. |
request (_: requestCallParams: handler:) | The method sends a uniform request to resource without loading to database. |
Auxiliary methods:
Method name | Brief description |
getURL (for:) | The method returns URL for the file with the specified name in the Documents folder. Type of returned data: URL |
The HHFWFap application includes one screen, the UITextView text view, and the UIButton button:
Connect FAP. Get data of Foresight Analytics Platform.
NOTE. When the application is initialized, authentication is executed, the button is not available. If the authentication is executed successfully, the button is available, and the text box displays the text "Authentication and resources load success".
To execute the example, click the Connect FAP button.
Clicking the button sends the POST request to Foresight Analytics Platform, and the response is returned from mobile platform server.
Application code:
import UIKit
class ViewController: UIViewController {
//MARK: - Outlets
@IBOutlet weak var resultTextView: UITextView!
@IBOutlet weak var connectFapButton: UIButton!
//MARK: - View life cycle
override func viewDidLoad() {
super.viewDidLoad()
self.deactivateButtons()
self.initializeFramework()
self.auth {
let databaseName = "database.sqlite"
if self.initializeDatabase(databaseName) {
self.loadResourcesScheme(databaseName) {
self.activateButtons()
self.resultTextView.text = "Authentication and load resources success"
}
}
}
}
//MARK: - Actions
@IBAction func connectFapPressed(_ sender: UIButton) {
self.connectFap { (jsonResult) in
self.resultTextView.text = String(format: "%@", jsonResult)
}
}
//MARK: - HHFW - Framework initialization
private func initializeFramework() {
let apiVersion: String = "v1"
let host: String = "http://testmasterfmp.fsight.cloud/"
let environment: String = "Leonid_environment"
let project: String = "Leonid_project"
let application: String = "app"
let device: String = (UIDevice.current.identifierForVendor?.uuidString)!
HHFWController.sharedInstance().initWithCredentials(
apiVersion,
host: host,
environment: environment,
project: project,
application: application,
device: device
)
}
//MARK: - HHFW - Authentication
private func auth(completion: @escaping ()->()) {
let username: String = "Leonid"
let password: String = "123123"
HHFWController.sharedInstance().auth(username, password: password){ (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Auth success")
completion()
} else {
print("Auth error")
}
}
}
//MARK: - HHFW - Open/create database
private func initializeDatabase(_ databaseName: String) -> Bool {
let fullDatabaseURL = self.getURL(for: databaseName)
return HHFWController.sharedInstance().openBase(fullDatabaseURL.path, key: "")
}
//MARK: - HHFW - Load resource's scheme
private func loadResourcesScheme(_ databaseName: String, completion: @escaping ()->()) {
HHFWController.sharedInstance().resources(databaseName) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Load resources success")
completion()
} else {
print("Load resources error")
}
}
}
//MARK: - HHFW - Get request
private func connectFap(completion: @escaping (NSDictionary)->()) {
let resourceName = "Fap"
let requestCallParams = RequestCallParams(defaultProperty: ())
requestCallParams?.headers = ["Content-Type": "application/json",
"Accept": "application/json",
"Content-Encoding": "utf-8"]
requestCallParams?.data = "{\"OpenMetabase\":{\"tDef\":{\"id\":\"TEST_INV_X\"},\"tCreds\":{\"user\":{\"id\":\"TEST_INV_X\"},\"pass\":\"TEST_INV_X\"},\"tArg\": \"\"}}"
HHFWController.sharedInstance().request(resourceName, requestCallParams: requestCallParams) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Connect FAP success")
completion(jsonDict)
} else {
print("Connect FAP error")
}
}
}
//MARK: - Helper methods
private func getURL(for fileName: String) -> URL {
let paths: [URL] = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let fileURL: URL = paths[0].appendingPathComponent(fileName)
return fileURL
}
//MARK: - View methods
private func deactivateButtons() {
self.connectFapButton.isEnabled = false
}
private func activateButtons() {
self.connectFapButton.isEnabled = true
}
}
See also: