To work with WEB resources, create the HHFWWeb 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 HHFWWeb application includes one screen, the UITextView text view, and UIButton buttons:
GET web request. GET request to get WEB resource data.
POST web request. POST request to get WEB resource data.
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:
Click the GET Web Request button.
As a result, a GET request to WEB resource is initialized.
Click the POST Web Request button.
As a result, a POST request to WEB resource is initialized.
Application code:
import UIKit
class ViewController: UIViewController {
// Determine output variables
@IBOutlet weak var resultTextView: UITextView!
@IBOutlet weak var getRequestButton: UIButton!
@IBOutlet weak var postRequestButton: 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 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)
}
}
// 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 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")
}
}
}
// Perform the 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)
}
}
}
}
// 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.getRequestButton.isEnabled = false
self.postRequestButton.isEnabled = false
}
private func activateButtons() {
self.getRequestButton.isEnabled = true
self.postRequestButton.isEnabled = true
}
}
See also: