Created
August 25, 2018 00:40
-
-
Save carlosjac/d042f696a1131227bf078f4006fd56f4 to your computer and use it in GitHub Desktop.
VIP News
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
import Foundation | |
import PromisedFuture | |
class APIClient { | |
var http:HttpProtocol = HttpAlamofire() | |
func getNews() -> Future<[ListNewsUseCase.ListNews.SuccessResponse.NewModel]> { | |
return self.http.getArray(endpoint: "news") | |
} | |
func getLastVersion(platform: APPPlatform) -> Future<LastAppVersionModel> { | |
return self.http.getObject(endpoint: "last_app_version/" + platform.rawValue.description) | |
} | |
} |
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
import Foundation | |
import Alamofire | |
import PromisedFuture | |
class HttpAlamofire: HttpProtocol { | |
@discardableResult | |
func getArray<T:Decodable>(endpoint: String) -> Future<[T]> { | |
let decoder: JSONDecoder = JSONDecoder() | |
return Future(operation: { completion in | |
Alamofire.request(K.ProductionServer.baseURL + endpoint).responseJSONDecodable(decoder: decoder, completionHandler: { (response: DataResponse<[T]>) in | |
switch response.result { | |
case .success(let value): | |
completion(.success(value)) | |
case .failure(let error): | |
completion(.failure(error)) | |
} | |
}) | |
}) | |
} | |
@discardableResult | |
func getObject<T:Decodable>(endpoint: String) -> Future<T> { | |
let decoder: JSONDecoder = JSONDecoder() | |
return Future(operation: { completion in | |
Alamofire.request(K.ProductionServer.baseURL + endpoint).responseJSONDecodable(decoder: decoder, completionHandler: { (response: DataResponse<T>) in | |
switch response.result { | |
case .success(let value): | |
completion(.success(value)) | |
case .failure(let error): | |
completion(.failure(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
import Foundation | |
import PromisedFuture | |
protocol HttpProtocol { | |
func getArray<T:Decodable>(endpoint: String) -> Future<[T]> | |
func getObject<T:Decodable>(endpoint: String) -> Future<T> | |
} |
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
import Foundation | |
import PromisedFuture | |
protocol ListNewsUseCaseBusinessLogic | |
{ | |
func fetchNews(request: ListNewsUseCase.ListNews.Request) | |
} | |
protocol ListNewsUseCaseDataStore | |
{ | |
var news: [ListNewsUseCase.ListNews.SuccessResponse.NewModel]? {get} | |
} | |
class ListNewsUseCaseInteractor: ListNewsUseCaseBusinessLogic, ListNewsUseCaseDataStore | |
{ | |
var presenter: ListNewsUseCasePresentationLogic? | |
var worker: ListNewsUseCaseWorker? | |
var news: [ListNewsUseCase.ListNews.SuccessResponse.NewModel]? | |
func fetchNews(request: ListNewsUseCase.ListNews.Request) | |
{ | |
worker = ListNewsUseCaseWorker() | |
let newsFuture = worker?.fetchNews() | |
newsFuture?.execute(onSuccess: { news in | |
self.news = news | |
let response = ListNewsUseCase.ListNews.SuccessResponse(news:news) | |
self.presenter?.presentListNewsSuccess(response: response) | |
}, onFailure: {error in | |
let response = ListNewsUseCase.ListNews.ErrorResponse(message: error.localizedDescription) | |
self.presenter?.presentListNewsError(response: response) }) | |
} | |
} |
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
import Foundation | |
import PromisedFuture | |
class ListNewsUseCaseWorker | |
{ | |
var api:APIClient = APIClient() | |
func fetchNews() -> Future<[ListNewsUseCase.ListNews.SuccessResponse.NewModel]> { | |
return api.getNews() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment