Skip to content

Instantly share code, notes, and snippets.

@AdieOlami
Last active May 3, 2019 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdieOlami/ae4fb3ff70abdb576fe4842afebbbf2d to your computer and use it in GitHub Desktop.
Save AdieOlami/ae4fb3ff70abdb576fe4842afebbbf2d to your computer and use it in GitHub Desktop.
Observable+ObjectMapper
extension Observable {
/// handle the network response and map to JSON
/// - returns: Observable<JSON>
func handleResponseMapJSON() -> Observable<Result<JSON, ORMError>> {
return self.map { representor in
guard let response = representor as? Moya.Response else {
return .failure(ORMError.ORMNoRepresentor)
}
guard ((200...299) ~= response.statusCode) else {
return .failure(ORMError.ORMNotSuccessfulHTTP)
}
guard let json = JSON.init(rawValue: response.data),
json != JSON.null,
let code = json["code"].int else {
return .failure(ORMError.ORMParseJSONError)
}
guard code == BizStatus.BizSuccess.rawValue else {
let message: String = {
let json = JSON.init(data: response.data)
guard let msg = json["status"].string else { return "" }
return msg
}()
log(message, .error)
return .failure(ORMError.ORMBizError(resultCode: "\(code)", resultMsg: message))
}
return .success(json["result"])
}
}
/// JSON map to object
/// - returns: Observable<T>
func jsonMapObject<T: Mappable>(type: T.Type) -> Observable<Result<T, ORMError>> {
return self.map { rawResult in
guard let result = rawResult as? Result<JSON, ORMError> else {
return .failure(ORMError.ORMParseJSONError)
}
switch result {
case let .success(json):
guard json != JSON.null,
let dict = json.dictionaryObject else {
return .failure(ORMError.ORMParseJSONError)
}
guard let object = Mapper<T>().map(JSON: dict) else {
return .failure(ORMError.ORMCouldNotMakeObjectError)
}
return .success(object)
case let .failure(error):
return .failure(error)
}
}
}
/// JSON map to object array
/// - returns: Observable<[T]>
func jsonMapArray<T: Mappable>(type: T.Type) -> Observable<Result<[T], ORMError>> {
return self.map { rawResult in
guard let result = rawResult as? Result<JSON, ORMError> else {
return .failure(ORMError.ORMParseJSONError)
}
switch result {
case let .success(json):
guard json != JSON.null,
let jsonArray = json.array else {
return .failure(ORMError.ORMParseJSONError)
}
let dictArray = jsonArray.map { $0.dictionaryObject! }
return .success(Mapper<T>().mapArray(JSONArray: dictArray))
case let .failure(error):
return .failure(error)
}
}
}
}
extension ObservableType where E == Response {
func handleResponseMapJSON() -> Observable<Result<JSON, ORMError>> {
return self.map { representor in
guard let response = representor as? Moya.Response else {
return .failure(ORMError.ORMNoRepresentor)
}
guard ((200...299) ~= response.statusCode) else {
let message: String = try {
let json = try JSON.init(data: response.data)
guard let msg = json["message"].string else { return "" }
return msg
}()
return .failure(ORMError.ORMNotSuccessfulHTTP(resultMsg: message))
}
guard let json = JSON.init(rawValue: response.data),
json != JSON.null,
let status = json["status"].string else {
return .failure(ORMError.ORMParseJSONError)
}
guard status == BizStatus.BizSuccess.rawValue else {
let message: String = try {
let json = try JSON.init(data: response.data)
guard let msg = json["status"].string else { return "" }
return msg
}()
log(message, .error)
return .failure(ORMError.ORMBizError(resultCode: "\(status)", resultMsg: message))
}
return .success(json["data"])
}
}
/// JSON map to object
/// - returns: Observable<T>
func jsonMapObject<T: Decodable>(_ type: T.Type, _ path: String? = nil) -> Observable<Result<T, ORMError>> {
return self.map { rawResult in
guard let result = rawResult as? Result<JSON, ORMError> else {
return .failure(ORMError.ORMParseJSONError)
}
switch result {
case let .success(json):
guard json != JSON.null,
let dict = json.dictionaryObject else {
return .failure(ORMError.ORMParseJSONError)
}
let decoder = JSONDecoder()
let jsonData = try JSONSerialization.data(withJSONObject: dict)
// let jsonString = String(data: jsonData, encoding: .utf8)
guard let object = try? decoder.decode(T.self, from: jsonData) else {
return .failure(ORMError.ORMCouldNotMakeObjectError)
}
return .success(object)
case let .failure(error):
return .failure(error)
}
}
}
/// JSON map to object array
/// - returns: Observable<[T]>
func jsonMapArray<T: Decodable>(type: T.Type) -> Observable<Result<[T], ORMError>> {
return self.map { rawResult in
guard let result = rawResult as? Result<JSON, ORMError> else {
return .failure(ORMError.ORMParseJSONError)
}
switch result {
case let .success(json):
guard json != JSON.null,
let jsonArray = json.array else {
return .failure(ORMError.ORMParseJSONError)
}
let decoder = JSONDecoder()
let dictArray = jsonArray.map { $0.dictionaryObject! }
let jsonData = try JSONSerialization.data(withJSONObject: dictArray)
let d = try decoder.decode(T.self, from: jsonData)
return .success([d])
case let .failure(error):
return .failure(error)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment