To work with Foresight Analytics Platform create the HHFWFap application, which uses framework methods:
Method name | Brief description |
initWithCredentials (_: host: environment: project: 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 a mobile platform server.
Application code:
import UIKit
class ViewController: UIViewController {
// Determine output variables
@IBOutlet weak var resultTextView: UITextView!
@IBOutlet weak var connectFapButton: UIButton!
// Redetermine method
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"
}
}
}
}
// Set method that will be executed on button click
@IBAction func connectFapPressed(_ sender: UIButton) {
self.connectFap { (jsonResult) in
self.resultTextView.text = String(format: "%@", jsonResult)
}
}
// Initialize framework
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 device: String = (UIDevice.current.identifierForVendor?.uuidString)!
HHFWController.sharedInstance().initWithCredentials(
apiVersion,
host: host,
environment: environment,
project: project,
device: device
)
}
// Authenticate
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")
}
}
}
// Open or create a database
private func initializeDatabase(_ databaseName: String) -> Bool {
let fullDatabaseURL = self.getURL(for: databaseName)
return HHFWController.sharedInstance().openBase(fullDatabaseURL.path, key: "")
}
// Load resources 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")
}
}
}
// Perform the 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")
}
}
}
// Call auxiliary classes
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
}
// Set methods to display application user interface elements
private func deactivateButtons() {
self.connectFapButton.isEnabled = false
}
private func activateButtons() {
self.connectFapButton.isEnabled = true
}
}
See also: