chore(request): use responseDecodable
This commit is contained in:
parent
01f3e3da2e
commit
ae7cf5e89e
|
@ -82,8 +82,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||||
removeUnExistProxyGroups()
|
removeUnExistProxyGroups()
|
||||||
|
|
||||||
// start proxy
|
// start proxy
|
||||||
setupData()
|
|
||||||
updateConfig(showNotification: false)
|
updateConfig(showNotification: false)
|
||||||
|
setupData()
|
||||||
updateLoggingLevel()
|
updateLoggingLevel()
|
||||||
|
|
||||||
// start watch config file change
|
// start watch config file change
|
||||||
|
|
|
@ -16,10 +16,6 @@ protocol ApiRequestStreamDelegate: class {
|
||||||
func didGetLog(log: String, level: String)
|
func didGetLog(log: String, level: String)
|
||||||
}
|
}
|
||||||
|
|
||||||
enum RequestError: Error {
|
|
||||||
case decodeFail
|
|
||||||
}
|
|
||||||
|
|
||||||
typealias ErrorString = String
|
typealias ErrorString = String
|
||||||
|
|
||||||
class ApiRequest {
|
class ApiRequest {
|
||||||
|
@ -73,15 +69,12 @@ class ApiRequest {
|
||||||
|
|
||||||
static func requestConfig(completeHandler: @escaping ((ClashConfig) -> Void)) {
|
static func requestConfig(completeHandler: @escaping ((ClashConfig) -> Void)) {
|
||||||
if !ConfigManager.builtInApiMode {
|
if !ConfigManager.builtInApiMode {
|
||||||
req("/configs").responseData {
|
req("/configs").responseDecodable(of: ClashConfig.self) {
|
||||||
res in
|
resp in
|
||||||
do {
|
switch resp.result {
|
||||||
let data = try res.result.get()
|
case let .success(config):
|
||||||
guard let config = ClashConfig.fromData(data) else {
|
|
||||||
throw RequestError.decodeFail
|
|
||||||
}
|
|
||||||
completeHandler(config)
|
completeHandler(config)
|
||||||
} catch let err {
|
case let .failure(err):
|
||||||
Logger.log(err.localizedDescription)
|
Logger.log(err.localizedDescription)
|
||||||
NSUserNotificationCenter.default.post(title: "Error", info: err.localizedDescription)
|
NSUserNotificationCenter.default.post(title: "Error", info: err.localizedDescription)
|
||||||
}
|
}
|
||||||
|
@ -156,14 +149,20 @@ class ApiRequest {
|
||||||
ApiRequest.shared.proxyRespCache = proxies
|
ApiRequest.shared.proxyRespCache = proxies
|
||||||
completeHandler?(proxies)
|
completeHandler?(proxies)
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static func requestProxyProviderList(completeHandler: ((ClashProviderResp) -> Void)? = nil) {
|
static func requestProxyProviderList(completeHandler: ((ClashProviderResp) -> Void)? = nil) {
|
||||||
req("/providers/proxies").responseData { res in
|
req("/providers/proxies")
|
||||||
let provider = ClashProviderResp.create(try? res.result.get())
|
.responseDecodable(of: ClashProviderResp.self, decoder: ClashProviderResp.decoder) { resp in
|
||||||
completeHandler?(provider)
|
switch resp.result {
|
||||||
}
|
case let .success(providerResp):
|
||||||
|
completeHandler?(providerResp)
|
||||||
|
case let .failure(err):
|
||||||
|
print(err)
|
||||||
|
completeHandler?(ClashProviderResp())
|
||||||
|
assertionFailure()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func updateAllowLan(allow: Bool, completeHandler: (() -> Void)? = nil) {
|
static func updateAllowLan(allow: Bool, completeHandler: (() -> Void)? = nil) {
|
||||||
|
@ -236,10 +235,14 @@ class ApiRequest {
|
||||||
|
|
||||||
extension ApiRequest {
|
extension ApiRequest {
|
||||||
static func getConnections(completeHandler: @escaping ([ClashConnectionSnapShot.Connection]) -> Void) {
|
static func getConnections(completeHandler: @escaping ([ClashConnectionSnapShot.Connection]) -> Void) {
|
||||||
req("/connections").responseData { res in
|
req("/connections").responseDecodable(of: ClashConnectionSnapShot.self) { resp in
|
||||||
guard let data = try? res.result.get() else { return }
|
switch resp.result {
|
||||||
let resp = ClashConnectionSnapShot.fromData(data)
|
case let .success(snapshot):
|
||||||
completeHandler(resp.connections)
|
completeHandler(snapshot.connections)
|
||||||
|
case .failure:
|
||||||
|
assertionFailure()
|
||||||
|
completeHandler([])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,12 +10,6 @@ import Cocoa
|
||||||
|
|
||||||
struct ClashConnectionSnapShot: Codable {
|
struct ClashConnectionSnapShot: Codable {
|
||||||
let connections: [Connection]
|
let connections: [Connection]
|
||||||
|
|
||||||
static func fromData(_ data: Data) -> ClashConnectionSnapShot {
|
|
||||||
let decoder = JSONDecoder()
|
|
||||||
let model = try? decoder.decode(ClashConnectionSnapShot.self, from: data)
|
|
||||||
return model ?? ClashConnectionSnapShot(connections: [])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension ClashConnectionSnapShot {
|
extension ClashConnectionSnapShot {
|
||||||
|
|
|
@ -14,15 +14,14 @@ class ClashProviderResp: Codable {
|
||||||
return allProviders.filter({ $0.value.vehicleType != .Compatible })
|
return allProviders.filter({ $0.value.vehicleType != .Compatible })
|
||||||
}()
|
}()
|
||||||
|
|
||||||
private init() {
|
init() {
|
||||||
allProviders = [:]
|
allProviders = [:]
|
||||||
}
|
}
|
||||||
|
|
||||||
static func create(_ data: Data?) -> ClashProviderResp {
|
static var decoder: JSONDecoder {
|
||||||
guard let data = data else { return ClashProviderResp() }
|
|
||||||
let decoder = JSONDecoder()
|
let decoder = JSONDecoder()
|
||||||
decoder.dateDecodingStrategy = .formatted(DateFormatter.js)
|
decoder.dateDecodingStrategy = .formatted(DateFormatter.js)
|
||||||
return (try? decoder.decode(ClashProviderResp.self, from: data)) ?? ClashProviderResp()
|
return decoder
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum CodingKeys: String, CodingKey {
|
private enum CodingKeys: String, CodingKey {
|
||||||
|
|
Loading…
Reference in New Issue