Skip to content

Instantly share code, notes, and snippets.

@StabbyMcDuck
Created November 7, 2016 21:40
Show Gist options
  • Save StabbyMcDuck/bb670e99ea5ffc07bd9080833af6fc53 to your computer and use it in GitHub Desktop.
Save StabbyMcDuck/bb670e99ea5ffc07bd9080833af6fc53 to your computer and use it in GitHub Desktop.
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