Closed AdieOlami closed 5 years ago
Are you using the EVReflection/MoyaRxSwift extension? You could then extend ObservalbeType Here is a sample of my:
import RxSwift
import Moya
import EVReflection
public extension ObservableType where E == Response {
/// Maps data received from the signal into an error if the statusCode != 0. alwasys call after mapJSON
public func genericNetworkError() -> Observable<E> {
return self.catchError { error in
let message = self.getErrorMessage(error as NSError)
let error: NSError = NSError(
domain: "Midlayer",
code: (error as NSError).code,
userInfo: [NSLocalizedDescriptionKey: message,
"showErrorCodeToUser": true,
NSLocalizedFailureReasonErrorKey: error.localizedDescription])
throw error
}
}
private func getErrorMessage(_ error: NSError) -> String {
var errorMessage: String = error.localizedDescription
switch error.code {
case NSURLErrorNotConnectedToInternet, // Returned when a network resource was requested, but an internet connection is not established and cannot be established automatically, either through a lack of connectivity, or by the user's choice not to make a network connection automatically.
NSURLErrorUnsupportedURL, // Returned when a properly formed URL cannot be handled by the framework.
NSURLErrorInternationalRoamingOff, // Returned when a connection would require activating a data context while roaming, but international roaming is disabled.
NSURLErrorCallIsActive, // Returned when a connection is attempted while a phone call is active on a network that does not support simultaneous phone and data communication (EDGE or GPRS).
NSURLErrorDataNotAllowed, // Returned when the cellular network disallows a connection.
NSURLErrorSecureConnectionFailed, // Returned when an attempt to establish a secure connection fails for reasons which cannot be expressed more specifically.
NSURLErrorCannotLoadFromNetwork, // Returned when a specific request to load an item only from the cache cannot be satisfied.
NSURLErrorNetworkConnectionLost: // Returned when a client or server connection is severed in the middle of an in-progress load.
// Internet connection problems
errorMessage = NSLocalizedString("noInternet", comment: "")
default:
errorMessage = NSLocalizedString("generalFailure", comment: "")
}
return errorMessage
}
}
public extension ObservableType where E == Any {
/// Decrypt the body for a cryptosession request
public func decryptBody() -> Observable<Any> {
return flatMap { result -> Observable<Any> in
let error: NSError = NSError(
domain: "Midlayer",
code: 9000,
userInfo: [NSLocalizedDescriptionKey: "Midlayer incorrect result",
"showErrorCodeToUser": true,
NSLocalizedFailureReasonErrorKey: "result: \(result)"])
guard var dictionary = result as? [AnyHashable : Any] else { throw error }
guard let body: String = dictionary["body"] as? String else { throw error }
guard let vector: String = dictionary["initializationVector"] as? String else { throw error }
let decrypted = SecureHelper.decryptWithAES(body, key: Midlayer.sessionKey ?? "", iv: vector) ?? ""
dictionary["body"] = decrypted
Stuff.print("⚡️✅ decrypted result:\n\(decrypted)")
return Observable.just(dictionary)
}
}
}
Do you have any update on this? Otherwise I will close the issue.
So, I was using Mapper and SwiftyJson before but I Just had a switch over to EVReflection which is a brilliant Library but I have issues extending Observable as I did so as to pass some custom response and all. below is the Mapper code and the new EVReflection that I am trying with Moya
Any assistance would be very appreciated. Thanks