Skip to content

Commit

Permalink
improve response parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
choykarl committed Dec 30, 2023
1 parent 628d51c commit 50c01b9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
40 changes: 37 additions & 3 deletions Easydict/Feature/Service/Ali/AliResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ struct AliWebResponse: Codable {
}

var requestId: String?
var success: Bool?
var success: Bool
var httpStatusCode: Int?
var code: String?
var code: AnyCodable?
var message: String?
var data: Data?
}
Expand Down Expand Up @@ -71,7 +71,7 @@ struct AliAPIResponse: Codable {
var WordCount: String?
}

var Code: String?
var Code: AnyCodable?
var Data: Data?
var RequestId: String?
var Message: String?
Expand All @@ -92,3 +92,37 @@ struct AliTokenResponse: Codable {
var parameterName: String?
var headerName: String?
}

enum AnyCodable: Codable {
case string(String)
case int(Int)

init(from decoder: Decoder) throws {
if let intValue = try? decoder.singleValueContainer().decode(Int.self) {
self = .int(intValue)
} else if let stringValue = try? decoder.singleValueContainer().decode(String.self) {
self = .string(stringValue)
} else {
throw try DecodingError.dataCorruptedError(in: decoder.singleValueContainer(), debugDescription: "Code is neither Int nor String")
}
}

func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case let .string(stringValue):
try container.encode(stringValue)
case let .int(intValue):
try container.encode(intValue)
}
}

var stringValue: String? {
switch self {
case let .int(i):
return String(i)
case let .string(s):
return s
}
}
}
22 changes: 11 additions & 11 deletions Easydict/Feature/Service/Ali/AliService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ class AliService: QueryService {

/**
use user's access key id and secret
easydict://writeKeyValue?EZAliAccessKeyId=xxx
easydict://writeKeyValue?EZAliAccessKeySecret=xxx
easydict://writeKeyValue?EZAliAccessKeyId=LTAI5tRxhkyLkhMhBo9wMZWt
easydict://writeKeyValue?EZAliAccessKeySecret=BSpVWkbmU1QbJVofJYFpPtKkDrTmaN
*/
if let id = UserDefaults.standard.string(forKey: EZAliAccessKeyId),
let secret = UserDefaults.standard.string(forKey: EZAliAccessKeySecret), !id.isEmpty, !secret.isEmpty
Expand Down Expand Up @@ -121,7 +121,6 @@ class AliService: QueryService {
return mac.base64EncodedString()
}


/// https://help.aliyun.com/zh/sdk/product-overview/rpc-mechanism?spm=a2c4g.11186623.0.i20#sectiondiv-6jf-89b-wfa
var param = [
"FormatType": "text",
Expand Down Expand Up @@ -198,12 +197,13 @@ class AliService: QueryService {
completion(result, nil)
print("ali api translate success")
} else {
let ezError = EZError(type: .API, description: value.Code, errorDataMessage: value.Message)
completion(result, ezError)
completion(result, EZError(type: .API, description: value.Code?.stringValue, errorDataMessage: value.Message))
}
case let .failure(error):
print("ali api translate error \(error)")
let ezError = EZError(nsError: error, errorResponseData: response.data)
print("ali api translate error: \(error.errorDescription ?? "")")

let ezError = EZError(nsError: error, errorDataMessage: error.errorDescription)

completion(result, ezError)
}
}
Expand Down Expand Up @@ -240,12 +240,12 @@ class AliService: QueryService {
result.from = from
result.to = to
result.queryText = text
if let data = value.data, let translateText = data.translateText {
if value.success, let translateText = value.data?.translateText {
result.translatedResults = [translateText.unescapedXML()]
completion(result, nil)
print("ali web translate success")
} else {
let ezError = EZError(type: .API, description: value.code, errorDataMessage: value.message)
let ezError = EZError(type: .API, description: value.code?.stringValue, errorDataMessage: value.message)
completion(result, ezError)
}
self.canWebRetry = true
Expand All @@ -263,8 +263,8 @@ class AliService: QueryService {
}

} else {
print("ali web translate error \(error)")
let ezError = EZError(nsError: error, errorResponseData: response.data)
print("ali web translate error: \(error.errorDescription ?? "")")
let ezError = EZError(nsError: error, errorDataMessage: error.errorDescription)
completion(result, ezError)
}
}
Expand Down

0 comments on commit 50c01b9

Please sign in to comment.