/ Caffinator / NetworkManager.swift
NetworkManager.swift
1 // 2 // NetworkManager.swift 3 // Caffinator 4 // 5 // Created by Mikael Mantis on 3/19/17. 6 // Copyright © 2017 AppRoar Studios. All rights reserved. 7 // 8 9 import Foundation 10 11 class NetworkManager { 12 13 static var locations = [Location]() 14 15 static func load(closure: @escaping (_ response: Any?) -> Void) { 16 //print ("test") 17 let defaultSession = URLSession(configuration: URLSessionConfiguration.default) 18 19 var dataTask: URLSessionDataTask? 20 21 if dataTask != nil { 22 dataTask?.cancel() 23 } 24 25 let url = URL(string: "https://gist.githubusercontent.com/mantism/ba4bd836351f90881fc0c08014c2eaf8/raw/5aa628ea6c78c3abeff192429ec370a37b267e19/places.json") 26 27 dataTask = defaultSession.dataTask(with: url!, completionHandler: { 28 data, response, error in 29 if error != nil { 30 31 print(error!.localizedDescription) 32 closure(nil) 33 34 } else if let httpResponse = response as? HTTPURLResponse { 35 36 if httpResponse.statusCode == 200 { 37 38 if let locationsData = data { 39 let locations = Location.dataToLocation(locationsData) 40 closure(locations) 41 } else { 42 closure(nil) 43 } 44 45 } 46 } else { 47 closure(nil) 48 } 49 50 }) 51 52 dataTask?.resume() 53 54 } 55 56 }