From 9b3fb2376ac626b3969fb60a411f4e2699f88ee3 Mon Sep 17 00:00:00 2001 From: Jerry Date: Sat, 2 Dec 2023 22:19:29 -0800 Subject: [PATCH] perf: prepares TencentSigning for other services --- Easydict/Feature/Service/Tencent/TencentService.swift | 7 ++++++- Easydict/Feature/Service/Tencent/TencentSigning.swift | 10 +++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Easydict/Feature/Service/Tencent/TencentService.swift b/Easydict/Feature/Service/Tencent/TencentService.swift index 0694aa0b8..829f9431d 100644 --- a/Easydict/Feature/Service/Tencent/TencentService.swift +++ b/Easydict/Feature/Service/Tencent/TencentService.swift @@ -92,7 +92,12 @@ public final class TencentService: QueryService { ] let endpoint = "https://tmt.tencentcloudapi.com" - let headers = tencentSignHeader(parameters: parameters, secretId: secretId, secretKey: secretKey) + + let service = "tmt" + let action = "TextTranslate" + let version = "2018-03-21" + + let headers = tencentSignHeader(service: service, action: action, version: version, parameters: parameters, secretId: secretId, secretKey: secretKey) let request = AF.request(endpoint, method: .post, diff --git a/Easydict/Feature/Service/Tencent/TencentSigning.swift b/Easydict/Feature/Service/Tencent/TencentSigning.swift index b77cb80df..071b0191f 100644 --- a/Easydict/Feature/Service/Tencent/TencentSigning.swift +++ b/Easydict/Feature/Service/Tencent/TencentSigning.swift @@ -11,12 +11,12 @@ import Alamofire import CryptoKit // Tencent sigh header, Ref: https://github.com/TencentCloud/signature-process-demo/blob/main/signature-v3/swift/signv3.swift -func tencentSignHeader(parameters: [String: Any], secretId: String, secretKey: String) -> HTTPHeaders { - let service = "tmt" - let host = "tmt.tencentcloudapi.com" +func tencentSignHeader(service: String, action: String, version: String, parameters: [String: Any], secretId: String, secretKey: String) -> HTTPHeaders { + let service = service + let host = "\(service).tencentcloudapi.com" let region = "ap-guangzhou" - let action = "TextTranslate" - let version = "2018-03-21" + let action = action + let version = version let algorithm = "TC3-HMAC-SHA256" let timestamp = Int(Date().timeIntervalSince1970) let dateFormatter = DateFormatter()