You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.8 KiB
65 lines
1.8 KiB
//
|
|
// DataRequestExtension.swift
|
|
// GMERemittance
|
|
//
|
|
// Created by gme_2 on 22/08/2018.
|
|
// Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
|
|
//
|
|
import Foundation
|
|
import Alamofire
|
|
import ObjectMapper
|
|
import SwiftyJSON
|
|
|
|
enum GMEResponseErrorCode: Int {
|
|
case timeOut = 0
|
|
case failure
|
|
case unknown
|
|
case deleted
|
|
}
|
|
|
|
enum GmeResponseOperationStatusCode: String {
|
|
case successfull = "Operation Successfull"
|
|
}
|
|
|
|
extension DataRequest {
|
|
func handle<T: Mappable>(
|
|
success: @escaping (T) -> Void,
|
|
failure: @escaping (Error) -> Void
|
|
) {
|
|
self.validate().responseJSON { (response) -> Void in
|
|
switch response.result {
|
|
case .success(let value):
|
|
let json = JSON(value)
|
|
print(json)
|
|
if let model: T = json.map() {
|
|
success(model)
|
|
} else if (response.response?.statusCode ?? -1) == 204 {
|
|
let error = self.makeError(
|
|
message: "Response could not be mapped",
|
|
code: GMEResponseErrorCode.unknown.rawValue
|
|
)
|
|
|
|
failure(error)
|
|
} else {
|
|
let error = self.makeError(
|
|
message: GmeResponseOperationStatusCode.successfull.rawValue,
|
|
code: GMEResponseErrorCode.deleted.rawValue
|
|
)
|
|
failure(error)
|
|
}
|
|
case .failure(let error):
|
|
debugPrint("DataRequest Error: \(error.localizedDescription)")
|
|
if (response.response?.statusCode ?? -1) == 401 {
|
|
MainWireframe.logoutWarningAlert(message: "You have logged in from another device.")
|
|
return
|
|
}
|
|
failure(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func makeError(message: String, code: Int) -> Error {
|
|
let errorInfo: [String: Any]? = [NSLocalizedDescriptionKey: message]
|
|
return NSError.init(domain: "Network", code: code, userInfo: errorInfo)
|
|
}
|
|
}
|