Created
November 7, 2016 21:40
-
-
Save StabbyMcDuck/bb670e99ea5ffc07bd9080833af6fc53 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
private func fetch(credentials: Credentials, inEntityNamed: String, url: String, _ completion: @escaping (NSError?) -> Void) { | |
//print("Authentication: '\(credentials.user)' '\(credentials.password)'") | |
Alamofire | |
.request(url) | |
.authenticate(user: credentials.user, password: credentials.password) | |
.responseData { response in | |
let statusCode = response.response?.statusCode | |
if 200 <= statusCode! && statusCode! <= 299 { | |
let jsonResult = Request.serializeResponseJSON(options: JSONSerialization.ReadingOptions.allowFragments, response: response.response, data: response.data, error: nil) | |
switch jsonResult { | |
case .success(let json): | |
let changes = json as! [[String: Any]] | |
Sync.changes(changes, inEntityNamed: inEntityNamed, dataStack: self.dataStack) { error in | |
completion(error) | |
} | |
case .failure(let error): | |
print(error) | |
} | |
} else if (statusCode! == 401) { | |
print("Authentication error") | |
} else { | |
print("Error (HTTP Status Code \(statusCode)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment