Skip to content

Instantly share code, notes, and snippets.

@AAlfare
Created February 25, 2016 09:11
Show Gist options
  • Save AAlfare/2d7a75b6462035c687ad to your computer and use it in GitHub Desktop.
Save AAlfare/2d7a75b6462035c687ad to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
// From That Thing in Swift
// https://thatthinginswift.com
import UIKit
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
class ClientAPI {
func login(email: String, password: String, completion: (success: Bool, message: String?) -> ()) {
let loginObject = ["email": email, "password": password]
post(clientURLRequest("auth/local", params: loginObject)) { (success, object) -> () in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
if success {
completion(success: true, message: nil)
} else {
var message = "there was an error"
if let object = object, let passedMessage = object["message"] as? String {
message = passedMessage
}
completion(success: true, message: message)
}
})
}
}
// MARK: private composition methods
private func post(request: NSMutableURLRequest, completion: (success: Bool, object: AnyObject?) -> ()) {
dataTask(request, method: "POST", completion: completion)
}
private func put(request: NSMutableURLRequest, completion: (success: Bool, object: AnyObject?) -> ()) {
dataTask(request, method: "PUT", completion: completion)
}
private func get(request: NSMutableURLRequest, completion: (success: Bool, object: AnyObject?) -> ()) {
dataTask(request, method: "GET", completion: completion)
}
private func dataTask(request: NSMutableURLRequest, method: String, completion: (success: Bool, object: AnyObject?) -> ()) {
request.HTTPMethod = method
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
session.dataTaskWithRequest(request) { (data, response, error) -> Void in
if let data = data {
let json = try? NSJSONSerialization.JSONObjectWithData(data, options: [])
if let response = response as? NSHTTPURLResponse where 200...299 ~= response.statusCode {
completion(success: true, object: json)
} else {
completion(success: false, object: json)
}
}
}.resume()
}
private func clientURLRequest(path: String, params: Dictionary<String, AnyObject>? = nil) -> NSMutableURLRequest {
let request = NSMutableURLRequest(URL: NSURL(string: "http://api.example.com/"+path)!)
if let params = params {
var paramString = ""
for (key, value) in params {
let escapedKey = key.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
let escapedValue = value.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
paramString += "\(escapedKey)=\(escapedValue)&"
}
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.HTTPBody = paramString.dataUsingEncoding(NSUTF8StringEncoding)
}
return request
}
}
let client = ClientAPI()
client.login("[email protected]", password: "password") { (success, message) -> () in
if success {
print("logged in successfully!")
} else {
print("there was an error:", message)
}
XCPlaygroundPage.currentPage.finishExecution()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment