Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions deepl-java/src/main/java/com/deepl/api/DeepLApiVersion.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2025 DeepL SE (https://www.deepl.com)
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
package com.deepl.api;

public enum DeepLApiVersion {
Expand Down
12 changes: 8 additions & 4 deletions deepl-java/src/main/java/com/deepl/api/Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,24 @@ public class Translator {
*/
@Deprecated
public Translator(String authKey, TranslatorOptions options) throws IllegalArgumentException {
if (authKey == null || authKey.length() == 0) {
throw new IllegalArgumentException("authKey must be a non-empty string");
if (authKey == null || authKey.isEmpty()) {
throw new IllegalArgumentException("authKey cannot be null or empty");
}

String sanitizedAuthKey = authKey.trim();
this.apiVersion = options.apiVersion;
String serverUrl =
(options.getServerUrl() != null)
? options.getServerUrl()
: (isFreeAccountAuthKey(authKey) ? DEEPL_SERVER_URL_FREE : DEEPL_SERVER_URL_PRO);
: (isFreeAccountAuthKey(sanitizedAuthKey)
? DEEPL_SERVER_URL_FREE
: DEEPL_SERVER_URL_PRO);

Map<String, String> headers = new HashMap<>();
if (options.getHeaders() != null) {
headers.putAll(options.getHeaders());
}
headers.putIfAbsent("Authorization", "DeepL-Auth-Key " + authKey);
headers.putIfAbsent("Authorization", "DeepL-Auth-Key " + sanitizedAuthKey);
headers.putIfAbsent(
"User-Agent",
constructUserAgentString(options.getSendPlatformInfo(), options.getAppInfo()));
Expand Down
10 changes: 10 additions & 0 deletions deepl-java/src/test/java/com/deepl/api/GeneralTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ void testEmptyAuthKey() {
});
}

@Test
void testNullAuthKey() {
IllegalArgumentException thrown =
Assertions.assertThrows(
IllegalArgumentException.class,
() -> {
Translator translator = new Translator(null);
});
}

@Test
void testInvalidAuthKey() {
String authKey = "invalid";
Expand Down