Created
June 26, 2019 14:53
-
-
Save jpcarreira/dc161250f35f866aa7bb044ac8351295 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
final class MockApiClient: Api { | |
private static let delay = 4 | |
private static var requestCount = 0 | |
func fetchMovies(completion: @escaping (Bool, FilmsData?) -> Void) { | |
switch MockApiClient.requestCount { | |
case 0: | |
DispatchQueue.main.async { | |
completion(false, nil) | |
} | |
default: | |
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(MockApiClient.delay)) { | |
let filePath = "films" | |
MockApiClient.loadJsonDataFromFile(filePath, completion: { data in | |
if let json = data { | |
do { | |
let films = try JSONDecoder().decode(FilmsData.self, from: json) | |
completion(true, films) | |
} | |
catch _ as NSError { | |
fatalError("Couldn't load data from \(filePath)") | |
} | |
} | |
}) | |
} | |
} | |
MockApiClient.requestCount += 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment