Skip to content

Commit

Permalink
style: format code
Browse files Browse the repository at this point in the history
Format with SwiftLint
  • Loading branch information
Jerry23011 committed Dec 1, 2023
1 parent 7797a3c commit 51fc666
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Easydict/Feature/Service/Tencent/TencentResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ struct TencentResponse: Codable {
var Target: String
var TargetText: String
}

var Response: Response
}
17 changes: 8 additions & 9 deletions Easydict/Feature/Service/Tencent/TencentService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class TencentService: QueryService {
throw QueryServiceError.notSupported
}

//MARK: API Request
// MARK: API Request

private static let defaultSecretId = ""
private static let defaultSecretKey = ""
Expand Down Expand Up @@ -82,13 +82,13 @@ public final class TencentService: QueryService {
"SourceText": text,
"Source": transType.sourceLanguage,
"Target": transType.targetLanguage,
"ProjectId": projectId,
"ProjectId": projectId
]

func sha256(msg: String) -> String {
let data = msg.data(using: .utf8)!
let digest = SHA256.hash(data: data)
return digest.compactMap{String(format: "%02x", $0)}.joined()
return digest.compactMap {String(format: "%02x", $0)}.joined()
}

let service = "tmt"
Expand All @@ -104,7 +104,6 @@ public final class TencentService: QueryService {
dateFormatter.timeZone = TimeZone(identifier: "UTC")
let date = dateFormatter.string(from: Date(timeIntervalSince1970: TimeInterval(timestamp)))


// ************* 步骤 1:拼接规范请求串 *************
let httpRequestMethod = "POST"
let canonicalUri = "/"
Expand Down Expand Up @@ -139,21 +138,21 @@ public final class TencentService: QueryService {
let dateData = Data(date.utf8)
var symmetricKey = SymmetricKey(data: keyData)
let secretDate = HMAC<SHA256>.authenticationCode(for: dateData, using: symmetricKey)
_ = Data(secretDate).map{String(format: "%02hhx", $0)}.joined()
_ = Data(secretDate).map {String(format: "%02hhx", $0)}.joined()

let serviceData = Data(service.utf8)
symmetricKey = SymmetricKey(data: Data(secretDate))
let secretService = HMAC<SHA256>.authenticationCode(for: serviceData, using: symmetricKey)
_ = Data(secretService).map{String(format: "%02hhx", $0)}.joined()
_ = Data(secretService).map {String(format: "%02hhx", $0)}.joined()

let signingData = Data("tc3_request".utf8)
symmetricKey = SymmetricKey(data: secretService)
let secretSigning = HMAC<SHA256>.authenticationCode(for: signingData, using: symmetricKey)
_ = Data(secretSigning).map{String(format: "%02hhx", $0)}.joined()
_ = Data(secretSigning).map {String(format: "%02hhx", $0)}.joined()

let stringToSignData = Data(stringToSign.utf8)
symmetricKey = SymmetricKey(data: secretSigning)
let signature = HMAC<SHA256>.authenticationCode(for: stringToSignData, using: symmetricKey).map{String(format: "%02hhx", $0)}.joined()
let signature = HMAC<SHA256>.authenticationCode(for: stringToSignData, using: symmetricKey).map {String(format: "%02hhx", $0)}.joined()

// ************* 步骤 4:拼接 Authorization *************
let authorization = """
Expand All @@ -167,7 +166,7 @@ public final class TencentService: QueryService {
"X-TC-Action": action,
"X-TC-Timestamp": "\(timestamp)",
"X-TC-Version": version,
"X-TC-Region": region,
"X-TC-Region": region
]

let request = AF.request(endpoint,
Expand Down
6 changes: 3 additions & 3 deletions Easydict/Feature/Service/Tencent/TencentTranslateType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct TencentTranslateType: Equatable {
.thai: [.simplifiedChinese, .traditionalChinese, .english, .french, .spanish, .italian, .german, .turkish, .russian],
.malay: [.simplifiedChinese, .traditionalChinese, .english, .french, .spanish, .italian, .german, .turkish, .russian],
.arabic: [.simplifiedChinese, .traditionalChinese, .english, .french, .spanish, .italian, .german, .turkish, .russian],
.hindi: [.simplifiedChinese, .traditionalChinese, .english, .french, .spanish, .italian, .german, .turkish, .russian],
.hindi: [.simplifiedChinese, .traditionalChinese, .english, .french, .spanish, .italian, .german, .turkish, .russian]
]

static let supportLanguagesDictionary: [Language: String] = [
Expand All @@ -55,11 +55,11 @@ struct TencentTranslateType: Equatable {
.thai: "th",
.malay: "ms",
.arabic: "ar",
.hindi: "hi",
.hindi: "hi"
]

static func transType(from: Language, to: Language) -> TencentTranslateType {
if (supportedTypes[from]?.contains(to) == true) {
if supportedTypes[from]?.contains(to) == true {
guard let from = supportLanguagesDictionary[from],
let to = supportLanguagesDictionary[to] else {
return .unsupported
Expand Down

0 comments on commit 51fc666

Please sign in to comment.