Last active
March 20, 2018 18:23
-
-
Save projectxcappe/27d2680201c6b5175d5ce62e3e4e9bc7 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
class func getHomeScreenPlaceList(completion: @escaping ((Result<VenueData>) -> Void)) { | |
guard let url = URL(string: "\(URL_STRING)/places") else { return } | |
let defaults = UserDefaults.standard | |
let token = defaults.object(forKey: "token") as? String | |
let tokenHeaderString = "Token \(token)" | |
var request = URLRequest(url: url) | |
request.httpMethod = "GET" | |
request.addValue("application/json", forHTTPHeaderField: "Content-Type") | |
request.addValue("application/json", forHTTPHeaderField: "Accept") | |
request.addValue(tokenHeaderString, forHTTPHeaderField: "Authentication") | |
let session = URLSession.shared | |
session.dataTask(with: request) { (data, response, error) in | |
if let data = data { | |
do { | |
let json = try JSONSerialization.jsonObject(with: data, options: []) | |
print(json) | |
} catch { | |
print(error) | |
} | |
} | |
DispatchQueue.main.async { | |
guard error == nil else { | |
completion(.failure(error!)) | |
return | |
} | |
guard let jsonData = data else { | |
let error = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey : "Data was not retrieved from request"]) as Error | |
completion(.failure(error)) | |
return | |
} | |
do { | |
let posts = try JSONDecoder().decode(VenueData.self, from: jsonData) | |
completion(.success(posts)) | |
} catch { | |
completion(.failure(error)) | |
} | |
} | |
}.resume() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment