Skip to content

Instantly share code, notes, and snippets.

@piyush23dez
Last active December 22, 2019 08:20
Show Gist options
  • Save piyush23dez/6d835b43a99cd9adbd6eb5a4a98e14f8 to your computer and use it in GitHub Desktop.
Save piyush23dez/6d835b43a99cd9adbd6eb5a4a98e14f8 to your computer and use it in GitHub Desktop.
typealias CompletionBlock = (Data?) -> Void
protocol NetworkService {
func make(request: URLRequest, completionHandler: @escaping CompletionBlock)
}
extension URLSession: NetworkService {
func make(request: URLRequest, completionHandler: @escaping CompletionBlock) {
let task = self.dataTask(with: request) { data, _, _ in
completionHandler(data)
}
task.resume()
}
}
//create mocked network service
class MockedService: NetworkService {
func make(request: URLRequest, completionHandler: @escaping CompletionBlock) {
completionHandler("Mocked data response".data(using: .utf8))
}
}
class PilotService: NetworkService {
func make(request: URLRequest, completionHandler: @escaping CompletionBlock) {}
}
class TestService: NetworkService {
func make(request: URLRequest, completionHandler: @escaping CompletionBlock) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment