Skip to content

Instantly share code, notes, and snippets.

@poksi592
Created May 15, 2018 09:56
Show Gist options
  • Save poksi592/3ee84a5dc2c3e4ef1333cc01413e4f93 to your computer and use it in GitHub Desktop.
Save poksi592/3ee84a5dc2c3e4ef1333cc01413e4f93 to your computer and use it in GitHub Desktop.
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