-
-
Save sohooo/1f3c199290c4ff313252 to your computer and use it in GitHub Desktop.
Networker made from enums
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
/** | |
paste this code into a playground to test it out | |
*/ | |
import Foundation | |
/** | |
*/ | |
protocol NetworkProtocol { | |
func go(completionHandler: (NetworkResponse) -> ()) | |
} | |
/** | |
The response types for my network. | |
Both values have appropriate associated types | |
*/ | |
enum NetworkResponse { | |
case Success(response: String) | |
case Error(error: String) | |
} | |
/** | |
the primary enum that handles the networking logic for my imaginary server | |
only deals with GET and POST, but can easily be adapted to include other | |
requests as well | |
*/ | |
enum Network: NetworkProtocol { | |
case GET(url: NSURL) | |
case POST(url: NSURL, data: String) | |
func go(completionHandler: (NetworkResponse) -> ()) { | |
switch self { | |
case .GET(let url): | |
let request = NSURLRequest(URL: url) | |
/// | |
/// in reality the code from here to the end of the case should be wrapped in an async network call | |
/// but for the purposes of this example, we skip that and go straight to the response | |
/// | |
let success = NetworkResponse.Success(response: "YAAAAAS YOU GOT DATA") | |
completionHandler(success) | |
/// let error = NetworkResponse.Error(error: "😬 uh oh! Your GET failed!") | |
/// completionHandler(error) | |
case .POST(let url, let data): | |
let request = NSURLRequest(URL: url) | |
/// | |
/// in reality the code from here to the end of the case should be wrapped in an async network call | |
/// but for the purposes of this example, we skip that and go straight to the response | |
/// | |
let success = NetworkResponse.Success(response: "`\(data)` POSTed like a boss!") | |
completionHandler(success) | |
/// let error = NetworkResponse.Error(error: "`\(data)`POST errored") | |
/// completionHandler(error) | |
} | |
} | |
} | |
/// | |
/// this obvi just a random url that's unsafely unwrapped for this example | |
/// PSA: Don't unsafely wrap things! | |
/// | |
let url = NSURL(string: "http://www.tester.com/api?foo=bar")! | |
/// | |
/// An example GET request | |
/// LOOK HOW EASY THIS IS TO READ AND UNDERSTAND | |
/// | |
let getRequest = Network.GET(url: url) | |
getRequest.go {(response) -> () in | |
switch response { | |
case .Success(let response): | |
println("GET Success: \(response)") | |
case .Error(let error): | |
println("GET error: \(error)") | |
} | |
} | |
/// | |
/// An example POST | |
/// LOOK!!! 👓 | |
/// | |
let postRequest = Network.POST(url: url, data: "Hello 🌎") | |
postRequest.go {(response) -> () in | |
switch response { | |
case .Success(let response): | |
println("POST success!: \(response)") | |
case .Error(let error): | |
println("POST error: \(error)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment