Unverified Commit 7cf799fc authored by Zach Knox's avatar Zach Knox
Browse files

network code for Alerts

parent 27e0f467
......@@ -54,4 +54,43 @@ class SRCTNetworkController: NSObject {
}
public static func performAlertsDownload(completion: @escaping (_ result: List<Alert>?) -> Void) {
let requestURL: NSURL = NSURL(string: "https://api.srct.gmu.edu/whatsopen/v2/alerts/?format=json")!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
let session = URLSession.shared
let task = session.dataTask(with: urlRequest as URLRequest) {
(data, response, error) -> Void in
if(error != nil) {
completion(nil)
return
}
else {
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
if let dataN = data {
if let json = try? JSONSerialization.jsonObject(with: dataN, options: []) as? [[String: Any]] {
// Map function to iterate through each JSON tree
let alerts = json!.map({ (json) -> Alert in
let alert = Alert()
let map = Map(mappingType: .fromJSON, JSON: json, toObject: true, context: alert, shouldIncludeNilValues: true)
alert.mapping(map: map)
return alert
})
// This is where completion is called
// Right after the array is done mapping all facility objects
completion(List(alerts))
}
}
}
}
}
task.resume()
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment