Skip to content

Commit a57b3d7

Browse files
committed
feat: Allow specifying API version
1 parent 87e78b1 commit a57b3d7

File tree

5 files changed

+74
-16
lines changed

5 files changed

+74
-16
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.deepl.api;
2+
3+
public enum DeepLApiVersion {
4+
VERSION_1("v1"),
5+
VERSION_2("v2");
6+
7+
/**
8+
* How the version is represented in the URL string. Does not include any slashes (/). Example:
9+
* "v2"
10+
*/
11+
private final String urlRepresentation;
12+
13+
private DeepLApiVersion(String urlRepresentation) {
14+
this.urlRepresentation = urlRepresentation;
15+
}
16+
17+
public String toString() {
18+
return this.urlRepresentation;
19+
}
20+
}

deepl-java/src/main/java/com/deepl/api/DeepLClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ public List<WriteResult> rephraseText(
7373
throws InterruptedException, DeepLException {
7474
Iterable<KeyValuePair<String, String>> params =
7575
createWriteHttpParams(texts, targetLang, options);
76-
HttpResponse response = httpClientWrapper.sendRequestWithBackoff("/v2/write/rephrase", params);
76+
HttpResponse response =
77+
httpClientWrapper.sendRequestWithBackoff(
78+
String.format("/%s/write/rephrase", apiVersion), params);
7779
checkResponse(response, false, false);
7880
return jsonParser.parseWriteResult(response.getBody());
7981
}

deepl-java/src/main/java/com/deepl/api/DeepLClientOptions.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@
33
// license that can be found in the LICENSE file.
44
package com.deepl.api;
55

6+
import org.jetbrains.annotations.Nullable;
7+
68
/** {@inheritDoc} */
79
@SuppressWarnings("deprecation")
8-
public class DeepLClientOptions extends TranslatorOptions {}
10+
public class DeepLClientOptions extends TranslatorOptions {
11+
/**
12+
* Set the version of the DeepL API to use. By default, this value is <code>
13+
* DeepLApiVersion.VERSION_2</code> and the most recent DeepL API version is used. Note that older
14+
* API versions like <code>DeepLApiVersion.VERSION_1</code> might not support all features of the
15+
* more modern API (eg. document translation is v2-only), and that not all API subscriptions have
16+
* access to one or the other API version. If in doubt, always use the most recent API version you
17+
* have access to.
18+
*/
19+
public DeepLClientOptions setApiVersion(DeepLApiVersion apiVersion) {
20+
this.apiVersion = apiVersion;
21+
return this;
22+
}
23+
24+
/** Gets the current API version. */
25+
public @Nullable DeepLApiVersion getApiVersion() {
26+
return apiVersion;
27+
}
28+
}

deepl-java/src/main/java/com/deepl/api/Translator.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class Translator {
2626

2727
protected final Parser jsonParser = new Parser();
2828
protected final HttpClientWrapper httpClientWrapper;
29+
protected final DeepLApiVersion apiVersion;
2930

3031
/**
3132
* Initializes a new Translator object using your Authentication Key.
@@ -44,6 +45,7 @@ public Translator(String authKey, TranslatorOptions options) throws IllegalArgum
4445
if (authKey == null || authKey.length() == 0) {
4546
throw new IllegalArgumentException("authKey must be a non-empty string");
4647
}
48+
this.apiVersion = options.apiVersion;
4749
String serverUrl =
4850
(options.getServerUrl() != null)
4951
? options.getServerUrl()
@@ -195,7 +197,9 @@ public List<TextResult> translateText(
195197
throws DeepLException, InterruptedException {
196198
Iterable<KeyValuePair<String, String>> params =
197199
createHttpParams(texts, sourceLang, targetLang, options);
198-
HttpResponse response = httpClientWrapper.sendRequestWithBackoff("/v2/translate", params);
200+
HttpResponse response =
201+
httpClientWrapper.sendRequestWithBackoff(
202+
String.format("/%s/translate", this.apiVersion), params);
199203
checkResponse(response, false, false);
200204
return jsonParser.parseTextResult(response.getBody());
201205
}
@@ -251,7 +255,8 @@ public List<TextResult> translateText(
251255
* @throws DeepLException If any error occurs while communicating with the DeepL API.
252256
*/
253257
public Usage getUsage() throws DeepLException, InterruptedException {
254-
HttpResponse response = httpClientWrapper.sendGetRequestWithBackoff("/v2/usage");
258+
HttpResponse response =
259+
httpClientWrapper.sendGetRequestWithBackoff(String.format("/%s/usage", apiVersion));
255260
checkResponse(response, false, false);
256261
return jsonParser.parseUsage(response.getBody());
257262
}
@@ -295,7 +300,9 @@ public List<Language> getLanguages(LanguageType languageType)
295300
if (languageType == LanguageType.Target) {
296301
params.add(new KeyValuePair<>("type", "target"));
297302
}
298-
HttpResponse response = httpClientWrapper.sendRequestWithBackoff("/v2/languages", params);
303+
HttpResponse response =
304+
httpClientWrapper.sendRequestWithBackoff(
305+
String.format("/%s/languages", apiVersion), params);
299306
checkResponse(response, false, false);
300307
return jsonParser.parseLanguages(response.getBody());
301308
}
@@ -312,7 +319,8 @@ public List<Language> getLanguages(LanguageType languageType)
312319
public List<GlossaryLanguagePair> getGlossaryLanguages()
313320
throws DeepLException, InterruptedException {
314321
HttpResponse response =
315-
httpClientWrapper.sendGetRequestWithBackoff("/v2/glossary-language-pairs");
322+
httpClientWrapper.sendGetRequestWithBackoff(
323+
String.format("/%s/glossary-language-pairs", apiVersion));
316324
checkResponse(response, false, false);
317325
return jsonParser.parseGlossaryLanguageList(response.getBody());
318326
}
@@ -451,7 +459,7 @@ public DocumentHandle translateDocumentUpload(
451459
try (FileInputStream inputStream = new FileInputStream(inputFile)) {
452460
HttpResponse response =
453461
httpClientWrapper.uploadWithBackoff(
454-
"/v2/document", params, inputFile.getName(), inputStream);
462+
String.format("/%s/document", apiVersion), params, inputFile.getName(), inputStream);
455463
checkResponse(response, false, false);
456464
return jsonParser.parseDocumentHandle(response.getBody());
457465
}
@@ -495,7 +503,8 @@ public DocumentHandle translateDocumentUpload(
495503
Iterable<KeyValuePair<String, String>> params =
496504
createHttpParams(sourceLang, targetLang, options);
497505
HttpResponse response =
498-
httpClientWrapper.uploadWithBackoff("/v2/document/", params, fileName, inputStream);
506+
httpClientWrapper.uploadWithBackoff(
507+
String.format("/%s/document/", apiVersion), params, fileName, inputStream);
499508
checkResponse(response, false, false);
500509
return jsonParser.parseDocumentHandle(response.getBody());
501510
}
@@ -525,7 +534,7 @@ public DocumentStatus translateDocumentStatus(DocumentHandle handle)
525534
throws DeepLException, InterruptedException {
526535
ArrayList<KeyValuePair<String, String>> params = new ArrayList<>();
527536
params.add(new KeyValuePair<>("document_key", handle.getDocumentKey()));
528-
String relativeUrl = String.format("/v2/document/%s", handle.getDocumentId());
537+
String relativeUrl = String.format("/%s/document/%s", apiVersion, handle.getDocumentId());
529538
HttpResponse response = httpClientWrapper.sendRequestWithBackoff(relativeUrl, params);
530539
checkResponse(response, false, false);
531540
return jsonParser.parseDocumentStatus(response.getBody());
@@ -599,7 +608,8 @@ public void translateDocumentDownload(DocumentHandle handle, OutputStream output
599608
throws DeepLException, IOException, InterruptedException {
600609
ArrayList<KeyValuePair<String, String>> params = new ArrayList<>();
601610
params.add(new KeyValuePair<>("document_key", handle.getDocumentKey()));
602-
String relativeUrl = String.format("/v2/document/%s/result", handle.getDocumentId());
611+
String relativeUrl =
612+
String.format("/%s/document/%s/result", apiVersion, handle.getDocumentId());
603613
try (HttpResponseStream response = httpClientWrapper.downloadWithBackoff(relativeUrl, params)) {
604614
checkResponse(response);
605615
assert response.getBody() != null;
@@ -682,7 +692,7 @@ public GlossaryInfo createGlossaryFromCsv(
682692
* @throws DeepLException If any error occurs while communicating with the DeepL API.
683693
*/
684694
public GlossaryInfo getGlossary(String glossaryId) throws DeepLException, InterruptedException {
685-
String relativeUrl = String.format("/v2/glossaries/%s", glossaryId);
695+
String relativeUrl = String.format("/%s/glossaries/%s", apiVersion, glossaryId);
686696
HttpResponse response = httpClientWrapper.sendGetRequestWithBackoff(relativeUrl);
687697
checkResponse(response, false, true);
688698
return jsonParser.parseGlossaryInfo(response.getBody());
@@ -698,7 +708,8 @@ public GlossaryInfo getGlossary(String glossaryId) throws DeepLException, Interr
698708
* @throws DeepLException If any error occurs while communicating with the DeepL API.
699709
*/
700710
public List<GlossaryInfo> listGlossaries() throws DeepLException, InterruptedException {
701-
HttpResponse response = httpClientWrapper.sendGetRequestWithBackoff("/v2/glossaries");
711+
HttpResponse response =
712+
httpClientWrapper.sendGetRequestWithBackoff(String.format("/%s/glossaries", apiVersion));
702713
checkResponse(response, false, false);
703714
return jsonParser.parseGlossaryInfoList(response.getBody());
704715
}
@@ -729,7 +740,7 @@ public GlossaryEntries getGlossaryEntries(GlossaryInfo glossary)
729740
*/
730741
public GlossaryEntries getGlossaryEntries(String glossaryId)
731742
throws DeepLException, InterruptedException {
732-
String relativeUrl = String.format("/v2/glossaries/%s/entries", glossaryId);
743+
String relativeUrl = String.format("/%s/glossaries/%s/entries", apiVersion, glossaryId);
733744
HttpResponse response = httpClientWrapper.sendGetRequestWithBackoff(relativeUrl);
734745
checkResponse(response, false, true);
735746
return GlossaryEntries.fromTsv(response.getBody());
@@ -754,7 +765,7 @@ public void deleteGlossary(GlossaryInfo glossary) throws DeepLException, Interru
754765
* @throws DeepLException If any error occurs while communicating with the DeepL API.
755766
*/
756767
public void deleteGlossary(String glossaryId) throws DeepLException, InterruptedException {
757-
String relativeUrl = String.format("/v2/glossaries/%s", glossaryId);
768+
String relativeUrl = String.format("/%s/glossaries/%s", apiVersion, glossaryId);
758769
HttpResponse response = httpClientWrapper.sendDeleteRequestWithBackoff(relativeUrl);
759770
checkResponse(response, false, true);
760771
}
@@ -954,7 +965,9 @@ private GlossaryInfo createGlossaryInternal(
954965
params.add(new KeyValuePair<>("target_lang", targetLang));
955966
params.add(new KeyValuePair<>("entries_format", entriesFormat));
956967
params.add(new KeyValuePair<>("entries", entries));
957-
HttpResponse response = httpClientWrapper.sendRequestWithBackoff("/v2/glossaries", params);
968+
HttpResponse response =
969+
httpClientWrapper.sendRequestWithBackoff(
970+
String.format("/%s/glossaries", apiVersion), params);
958971
checkResponse(response, false, false);
959972
return jsonParser.parseGlossaryInfo(response.getBody());
960973
}

deepl-java/src/main/java/com/deepl/api/TranslatorOptions.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ public class TranslatorOptions {
2626
@Nullable private String serverUrl = null;
2727
private boolean sendPlatformInfo = true;
2828
@Nullable private AppInfo appInfo = null;
29+
@Nullable protected DeepLApiVersion apiVersion = null;
2930

3031
/** @deprecated Use {@link DeepLClient} instead. */
3132
@Deprecated
32-
public TranslatorOptions() {}
33+
public TranslatorOptions() {
34+
apiVersion = DeepLApiVersion.VERSION_2;
35+
}
3336

3437
/**
3538
* Set the maximum number of failed attempts that {@link Translator} will retry, per request. By

0 commit comments

Comments
 (0)