Bark/Model/Observable+Extension.swift
2018-06-27 11:29:51 +08:00

126 lines
3.6 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 UIKit
import RxSwift
import ObjectMapper
import SwiftyJSON
import Moya
public enum ApiError : Swift.Error {
case Error(info: String)
case AccountBanned(info: String)
}
extension Swift.Error {
func rawString() -> String {
guard let err = self as? ApiError else {
return self.localizedDescription
}
switch err {
case let .Error(info):
return info
case let .AccountBanned(info):
return info
}
}
}
extension Observable where Element: Moya.Response {
/// HTTP
func filterHttpError() -> Observable<Element> {
return filter{ response in
if (200...209) ~= response.statusCode {
return true
}
print("网络错误")
throw ApiError.Error(info: "网络错误")
}
}
/// CODE
func filterResponseError() -> Observable<JSON> {
return filterHttpError().map({ (response) -> JSON in
let json = try JSON(data: response.data)
var code = 400
var msg:String?
if let codeStr = json["code"].rawString(), let c = Int(codeStr) {
code = c
}
if json["message"].exists() {
msg = json["message"].rawString()!
}
if (code == 200){
return json
}
switch code {
default: throw ApiError.Error(info: msg ?? "未知错误")
}
})
}
/// Response JSON Model
///
/// - Parameters:
/// - typeName: Model Class
/// - dataPath: ["data","links"]
func mapResponseToObj<T: Mappable>(_ typeName: T.Type , dataPath:[String] = ["data"] ) -> Observable<T> {
return filterResponseError().map{ json in
var rootJson = json
if dataPath.count > 0{
rootJson = rootJson[dataPath]
}
if let model: T = self.resultFromJSON(json: rootJson) {
return model
}
else{
throw ApiError.Error(info: "json 转换失败")
}
}
}
/// Response JSON Model Array
func mapResponseToObjArray<T: Mappable>(_ type: T.Type, dataPath:[String] = ["data"] ) -> Observable<[T]> {
return filterResponseError().map{ json in
var rootJson = json;
if dataPath.count > 0{
rootJson = rootJson[dataPath]
}
var result = [T]()
guard let jsonArray = rootJson.array else{
return result
}
for json in jsonArray{
if let jsonModel: T = self.resultFromJSON(json: json) {
result.append(jsonModel)
}
else{
throw ApiError.Error(info: "json 转换失败")
}
}
return result
}
}
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
}
}