Created
January 29, 2019 11:22
-
-
Save charlieInDen/7262ea976d3ec245be826e4ddd264759 to your computer and use it in GitHub Desktop.
Restructuring code to write unit test cases - Prepare URLRequest, Parse Response
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
struct PointsOfInterestRequest: APIRequest { | |
enum RequestError: Error { | |
case invalidCoordinate | |
case unknown | |
} | |
func makeRequest(from coordinate: CLLocationCoordinate2D) throws -> URLRequest { | |
guard CLLocationCoordinate2DIsValid(coordinate) else { | |
throw RequestError.invalidCoordinate | |
} | |
var components = URLComponents(string: "https://example.com/locations")! | |
components.queryItems = [ | |
URLQueryItem(name: "lat", value: "\(coordinate.latitude)"), | |
URLQueryItem(name: "long", value: "\(coordinate.longitude)") | |
] | |
return URLRequest(url: components.url!) | |
} | |
func parseResponse(data: Data) throws -> [PointOfInterest] { | |
return try JSONDecoder().decode([PointOfInterest].self, from: data) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment