Created
September 24, 2021 07:00
-
-
Save 13hoop/e81fdb3955341b3e076c840b12dd1f13 to your computer and use it in GitHub Desktop.
this is a mock API server demo
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 Server { | |
private init() {} | |
static let shared = Server.init() | |
private static let serverQueue = DispatchQueue.global(qos: .background) | |
func getCountries(completion: @escaping (Array<String>) -> Void) { | |
let countries = ["France", "Germany", "Spain", "Portugal"] | |
let delay = Int.random(in: 1..<4) | |
Self.serverQueue.asyncAfter(deadline: .now() + .seconds(delay)) { | |
completion(countries) | |
} | |
} | |
func getCapital(of country:String, completion: @escaping (String) -> Void) { | |
let capitals = ["France":"Paris", "Germany":"Berlin", "Spain":"Madrid", "Portugal":"Lisbon"] | |
let delay = Int.random(in: 1..<10) | |
Self.serverQueue.asyncAfter(deadline: .now() + .seconds(delay)) { | |
completion(capitals[country, default:"N/A"]) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
share from here