@@ -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 }
0 commit comments