Skip to content

Instantly share code, notes, and snippets.

@charlieInDen
Created January 29, 2019 11:22
Show Gist options
  • Save charlieInDen/7262ea976d3ec245be826e4ddd264759 to your computer and use it in GitHub Desktop.
Save charlieInDen/7262ea976d3ec245be826e4ddd264759 to your computer and use it in GitHub Desktop.
Restructuring code to write unit test cases - Prepare URLRequest, Parse Response
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