Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feighter09/178c48f622f2841cc99321657f80572f to your computer and use it in GitHub Desktop.
Save feighter09/178c48f622f2841cc99321657f80572f to your computer and use it in GitHub Desktop.
import Alamofire
import SwiftyJSON
protocol NetworkClientType {
func fetchUsername(callback: (String?, ErrorType?) -> Void)
func makeRequest(url: String,
params: [String : AnyObject],
callback: (JSON?, ErrorType?) -> Void)
}
struct NetworkClient: NetworkClientType {
func fetchUsername(callback: (String?, ErrorType?) -> Void)
{
let url = "http://httpbin.org/post"
let params = ["param": "feighter09"]
makeRequest(url, params: params) { json, error in
if let json = json,
let username = json["form"]["param"].string {
callback(username, nil)
}
else {
callback(nil, error)
}
}
}
func makeRequest(url: String,
params: [String : AnyObject],
callback: (JSON?, ErrorType?) -> Void)
{
request(.POST, url, parameters: params).response { _, _, data, error in
if let jsonData = data where error == nil {
let json = JSON(data: jsonData)
callback(json, nil)
}
else {
callback(nil, error)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment