@@ -26,6 +26,7 @@ public class Translator {
26
26
27
27
protected final Parser jsonParser = new Parser ();
28
28
protected final HttpClientWrapper httpClientWrapper ;
29
+ protected final DeepLApiVersion apiVersion ;
29
30
30
31
/**
31
32
* Initializes a new Translator object using your Authentication Key.
@@ -44,6 +45,7 @@ public Translator(String authKey, TranslatorOptions options) throws IllegalArgum
44
45
if (authKey == null || authKey .length () == 0 ) {
45
46
throw new IllegalArgumentException ("authKey must be a non-empty string" );
46
47
}
48
+ this .apiVersion = options .apiVersion ;
47
49
String serverUrl =
48
50
(options .getServerUrl () != null )
49
51
? options .getServerUrl ()
@@ -195,7 +197,9 @@ public List<TextResult> translateText(
195
197
throws DeepLException , InterruptedException {
196
198
Iterable <KeyValuePair <String , String >> params =
197
199
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 );
199
203
checkResponse (response , false , false );
200
204
return jsonParser .parseTextResult (response .getBody ());
201
205
}
@@ -251,7 +255,8 @@ public List<TextResult> translateText(
251
255
* @throws DeepLException If any error occurs while communicating with the DeepL API.
252
256
*/
253
257
public Usage getUsage () throws DeepLException , InterruptedException {
254
- HttpResponse response = httpClientWrapper .sendGetRequestWithBackoff ("/v2/usage" );
258
+ HttpResponse response =
259
+ httpClientWrapper .sendGetRequestWithBackoff (String .format ("/%s/usage" , apiVersion ));
255
260
checkResponse (response , false , false );
256
261
return jsonParser .parseUsage (response .getBody ());
257
262
}
@@ -295,7 +300,9 @@ public List<Language> getLanguages(LanguageType languageType)
295
300
if (languageType == LanguageType .Target ) {
296
301
params .add (new KeyValuePair <>("type" , "target" ));
297
302
}
298
- HttpResponse response = httpClientWrapper .sendRequestWithBackoff ("/v2/languages" , params );
303
+ HttpResponse response =
304
+ httpClientWrapper .sendRequestWithBackoff (
305
+ String .format ("/%s/languages" , apiVersion ), params );
299
306
checkResponse (response , false , false );
300
307
return jsonParser .parseLanguages (response .getBody ());
301
308
}
@@ -312,7 +319,8 @@ public List<Language> getLanguages(LanguageType languageType)
312
319
public List <GlossaryLanguagePair > getGlossaryLanguages ()
313
320
throws DeepLException , InterruptedException {
314
321
HttpResponse response =
315
- httpClientWrapper .sendGetRequestWithBackoff ("/v2/glossary-language-pairs" );
322
+ httpClientWrapper .sendGetRequestWithBackoff (
323
+ String .format ("/%s/glossary-language-pairs" , apiVersion ));
316
324
checkResponse (response , false , false );
317
325
return jsonParser .parseGlossaryLanguageList (response .getBody ());
318
326
}
@@ -451,7 +459,7 @@ public DocumentHandle translateDocumentUpload(
451
459
try (FileInputStream inputStream = new FileInputStream (inputFile )) {
452
460
HttpResponse response =
453
461
httpClientWrapper .uploadWithBackoff (
454
- "/v2 /document" , params , inputFile .getName (), inputStream );
462
+ String . format ( "/%s /document", apiVersion ) , params , inputFile .getName (), inputStream );
455
463
checkResponse (response , false , false );
456
464
return jsonParser .parseDocumentHandle (response .getBody ());
457
465
}
@@ -495,7 +503,8 @@ public DocumentHandle translateDocumentUpload(
495
503
Iterable <KeyValuePair <String , String >> params =
496
504
createHttpParams (sourceLang , targetLang , options );
497
505
HttpResponse response =
498
- httpClientWrapper .uploadWithBackoff ("/v2/document/" , params , fileName , inputStream );
506
+ httpClientWrapper .uploadWithBackoff (
507
+ String .format ("/%s/document/" , apiVersion ), params , fileName , inputStream );
499
508
checkResponse (response , false , false );
500
509
return jsonParser .parseDocumentHandle (response .getBody ());
501
510
}
@@ -525,7 +534,7 @@ public DocumentStatus translateDocumentStatus(DocumentHandle handle)
525
534
throws DeepLException , InterruptedException {
526
535
ArrayList <KeyValuePair <String , String >> params = new ArrayList <>();
527
536
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 ());
529
538
HttpResponse response = httpClientWrapper .sendRequestWithBackoff (relativeUrl , params );
530
539
checkResponse (response , false , false );
531
540
return jsonParser .parseDocumentStatus (response .getBody ());
@@ -599,7 +608,8 @@ public void translateDocumentDownload(DocumentHandle handle, OutputStream output
599
608
throws DeepLException , IOException , InterruptedException {
600
609
ArrayList <KeyValuePair <String , String >> params = new ArrayList <>();
601
610
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 ());
603
613
try (HttpResponseStream response = httpClientWrapper .downloadWithBackoff (relativeUrl , params )) {
604
614
checkResponse (response );
605
615
assert response .getBody () != null ;
@@ -682,7 +692,7 @@ public GlossaryInfo createGlossaryFromCsv(
682
692
* @throws DeepLException If any error occurs while communicating with the DeepL API.
683
693
*/
684
694
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 );
686
696
HttpResponse response = httpClientWrapper .sendGetRequestWithBackoff (relativeUrl );
687
697
checkResponse (response , false , true );
688
698
return jsonParser .parseGlossaryInfo (response .getBody ());
@@ -698,7 +708,8 @@ public GlossaryInfo getGlossary(String glossaryId) throws DeepLException, Interr
698
708
* @throws DeepLException If any error occurs while communicating with the DeepL API.
699
709
*/
700
710
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 ));
702
713
checkResponse (response , false , false );
703
714
return jsonParser .parseGlossaryInfoList (response .getBody ());
704
715
}
@@ -729,7 +740,7 @@ public GlossaryEntries getGlossaryEntries(GlossaryInfo glossary)
729
740
*/
730
741
public GlossaryEntries getGlossaryEntries (String glossaryId )
731
742
throws DeepLException , InterruptedException {
732
- String relativeUrl = String .format ("/v2 /glossaries/%s/entries" , glossaryId );
743
+ String relativeUrl = String .format ("/%s /glossaries/%s/entries" , apiVersion , glossaryId );
733
744
HttpResponse response = httpClientWrapper .sendGetRequestWithBackoff (relativeUrl );
734
745
checkResponse (response , false , true );
735
746
return GlossaryEntries .fromTsv (response .getBody ());
@@ -754,7 +765,7 @@ public void deleteGlossary(GlossaryInfo glossary) throws DeepLException, Interru
754
765
* @throws DeepLException If any error occurs while communicating with the DeepL API.
755
766
*/
756
767
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 );
758
769
HttpResponse response = httpClientWrapper .sendDeleteRequestWithBackoff (relativeUrl );
759
770
checkResponse (response , false , true );
760
771
}
@@ -954,7 +965,9 @@ private GlossaryInfo createGlossaryInternal(
954
965
params .add (new KeyValuePair <>("target_lang" , targetLang ));
955
966
params .add (new KeyValuePair <>("entries_format" , entriesFormat ));
956
967
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 );
958
971
checkResponse (response , false , false );
959
972
return jsonParser .parseGlossaryInfo (response .getBody ());
960
973
}
0 commit comments