This file contains hidden or 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 User: Equatable { | |
| let firstName: String | |
| let lastName: String | |
| } | |
| struct Contact: Equatable { | |
| let user: User | |
| let isConnected: Boolean | |
| } |
This file contains hidden or 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
| func fetch<Model: Codable> (fromRoute route: Route<Model>, then: @escaping (Result<Model>) -> Void) { | |
| // make sure that the endpoint path is a valid URL | |
| guard let url = URL(string: self.baseURL+route.endpoint) else { | |
| then(.failure(NSError(domain: "warpfactor.io", code: 500))) | |
| return | |
| } | |
| Alamofire | |
| .request(url, method: route.httpMethod) |
This file contains hidden or 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 Route<Model> { | |
| let endpoint: String | |
| let httpMethod: HTTPMethod | |
| } | |
| struct Routes { | |
| static let allBreeds = Route<Breeds>(endpoint: "breeds/list/all", httpMethod: HTTPMethod.post) | |
| static let beagles = Route<Beagles>(endpoint: "breed/beagle/images", httpMethod: HTTPMethod.get) | |
| } |
This file contains hidden or 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
| networkService.fetch(fromRoute: Routes.allBreeds) { (result) in | |
| switch result { | |
| case .success(let model): | |
| print (model) | |
| case .failure(let error): | |
| print (error) | |
| } | |
| } |
This file contains hidden or 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
| func fetch<Model: Codable> (fromRoute route: Route<Model>, | |
| then: @escaping (Result<Model>) -> Void) { |
This file contains hidden or 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
| func fetch<Model: Codable> (fromRoute route: Routes, | |
| then: @escaping (Result<Model>) -> Void) { |
This file contains hidden or 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 Route<Model> { | |
| let endpoint: String | |
| } | |
| struct Routes { | |
| static let allBreeds = Route<Breeds>(endpoint: "breeds/list/all") | |
| static let beagles = Route<Beagles>(endpoint: "breed/beagle/images") | |
| } |
This file contains hidden or 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
| networkService.fetch(fromRoute: Routes.allBreeds) { (result: Result<Beagles>) in | |
| switch result { | |
| case .success(let model): | |
| print (model) | |
| case .failure(let error): | |
| print (error) | |
| } | |
| } |
This file contains hidden or 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
| let networkService = NetworkService(withBaseURL: "https://dog.ceo/api/") | |
| networkService.fetch(fromRoute: Routes.allBreeds) { (result: Result<Breeds>) in | |
| switch result { | |
| case .success(let model): | |
| print (model) | |
| case .failure(let error): | |
| print (error) | |
| } | |
| } |
This file contains hidden or 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
| final class NetworkService { | |
| let baseURL: String | |
| init(withBaseURL baseURL: String) { | |
| self.baseURL = baseURL | |
| } | |
| func fetch<Model: Codable> (fromRoute route: Routes, | |
| then: @escaping (Result<Model>) -> Void) { |