Last active
May 14, 2019 07:37
-
-
Save Mintri1199/384069113ad459b45d462baff61ab1e5 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
fileprivate func callKeywordApi() { | |
NetworkManager().newGetKeywords { (result) in | |
switch result { | |
case .success(let keywords): | |
self.listOfKeywords = keywords | |
case .failure(let error): | |
print(error.localizedDescription) | |
} | |
} | |
} |
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
func newGetKeywords(completion: @escaping (Result<[Keyword], NetworkReponse>) -> Void) { // Use Result type in our closure parameter | |
keywordRouter.request(.keywords) { (data, response, error) in | |
if error != nil { | |
print("Check your network connection") | |
return | |
} | |
if let response = response as? HTTPURLResponse { | |
let result = self.newHandleNetworkResponse(response) | |
switch result { | |
case .success: | |
guard let responseData = data else { completion(.failure(.noData)); return } | |
do { | |
let apiResponse = try JSONDecoder().decode([Keyword].self, from: responseData) | |
completion(.success(apiResponse)) | |
} catch { | |
completion(.failure(.unableToDecode)) | |
} | |
case .failure(let networkFailureError): | |
completion(.failure(networkFailureError)) | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment