Commit 2a752159 authored by Thomas's avatar Thomas
Browse files

- Fix #1346 - DeepL translation by using Authorization header

parent 2385de94
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -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);
+19 −0
Original line number Diff line number Diff line
@@ -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) {
+15 −7
Original line number Diff line number Diff line
@@ -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;
@@ -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;
    }