Skip to content

Commit 85695f9

Browse files
author
Lucas Mathis
committed
feat: add support for extra request params
1 parent e3fd5b8 commit 85695f9

File tree

6 files changed

+80
-11
lines changed

6 files changed

+80
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8-
### Added
9-
<!-- * add to here -->
10-
### Changed
11-
<!-- * add to here -->
128

9+
### Added
10+
* Added `extraRequestParameters` option to text and document translation methods to pass arbitrary parameters in the request body. This can be used to access beta features or override built-in parameters (such as `target_lang`, `source_lang`, etc.).
1311

1412
## [1.10.3] - 2025-08-22
1513
### Security
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 DeepL SE (https://www.deepl.com)
2+
// Use of this source code is governed by an MIT
3+
// license that can be found in the LICENSE file.
4+
package com.deepl.api;
5+
6+
import java.util.Map;
7+
8+
/** Base class for request options providing common functionality for all endpoints. */
9+
public abstract class BaseRequestOptions {
10+
private Map<String, String> extraBodyParameters;
11+
12+
/**
13+
* Sets additional parameters to pass in the body of the HTTP request. Can be used to access beta
14+
* features, override built-in parameters, or for testing purposes. Keys in this map will be added
15+
* to the request body and can override existing keys.
16+
*
17+
* @param extraBodyParameters Map of additional parameters to include in the request.
18+
* @return This options object for method chaining.
19+
*/
20+
public BaseRequestOptions setExtraBodyParameters(Map<String, String> extraBodyParameters) {
21+
this.extraBodyParameters = extraBodyParameters;
22+
return this;
23+
}
24+
25+
/** Gets the current extra body parameters. */
26+
public Map<String, String> getExtraBodyParameters() {
27+
return extraBodyParameters;
28+
}
29+
}

deepl-java/src/main/java/com/deepl/api/DocumentTranslationOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* .setFormality(Formality.Less).setGlossaryId("f63c02c5-f056-..");
1414
* </code>
1515
*/
16-
public class DocumentTranslationOptions {
16+
public class DocumentTranslationOptions extends BaseRequestOptions {
1717
private Formality formality;
1818
private String glossaryId;
1919

deepl-java/src/main/java/com/deepl/api/TextTranslationOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* .setFormality(Formality.Less).setGlossaryId("f63c02c5-f056-..");
1414
* </code>
1515
*/
16-
public class TextTranslationOptions {
16+
public class TextTranslationOptions extends BaseRequestOptions {
1717
private Formality formality;
1818
private String glossaryId;
1919
private SentenceSplittingMode sentenceSplittingMode;

deepl-java/src/main/java/com/deepl/api/Translator.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ private static ArrayList<KeyValuePair<String, String>> createHttpParams(
846846
if (options.getIgnoreTags() != null) {
847847
params.add(new KeyValuePair<>("ignore_tags", joinTags(options.getIgnoreTags())));
848848
}
849+
addExtraBodyParameters(params, options.getExtraBodyParameters());
849850
}
850851
return params;
851852
}
@@ -862,11 +863,16 @@ private static ArrayList<KeyValuePair<String, String>> createHttpParams(
862863
*/
863864
protected static ArrayList<KeyValuePair<String, String>> createHttpParams(
864865
String sourceLang, String targetLang, DocumentTranslationOptions options) {
865-
return createHttpParamsCommon(
866-
sourceLang,
867-
targetLang,
868-
options != null ? options.getFormality() : null,
869-
options != null ? options.getGlossaryId() : null);
866+
ArrayList<KeyValuePair<String, String>> params =
867+
createHttpParamsCommon(
868+
sourceLang,
869+
targetLang,
870+
options != null ? options.getFormality() : null,
871+
options != null ? options.getGlossaryId() : null);
872+
873+
addExtraBodyParameters(params, options != null ? options.getExtraBodyParameters() : null);
874+
875+
return params;
870876
}
871877

872878
/**
@@ -931,6 +937,23 @@ private static String joinTags(Iterable<String> tags) {
931937
return String.join(",", tags);
932938
}
933939

940+
/**
941+
* Adds extra body parameters to the HTTP request parameters. Extra parameters can override
942+
* existing parameters.
943+
*
944+
* @param params List of HTTP parameters to add to.
945+
* @param extraBodyParameters Map of extra parameters to add (can be null).
946+
*/
947+
private static void addExtraBodyParameters(
948+
ArrayList<KeyValuePair<String, String>> params, Map<String, String> extraBodyParameters) {
949+
if (extraBodyParameters != null) {
950+
params.removeIf(pair -> extraBodyParameters.containsKey(pair.getKey()));
951+
for (Map.Entry<String, String> entry : extraBodyParameters.entrySet()) {
952+
params.add(new KeyValuePair<>(entry.getKey(), entry.getValue()));
953+
}
954+
}
955+
}
956+
934957
/**
935958
* Checks the specified source and target language are valid.
936959
*

deepl-java/src/test/java/com/deepl/api/TranslateTextTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,23 @@ void testMixedCaseLanguages() throws DeepLException, InterruptedException {
330330
Assertions.assertEquals(exampleText.get("en-US"), result.getText().toLowerCase(Locale.ENGLISH));
331331
Assertions.assertEquals("de", result.getDetectedSourceLanguage());
332332
}
333+
334+
@Test
335+
void testExtraBodyParams() throws DeepLException, InterruptedException {
336+
Translator translator = createTranslator();
337+
338+
// Verifies that extra_body_parameters can override standard parameters like target_lang
339+
Map<String, String> extraParams = new HashMap<>();
340+
extraParams.put("target_lang", "FR");
341+
extraParams.put("debug", "1");
342+
343+
TextTranslationOptions options = new TextTranslationOptions();
344+
options.setExtraBodyParameters(extraParams);
345+
346+
TextResult result = translator.translateText(exampleText.get("en"), null, "DE", options);
347+
348+
Assertions.assertEquals(exampleText.get("fr"), result.getText());
349+
Assertions.assertEquals("en", result.getDetectedSourceLanguage());
350+
Assertions.assertEquals(exampleText.get("en").length(), result.getBilledCharacters());
351+
}
333352
}

0 commit comments

Comments
 (0)