Loading mytransl/src/main/java/com/github/stom79/mytransl/async/TransAsync.java +3 −2 Original line number Diff line number Diff line Loading @@ -127,8 +127,9 @@ public class TransAsync { } else if (te == MyTransL.translatorEngine.DEEPL) { String key = MyTransL.getInstance(te).getDeeplAPIKey(); params.setSplit_sentences(true); url = Helper.getDeeplAbsoluteUrl(contentToSend, toLanguage, params, key); str_response = new Client().get(url, this.timeout); url = Helper.getDeeplAbsoluteUrl(contentToSend, toLanguage, params, params.isPro()); String authHeader = Helper.getDeeplAuthorizationHeader(key); str_response = new Client().get(url, this.timeout, authHeader); } else if (te == MyTransL.translatorEngine.SYSTRAN) { String key = MyTransL.getInstance(te).getSystranAPIKey(); url = Helper.getSystranAbsoluteUrl(contentToSend, key, toLanguage); Loading mytransl/src/main/java/com/github/stom79/mytransl/client/Client.java +19 −0 Original line number Diff line number Diff line Loading @@ -59,11 +59,30 @@ public class Client { */ @SuppressWarnings({"SameParameterValue"}) public String get(String urlConnection, int timeout) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException { return get(urlConnection, timeout, null); } /*** * Get call to the translator API with Authorization header * @param urlConnection - String url to query * @param timeout - int a timeout * @param authorizationHeader - String authorization header value (can be null) * @return response - String * @throws IOException - Exception * @throws NoSuchAlgorithmException - Exception * @throws KeyManagementException - Exception * @throws HttpsConnectionException - Exception */ @SuppressWarnings({"SameParameterValue"}) public String get(String urlConnection, int timeout, String authorizationHeader) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException { URL url = new URL(urlConnection); HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); httpsURLConnection.setConnectTimeout(timeout * 1000); httpsURLConnection.setRequestProperty("http.keepAlive", "false"); httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); if (authorizationHeader != null) { httpsURLConnection.setRequestProperty("Authorization", authorizationHeader); } httpsURLConnection.setRequestMethod("GET"); //Read the reply if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { Loading mytransl/src/main/java/com/github/stom79/mytransl/translate/Helper.java +15 −7 Original line number Diff line number Diff line Loading @@ -62,11 +62,10 @@ public class Helper { * @param content String - Content to translate * @param toLanguage String - The targeted locale * @param deepLParams DeepLParams - The deepl paramaters see: https://www.deepl.com/api.html#api_reference_article * @param apikey String - The Deepl API Key * @return String - absolute URL for Deepl * @param isPro boolean - Whether to use pro API endpoint * @return String - absolute URL for Deepl (without auth_key, use Authorization header instead) */ public static String getDeeplAbsoluteUrl(String content, String toLanguage, Params deepLParams, String apikey) { String key = "&auth_key=" + apikey; public static String getDeeplAbsoluteUrl(String content, String toLanguage, Params deepLParams, boolean isPro) { toLanguage = toLanguage.replace("null", ""); String lang = "target_lang=" + toLanguage.toUpperCase(); String text; Loading Loading @@ -99,11 +98,20 @@ public class Helper { if (deepLParams.getNon_splitting_tags() != null) params += "&tag_handling=" + deepLParams.getNon_splitting_tags(); if (deepLParams.isPro()) { return Helper.DEEPL_BASE_URL + text + lang + params + key; if (isPro) { return Helper.DEEPL_BASE_URL + text + lang + params; } else { return Helper.DEEPL_BASE_FREE_URL + text + lang + params + key; return Helper.DEEPL_BASE_FREE_URL + text + lang + params; } } /*** * Returns the Authorization header value for DeepL API * @param apikey String - The Deepl API Key * @return String - Authorization header value */ public static String getDeeplAuthorizationHeader(String apikey) { return "DeepL-Auth-Key " + apikey; } Loading Loading
mytransl/src/main/java/com/github/stom79/mytransl/async/TransAsync.java +3 −2 Original line number Diff line number Diff line Loading @@ -127,8 +127,9 @@ public class TransAsync { } else if (te == MyTransL.translatorEngine.DEEPL) { String key = MyTransL.getInstance(te).getDeeplAPIKey(); params.setSplit_sentences(true); url = Helper.getDeeplAbsoluteUrl(contentToSend, toLanguage, params, key); str_response = new Client().get(url, this.timeout); url = Helper.getDeeplAbsoluteUrl(contentToSend, toLanguage, params, params.isPro()); String authHeader = Helper.getDeeplAuthorizationHeader(key); str_response = new Client().get(url, this.timeout, authHeader); } else if (te == MyTransL.translatorEngine.SYSTRAN) { String key = MyTransL.getInstance(te).getSystranAPIKey(); url = Helper.getSystranAbsoluteUrl(contentToSend, key, toLanguage); Loading
mytransl/src/main/java/com/github/stom79/mytransl/client/Client.java +19 −0 Original line number Diff line number Diff line Loading @@ -59,11 +59,30 @@ public class Client { */ @SuppressWarnings({"SameParameterValue"}) public String get(String urlConnection, int timeout) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException { return get(urlConnection, timeout, null); } /*** * Get call to the translator API with Authorization header * @param urlConnection - String url to query * @param timeout - int a timeout * @param authorizationHeader - String authorization header value (can be null) * @return response - String * @throws IOException - Exception * @throws NoSuchAlgorithmException - Exception * @throws KeyManagementException - Exception * @throws HttpsConnectionException - Exception */ @SuppressWarnings({"SameParameterValue"}) public String get(String urlConnection, int timeout, String authorizationHeader) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException { URL url = new URL(urlConnection); HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); httpsURLConnection.setConnectTimeout(timeout * 1000); httpsURLConnection.setRequestProperty("http.keepAlive", "false"); httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); if (authorizationHeader != null) { httpsURLConnection.setRequestProperty("Authorization", authorizationHeader); } httpsURLConnection.setRequestMethod("GET"); //Read the reply if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { Loading
mytransl/src/main/java/com/github/stom79/mytransl/translate/Helper.java +15 −7 Original line number Diff line number Diff line Loading @@ -62,11 +62,10 @@ public class Helper { * @param content String - Content to translate * @param toLanguage String - The targeted locale * @param deepLParams DeepLParams - The deepl paramaters see: https://www.deepl.com/api.html#api_reference_article * @param apikey String - The Deepl API Key * @return String - absolute URL for Deepl * @param isPro boolean - Whether to use pro API endpoint * @return String - absolute URL for Deepl (without auth_key, use Authorization header instead) */ public static String getDeeplAbsoluteUrl(String content, String toLanguage, Params deepLParams, String apikey) { String key = "&auth_key=" + apikey; public static String getDeeplAbsoluteUrl(String content, String toLanguage, Params deepLParams, boolean isPro) { toLanguage = toLanguage.replace("null", ""); String lang = "target_lang=" + toLanguage.toUpperCase(); String text; Loading Loading @@ -99,11 +98,20 @@ public class Helper { if (deepLParams.getNon_splitting_tags() != null) params += "&tag_handling=" + deepLParams.getNon_splitting_tags(); if (deepLParams.isPro()) { return Helper.DEEPL_BASE_URL + text + lang + params + key; if (isPro) { return Helper.DEEPL_BASE_URL + text + lang + params; } else { return Helper.DEEPL_BASE_FREE_URL + text + lang + params + key; return Helper.DEEPL_BASE_FREE_URL + text + lang + params; } } /*** * Returns the Authorization header value for DeepL API * @param apikey String - The Deepl API Key * @return String - Authorization header value */ public static String getDeeplAuthorizationHeader(String apikey) { return "DeepL-Auth-Key " + apikey; } Loading