Created
May 15, 2018 09:56
-
-
Save poksi592/3ee84a5dc2c3e4ef1333cc01413e4f93 to your computer and use it in GitHub Desktop.
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
class PaymentsModuleRouter: ModuleRouter { | |
lazy var interactor = PaymentInteractor() | |
internal var route: String | |
required init(route: String) { | |
self.route = route | |
} | |
func route(parameters: ModuleParameters?, | |
path: String?, | |
callback: ModuleCallback?) { | |
switch path { | |
case "/pay": | |
interactor.pay(parameters: parameters) { (urlResponse, error) in | |
callback?(nil, nil, urlResponse, error) | |
} | |
default: | |
return | |
} | |
} | |
} | |
class PaymentInteractor { | |
func pay(parameters: ModuleParameters?, | |
completion: @escaping (HTTPURLResponse?, Error?) -> Void) { | |
let service = MockPaymentsNetworkService() | |
guard let parameters = parameters, | |
let token = parameters[PaymentsModuleParameters.token.rawValue], | |
let amount = parameters[PaymentsModuleParameters.amount.rawValue] else { | |
return | |
} | |
let payParameters = [PaymentsModuleParameters.token.rawValue: token, | |
PaymentsModuleParameters.amount.rawValue: amount] | |
service.post(host: "payments", | |
path: "/pay", | |
parameters: payParameters) { (response, urlResponse, error) in | |
// We are not going to check errors and URL response status codes, just a shortest path. | |
var networkError: ResponseError? = nil | |
if let error = error { | |
networkError = ResponseError(error: error, response: urlResponse) | |
} | |
completion(urlResponse, networkError) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment