jsdw_ios/QuickLocation/API/APIProvider.swift

135 lines
4.0 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.

//
// APIProvider.swift
// HealthyZG
//
// Created by on 2020/5/18.
// Copyright © 2020 Lin. All rights reserved.
//
import Foundation
import Moya
import RxSwift
// swiftlint:disable:next identifier_name
public let GatewayStatusCodeKey = "GatewayStatusCodeKey"
typealias Parameters = [String: Any]
private func JSONResponseDataFormatter(_ data: Data) -> String {
do {
let dataAsJSON = try JSONSerialization.jsonObject(with: data)
let prettyData = try JSONSerialization.data(withJSONObject: dataAsJSON, options: .prettyPrinted)
return String(data: prettyData, encoding: .utf8) ?? String(data: data, encoding: .utf8) ?? ""
} catch {
return String(data: data, encoding: .utf8) ?? ""
}
}
private func reversedPrint(_ separator: String, terminator: String, items: Any...) {
for item in items {
if let msg = item as? String {
LogInfo(msg)
}
}
}
// MARK: - Provider setup
let requestClosure = { (endpoint: Endpoint, done: MoyaProvider.RequestResultClosure) in
do {
var request = try endpoint.urlRequest()
// Modify the request however you like.
request.timeoutInterval = 15 //
done(.success(request))
} catch {
done(.failure(MoyaError.underlying(error, nil)))
}
}
let plugins: [PluginType] = [
SignPlugin(),
NetworkLoggerPlugin(configuration: .init(formatter: .init(responseData: JSONResponseDataFormatter), logOptions: .verbose)),
NetworkActivityPlugin(networkActivityClosure: { change, _ in
#if !SHARE_EXTENSION
DispatchQueue.main.async {
switch change {
case .began:
UIApplication.shared.isNetworkActivityIndicatorVisible = true
case .ended:
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
}
#endif
})
]
public let APIProvider = MoyaProvider<MultiTarget>(requestClosure: requestClosure,
plugins: plugins).rx
public extension Error {
var underlyingError: NSError? {
guard let moyaError = self as? MoyaError else { return nil }
guard case let .underlying(error, _) = moyaError else { return nil }
return error as NSError
}
var gatewayStatusCode: Int {
let error = underlyingError ?? self as NSError
return error.code
}
var gatewayMessage: String? {
let error = underlyingError ?? self as NSError
return error.userInfo[NSLocalizedDescriptionKey] as? String
}
var responseData: Response? {
guard let moyaError = self as? MoyaError else { return nil }
guard case let .underlying(_, response) = moyaError else { return nil }
return response
}
var isShowErrorTips: Bool {
let error = underlyingError ?? self as NSError
return error.userInfo["isShowTip"] as? Bool ?? true
}
}
public extension MoyaError {
var innerError: NSError? {
switch self {
case let .encodableMapping(error):
return error as NSError
case let .objectMapping(error, _):
return error as NSError
case let .underlying(error, _):
return error as NSError
case let .parameterEncoding(error):
return error as NSError
default:
return nil
}
}
}
//
enum GatewayStatusCode: Int {
//
case success = 0
//
case outdateVersion = 3
/** ============== ============== */
case riskControl = 701
/** ============== 200~300 ============== */
//
case userLoginExpair = 1001003
//
case noAuthority = 500
//
case review = 201
/** ============== ============== */
//
case groupLimit = 20009
/** ============== ============== */
case unknownError = -9999
}