bhamm@mac.com

Data API and Swift 4 (examples)

Discussion created by bhamm@mac.com on May 22, 2018
Latest reply on May 30, 2018 by bhamm@mac.com

I took the Data API (v.17) for a spin this past weekend. Here are a couple examples for anyone who wants to see a working auth/token and query call in Swift 4. The other request types (create, edit, delete) are pretty similar, I don't need to post them all.

 

Obviously this is lousy error handling, and I'm just sending returned objects to the console. If you're a Swift dev, you know where to go from here (refactoring to pass `payload` as a parameter, for starters). Also note the URLs are intentionally broken so they'll display properly in the forum.

 

Hope this helps somebody.

 

 

/* auth */

func sessionToken() {

   

    let url = URL(string: "https ://[host]/fmi/data/v1/databases/[fileName]/sessions")!   // update as needed

   

    let headers = [

        "Authorization": "Basic [base64 user:pass]",   // ex. Basic Bdf8aE98Fe16FD6RmvZjN

        "Content-Type" : "application/json"

    ]

   

    var request = URLRequest(url: url)

    request.httpMethod = "POST"

    request.allHTTPHeaderFields = headers

   

    URLSession.shared.dataTask(with: request) { data, _, error in

       

        guard   let data  = data, error == nil,

                let json  = try? JSONSerialization.jsonObject(with: data) as! [String: Any],

                let resp  = json["response"] as? [String: Any],

                let token = resp["token"] as? String else {

               

                print("\n*** token sad\n")

                return

        }

       

        print("\n*** token: \(token)\n")   // disco!

       

    }.resume()

}

 

 

 

 

/* find request */

func apiRequest(with token: String) {    // token from previous call, class var, user defaults, wherever...

   

    let url = URL(string: "https ://[host]/fmi/data/v1/databases/[fileName]/layouts/[layoutName]/_find")!

   

    let headers = [

        "Authorization": "Bearer \(token)",    // note: Bearer, not Basic

        "Content-Type" : "application/json"

    ]

   

    let payload = [    //  or:[[p1],[p2]]  and:[[p1,p2]]

    "query": [

        ["firstName": "Brian"],

        ["firstName": "Katherine"]

    ]]

   

    // dict -> data

    guard   let body = try? JSONSerialization.data(withJSONObject: payload) else {

           

            print("\n*** serialize sad\n")

            return

    }

   

    var request = URLRequest(url: url)

    request.httpMethod = "POST"

    request.allHTTPHeaderFields = headers

    request.httpBody = body

   

    // task

    URLSession.shared.dataTask(with: request) { data, _, error in

       

        guard   let data = data, error == nil,

                let json = try? JSONSerialization.jsonObject(with: data) else {

               

                print("\n*** json sad\n")

                return

        }

       

        print(json)   // woot!

       

    }.resume()

}

Outcomes