-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/4.5.0' into versions
- Loading branch information
Showing
18 changed files
with
197 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Foundation | ||
|
||
public enum Secret: Equatable { | ||
case microsoftTranslator(secret: String) | ||
case deepL(secret: String) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import Foundation | ||
import Microya | ||
|
||
// Documentation can be found here: https://www.deepl.com/ja/docs-api/ | ||
|
||
enum DeepLApi { | ||
case translate(texts: [String], from: Language, to: Language, apiKey: String) | ||
|
||
static let maximumTextsPerRequest: Int = 25 | ||
static let maximumTextsLengthPerRequest: Int = 5_000 | ||
|
||
static func textBatches(forTexts texts: [String]) -> [[String]] { | ||
var batches: [[String]] = [] | ||
var currentBatch: [String] = [] | ||
var currentBatchTotalLength: Int = 0 | ||
|
||
for text in texts { | ||
if currentBatch.count < maximumTextsPerRequest && text.count + currentBatchTotalLength < maximumTextsLengthPerRequest { | ||
currentBatch.append(text) | ||
currentBatchTotalLength += text.count | ||
} else { | ||
batches.append(currentBatch) | ||
|
||
currentBatch = [text] | ||
currentBatchTotalLength = text.count | ||
} | ||
} | ||
|
||
return batches | ||
} | ||
} | ||
|
||
extension DeepLApi: Endpoint { | ||
typealias ClientErrorType = DeepLTranslateErrorResponse | ||
|
||
var decoder: JSONDecoder { | ||
let decoder = JSONDecoder() | ||
decoder.keyDecodingStrategy = .convertFromSnakeCase | ||
return decoder | ||
} | ||
|
||
var encoder: JSONEncoder { | ||
JSONEncoder() | ||
} | ||
|
||
var baseUrl: URL { | ||
URL(string: "https://api.deepl.com")! | ||
} | ||
|
||
var subpath: String { | ||
switch self { | ||
case .translate: | ||
return "/v2/translate" | ||
} | ||
} | ||
|
||
var method: HttpMethod { | ||
.get | ||
} | ||
|
||
var queryParameters: [String: QueryParameterValue] { | ||
var urlParameters: [String: QueryParameterValue] = [:] | ||
|
||
switch self { | ||
case let .translate(texts, sourceLanguage, targetLanguage, apiKey): | ||
urlParameters["text"] = .array(texts) | ||
urlParameters["source_lang"] = .string(sourceLanguage.rawValue.capitalized) | ||
urlParameters["target_lang"] = .string(targetLanguage.rawValue.capitalized) | ||
urlParameters["auth_key"] = .string(apiKey) | ||
} | ||
|
||
return urlParameters | ||
} | ||
|
||
var headers: [String: String] { | ||
["Content-Type": "application/json"] | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
Sources/BartyCrouchTranslator/DeeplApi/Model/DeepLTranslateErrorResponse.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Foundation | ||
|
||
struct DeepLTranslateErrorResponse: Decodable { | ||
let message: String | ||
} |
10 changes: 10 additions & 0 deletions
10
Sources/BartyCrouchTranslator/DeeplApi/Model/DeepLTranslateResponse.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Foundation | ||
|
||
struct DeepLTranslateResponse: Decodable { | ||
struct Translation: Decodable { | ||
let detectedSourceLanguage: String | ||
let text: String | ||
} | ||
|
||
let translations: [Translation] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
Tests/BartyCrouchTranslatorTests/DeepLTranslatorApiTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
@testable import BartyCrouchTranslator | ||
import Foundation | ||
import Microya | ||
import XCTest | ||
|
||
class DeepLTranslatorApiTests: XCTestCase { | ||
func testTranslate() { | ||
let apiKey = "" // TODO: load from environment variable | ||
guard !apiKey.isEmpty else { return } | ||
|
||
let endpoint = DeepLApi.translate( | ||
texts: ["How old are you?", "Love"], | ||
from: .english, | ||
to: .german, | ||
apiKey: apiKey | ||
) | ||
|
||
let apiProvider = ApiProvider<DeepLApi>() | ||
|
||
switch apiProvider.performRequestAndWait(on: endpoint, decodeBodyTo: DeepLTranslateResponse.self) { | ||
case let .success(translateResponses): | ||
XCTAssertEqual(translateResponses.translations[0].text, "Wie alt sind Sie?") | ||
|
||
case let .failure(failure): | ||
XCTFail(failure.localizedDescription) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters