Created
January 14, 2021 17:07
-
-
Save thepearl/a829d2cd529e362b31428bd39bae1fc1 to your computer and use it in GitHub Desktop.
Networking call
This file contains hidden or 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
| import UIKit | |
| class Service | |
| { | |
| let API_KEY = "k_0hpcd8sn" | |
| let endpoint = "https://imdb-api.com/en/API/SearchMovie/" | |
| static var sharedInstance = Service() | |
| private init(){} | |
| func fetchFilms(for keyWordSearch: String, completionHandler: @escaping ([Result]) -> Void) { | |
| guard let endpoint = URL(string: endpoint + API_KEY + "/" + keyWordSearch) else { return } | |
| let task = URLSession.shared.dataTask(with: endpoint, completionHandler: { (data, response, error) in | |
| if let error = error { | |
| print("Error with fetching films: \(error)") | |
| return | |
| } | |
| guard let httpResponse = response as? HTTPURLResponse, | |
| (200...299).contains(httpResponse.statusCode) else { | |
| print("Error with the response, unexpected status code: \(response)") | |
| return | |
| } | |
| if let data = data, | |
| let filmSummary = try? JSONDecoder().decode(Response.self, from: data) { | |
| completionHandler(filmSummary.results ?? []) | |
| } | |
| }) | |
| task.resume() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment