Skip to content

Commit

Permalink
style: format code
Browse files Browse the repository at this point in the history
fixed extraneous white spaces with SwiftLint
  • Loading branch information
Jerry23011 committed Dec 3, 2023
1 parent 3ef22b7 commit 103a4de
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
24 changes: 12 additions & 12 deletions Easydict/Feature/Service/Tencent/TencentService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public final class TencentService: QueryService {
override public func serviceType() -> ServiceType {
.tencent
}

override public func link() -> String? {
"https://fanyi.qq.com"
}

override public func name() -> String {
NSLocalizedString("tencent_translate", comment: "The name of Tencent Translate")
}

override public func supportLanguagesDictionary() -> MMOrderedDictionary<AnyObject, AnyObject> {
// TODO: Replace MMOrderedDictionary in the API
let orderedDict = MMOrderedDictionary<AnyObject, AnyObject>()
Expand All @@ -31,7 +31,7 @@ public final class TencentService: QueryService {
}
return orderedDict
}

override public func ocr(_: EZQueryModel) async throws -> EZOCRResult {
NSLog("Tencent Translate currently does not support OCR")
throw QueryServiceError.notSupported
Expand All @@ -42,10 +42,10 @@ public final class TencentService: QueryService {
}

// MARK: API Request

private static let defaultSecretId = ""
private static let defaultSecretKey = ""



// easydict://writeKeyValue?EZTencentSecretId=xxx
private var secretId: String {
let secretId = UserDefaults.standard.string(forKey: EZTencentSecretId)
Expand All @@ -55,7 +55,7 @@ public final class TencentService: QueryService {
return TencentService.defaultSecretId
}
}

// easydict://writeKeyValue?EZTencentSecretKey=xxx
private var secretKey: String {
let secretKey = UserDefaults.standard.string(forKey: EZTencentSecretKey)
Expand All @@ -65,16 +65,16 @@ public final class TencentService: QueryService {
return TencentService.defaultSecretKey
}
}

public override func translate(_ text: String, from: Language, to: Language, completion: @escaping (EZQueryResult, Error?) -> Void) {
if prehandleQueryTextLanguage(text, from: from, to: to, completion: completion) {
return
}

translateText(text, from: from, to: to, completion: completion)
}

func translateText(_ text: String, from: Language, to: Language, completion: @escaping (EZQueryResult, Error?) -> Void) {
func translateText(_ text: String, from: Language, to: Language, completion: @escaping (EZQueryResult, Error?) -> Void) {
let transType = TencentTranslateType.transType(from: from, to: to)
guard transType != .unsupported else {
result.errorType = .unsupportedLanguage
Expand All @@ -83,14 +83,14 @@ public final class TencentService: QueryService {
completion(result, nil)
return
}

let parameters: [String: Any] = [
"SourceText": text,
"Source": transType.sourceLanguage,
"Target": transType.targetLanguage,
"ProjectId": 0
]

let endpoint = "https://tmt.tencentcloudapi.com"
let headers = tencentSignHeader(parameters: parameters, secretId: secretId, secretKey: secretKey)

Expand Down
12 changes: 6 additions & 6 deletions Easydict/Feature/Service/Tencent/TencentSigning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func tencentSignHeader(parameters: [String: Any], secretId: String, secretKey: S
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
let date = dateFormatter.string(from: Date(timeIntervalSince1970: TimeInterval(timestamp)))

// ************* 步骤 1:拼接规范请求串 *************
let httpRequestMethod = "POST"
let canonicalUri = "/"
Expand All @@ -42,7 +42,7 @@ func tencentSignHeader(parameters: [String: Any], secretId: String, secretKey: S
\(signedHeaders)
\(hashedRequestPayload)
"""

// ************* 步骤 2:拼接待签名字符串 *************
let credentialScope = "\(date)/\(service)/tc3_request"
let hashedCanonicalRequest = canonicalRequest.sha256()
Expand All @@ -52,7 +52,7 @@ func tencentSignHeader(parameters: [String: Any], secretId: String, secretKey: S
\(credentialScope)
\(hashedCanonicalRequest)
"""

// ************* 步骤 3:计算签名 *************
let secretDate = date.hmac(key: Data("TC3\(secretKey)".utf8))
let secretService = service.hmac(key: secretDate)
Expand All @@ -63,7 +63,7 @@ func tencentSignHeader(parameters: [String: Any], secretId: String, secretKey: S
let authorization = """
\(algorithm) Credential=\(secretId)/\(credentialScope), SignedHeaders=\(signedHeaders), Signature=\(signature)
"""

let headers: HTTPHeaders = [
"Authorization": authorization,
"Content-Type": ct,
Expand All @@ -73,7 +73,7 @@ func tencentSignHeader(parameters: [String: Any], secretId: String, secretKey: S
"X-TC-Version": version,
"X-TC-Region": region
]

return headers
}

Expand All @@ -84,7 +84,7 @@ extension String {
let digest = SHA256.hash(data: data)
return digest.compactMap {String(format: "%02x", $0)}.joined()
}

// hmac
func hmac(key: Data) -> Data {
let data = Data(self.utf8)
Expand Down

0 comments on commit 103a4de

Please sign in to comment.