Last active
October 26, 2015 14:21
-
-
Save bobgodwinx/68424f1b514067e85cd7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func makeRequest(success:(response:AnyObject)->(), failure:(error:CommunicatorError)->()) { | |
/** | |
Play with resp api take inspiration | |
https://grokswift.com/simple-rest-with-swift/ | |
*/ | |
let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration() | |
let session = NSURLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil) | |
let URL = NSURL(string: "http://jsonplaceholder.typicode.com/posts") | |
//let URL = NSURL(string: "http://private-anon-827bc57e0-andresbrun.apiary-mock.com/api/v3/search_suggestions") | |
let request = NSMutableURLRequest(URL: URL!) | |
//request.HTTPMethod = "GET" | |
request.HTTPMethod = "POST" | |
request.addValue("application/json", forHTTPHeaderField: "Content-Type") | |
request.addValue("e3e4db4432d6f93905f705e53a4898087920b6e71b0ffc4b5451924361c3e86b", forHTTPHeaderField: "access_token") | |
let parameters = ["title": "Bob", "body": "bar bob", "userId": "1"] | |
//let parameters: NSDictionary = ["q":"berlin"] | |
do { | |
let options = NSJSONWritingOptions() | |
let data = try NSJSONSerialization.dataWithJSONObject(parameters, options:options) | |
/** | |
you can reduce the syntax | |
*/ | |
//let data = try NSJSONSerialization.dataWithJSONObject(parameters, options: []) | |
/** | |
add body to request in NSData format | |
*/ | |
request.HTTPBody = data | |
} catch let error as NSError { | |
return failure(error: CommunicatorError.CommunicatorJSONSerializationError(errorMessage: error.description)) | |
} | |
let task = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> () in | |
guard error == nil else { | |
return failure(error: CommunicatorError.CommunicatorNetworkError) | |
} | |
let HTTPStatusCode = (response as! NSHTTPURLResponse).statusCode | |
/** | |
here need to implement optionSetType because in case of get you have 200 | |
*/ | |
guard HTTPStatusCode == 201 else { | |
switch HTTPStatusCode { | |
case 500: | |
return failure(error: CommunicatorError.CommunicatorHTTPStatusError(statusCode: HTTPStatusCode, errorMessages: "Internal server error ")) | |
default: | |
return failure(error: CommunicatorError.CommunicatorHTTPStatusError(statusCode: HTTPStatusCode, errorMessages: "Invalid server response")) | |
} | |
} | |
do { | |
let JSONResponse = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! [String: AnyObject] | |
return(success(response: JSONResponse)) | |
} catch let error as NSError { | |
return failure(error: CommunicatorError.CommunicatorJSONSerializationError(errorMessage: error.description)) | |
} | |
} | |
task.resume() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment