Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Tiny Networking in Swift

Tiny Networking in Swift

Chris Eidhof | @chriseidhof

December 09, 2014
Tweet

More Decks by Chris Eidhof | @chriseidhof

Other Decks in Technology

Transcript

  1. $ curl "http://localhost:3000/api/users/sign_in" \ -d "{\"email\": \"[email protected]\", \"password\": \"testtes\"}" \

    --header "Content-Type:application/json" \ {"message":"Error with your login or password"}~
  2. struct Resource<A> { let path: String let method : Method

    let headers : [String:String] let requestBody: NSData? let parse: NSData -> A? }
  3. let parse : NSData -> String? = { data in

    if let jsonDict = decodeJSON(data) { return jsonDict["auth_token"] as? String } return nil }
  4. let path = "api/users/sign_in" let headers = ["Content-Type": "application/json", "Accept":

    "application/json"] let resource = Resource(path: path, method: .POST, requestBody: jsonBody, headers: headers, parse: parse)
  5. let email = "[email protected]" let password = "testtest" let baseURL

    = NSURL(string:"http://localhost:3000")! let resource = login(email, password) apiRequest(baseURL, resource, { println("failure") }, { authorizationToken in println("Auth token \(authorizationToken)") })