Example of Working with WEB Resources

To work with WEB resources, create the HHFWWeb 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 HHFWWeb application includes one screen, the UITextView text view, and UIButton buttons:

NOTE. When the application is initialized, authentication is executed, the buttons are not available. If the authentication is executed successfully, the buttons are available, and the text box displays the text "Authentication and resources load success".

To execute the example:

  1. Click the GET Web Request button.

As a result, a GET request to WEB resource is initialized.

  1. Click the POST Web Request button.

As a result, a POST request to WEB resource is initialized.

Application code:

import UIKit
 
class ViewController: UIViewController {
 
    //MARK: - Outlets
    @IBOutlet weak var resultTextView: UITextView!
    @IBOutlet weak var getRequestButton: UIButton!
    @IBOutlet weak var postRequestButton: 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 getRequestPressed(_ sender: UIButton) {
        self.performGetRequest { (jsonResult) in
            self.resultTextView.text = String(format: "%@", jsonResult)
        }
    }
 
    @IBAction func postRequestPressed(_ sender: UIButton) {
        self.performPostRequest { (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 performGetRequest(completion: @escaping (NSDictionary)->()) {
        let resourceName = "yandex"
        let requestCallParams = RequestCallParams(defaultProperty: ())
        HHFWController.sharedInstance().request(resourceName, requestCallParams: requestCallParams) { (jsonResult) in
            if let jsonDict = jsonResult as? NSDictionary,
                let status = jsonDict["status"] as? String, status == "ok" {
                print("GET request success")
                completion(jsonDict)
            } else {
                print("GET request error")
            }
        }
    }
 
    //MARK: - HHFW - Post request
    private func performPostRequest(completion: @escaping (NSDictionary)->()) {
        let resourceName = "yandex"
        let requestCallParams = RequestCallParams(defaultProperty: ())
        requestCallParams?.headers = ["Content-Type": "application/json", "Accept": "application/json"]
        requestCallParams?.data = "{}"
        HHFWController.sharedInstance().request(resourceName, requestCallParams: requestCallParams) { (jsonResult) in
            if let jsonDict = jsonResult as? NSDictionary,
                let status = jsonDict["status"] as? String, status == "ok"{
                print("POST request success")
                completion(jsonDict)
            } else {
                print("POST request error")
                if let jsonDict = jsonResult as? NSDictionary {
                    self.resultTextView.text = String(format: "%@", jsonDict)
                }
            }
        }
    }
 
    //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.getRequestButton.isEnabled = false
        self.postRequestButton.isEnabled = false
    }
 
    private func activateButtons() {
        self.getRequestButton.isEnabled = true
        self.postRequestButton.isEnabled = true
    }
 }

See also:

Examples of iOS Framework Use