Bark/Common/Moya/Observable+Extension.swift
2024-09-24 11:49:55 +08:00

124 lines
4.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Observable+Extension.swift
// Bark
//
// Created by huangfeng on 2018/6/25.
// Copyright © 2018 Fin. All rights reserved.
//
import UIKit
import Moya
import ObjectMapper
import RxSwift
import SwiftyJSON
import UIKit
extension Observable where Element: Moya.Response {
/// HTTP
func filterHttpError() -> Observable<Result<Element, ApiError>> {
return catchAndReturn(Element(statusCode: 599, data: Data()))
.map { response -> Result<Element, ApiError> in
if (200...209) ~= response.statusCode {
return .success(response)
} else {
return .failure(ApiError.Error(info: "网络错误"))
}
}
}
/// CODE
func filterResponseError() -> Observable<Result<JSON, ApiError>> {
return filterHttpError()
.map { response -> Result<JSON, ApiError> in
switch response {
case .success(let element):
do {
let json = try JSON(data: element.data)
if let codeStr = json["code"].rawString(),
let code = Int(codeStr),
code == 200
{
return .success(json)
} else {
var msg: String = ""
if json["message"].exists() {
msg = json["message"].rawString()!
}
return .failure(ApiError.Error(info: msg))
}
} catch {
return .failure(ApiError.Error(info: error.rawString()))
}
case .failure(let error):
return .failure(ApiError.Error(info: error.rawString()))
}
}
}
/// Response JSON Model
///
/// - Parameters:
/// - typeName: Model Class
/// - dataPath: ["data","links"]
func mapResponseToObj<T: Mappable>(_ typeName: T.Type, dataPath: [String] = ["data"]) -> Observable<Result<T, ApiError>> {
return filterResponseError().map { json in
switch json {
case .success(let json):
var rootJson = json
if dataPath.count > 0 {
rootJson = rootJson[dataPath]
}
if let model: T = self.resultFromJSON(json: rootJson) {
return .success(model)
} else {
return .failure(ApiError.Error(info: "json 转换失败"))
}
case .failure(let error):
return .failure(error)
}
}
}
/// Response JSON Model Array
func mapResponseToObjArray<T: Mappable>(_ type: T.Type, dataPath: [String] = ["data"]) -> Observable<Result<[T], ApiError>> {
return filterResponseError().map { json in
switch json {
case .success(let json):
var rootJson = json
if dataPath.count > 0 {
rootJson = rootJson[dataPath]
}
var result = [T]()
guard let jsonArray = rootJson.array else {
return .failure(ApiError.Error(info: "Root Json 不是 Array"))
}
for json in jsonArray {
if let jsonModel: T = self.resultFromJSON(json: json) {
result.append(jsonModel)
} else {
return .failure(ApiError.Error(info: "json 转换失败"))
}
}
return .success(result)
case .failure(let error):
return .failure(error)
}
}
}
private func resultFromJSON<T: Mappable>(jsonString: String) -> T? {
return T(JSONString: jsonString)
}
private func resultFromJSON<T: Mappable>(json: JSON) -> T? {
if let str = json.rawString() {
return resultFromJSON(jsonString: str)
}
return nil
}
}