Skip to content

Commit a2b6723

Browse files
committed
feat: Allow passing extra parameters to API requests
1 parent 6ac8027 commit a2b6723

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

deepl/translator.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ def translate_text(
368368
splitting_tags: Union[str, List[str], None] = None,
369369
ignore_tags: Union[str, List[str], None] = None,
370370
model_type: Union[str, ModelType, None] = None,
371+
extra_body_parameters: Optional[dict] = None,
371372
) -> Union[TextResult, List[TextResult]]:
372373
"""Translate text(s) into the target language.
373374
@@ -410,6 +411,11 @@ def translate_text(
410411
:type ignore_tags: List of XML tags or comma-separated-list of tags.
411412
:param model_type: (Optional) Controls whether the translation engine
412413
should use a potentially slower model to achieve higher quality.
414+
:param extra_body_parameters: (Optional) Additional key/value pairs to
415+
include in the JSON request body sent to the API. If provided,
416+
keys in this dict will be added to the request body. Existing
417+
keys set by the client will not be overwritten by entries in
418+
extra_body_parameters.
413419
:return: List of TextResult objects containing results, unless input
414420
text was one string, then a single TextResult object is returned.
415421
"""
@@ -467,6 +473,13 @@ def join_tags(tag_argument: Union[str, Iterable[str]]) -> List[str]:
467473
if ignore_tags is not None:
468474
request_data["ignore_tags"] = join_tags(ignore_tags)
469475

476+
# Do not overwrite keys that were explicitly set by this method.
477+
if extra_body_parameters:
478+
for k, v in extra_body_parameters.items():
479+
if k in request_data:
480+
continue
481+
request_data[k] = v
482+
470483
status, content, json = self._api_call(
471484
"v2/translate", json=request_data
472485
)
@@ -558,6 +571,7 @@ def translate_document_from_filepath(
558571
str, GlossaryInfo, MultilingualGlossaryInfo, None
559572
] = None,
560573
timeout_s: Optional[int] = None,
574+
extra_body_parameters: Optional[dict] = None,
561575
) -> DocumentStatus:
562576
"""Upload document at given input path, translate it into the target
563577
language, and download result to given output path.
@@ -576,6 +590,11 @@ def translate_document_from_filepath(
576590
:param timeout_s: (beta) (Optional) Maximum time to wait before
577591
the call raises an error. Note that this is not accurate to the
578592
second, but only polls every 5 seconds.
593+
:param extra_body_parameters: (Optional) Additional key/value pairs to
594+
include in the JSON request body sent to the API. If provided,
595+
keys in this dict will be added to the request body. Existing
596+
keys set by the client will not be overwritten by entries in
597+
extra_body_parameters.
579598
:return: DocumentStatus when document translation completed, this
580599
allows the number of billed characters to be queried.
581600
@@ -600,6 +619,7 @@ def translate_document_from_filepath(
600619
glossary=glossary,
601620
output_format=output_format,
602621
timeout_s=timeout_s,
622+
extra_body_parameters=extra_body_parameters,
603623
)
604624
except Exception as e:
605625
out_file.close()
@@ -620,6 +640,7 @@ def translate_document(
620640
filename: Optional[str] = None,
621641
output_format: Optional[str] = None,
622642
timeout_s: Optional[int] = None,
643+
extra_body_parameters: Optional[dict] = None,
623644
) -> DocumentStatus:
624645
"""Upload document, translate it into the target language, and download
625646
result.
@@ -644,6 +665,11 @@ def translate_document(
644665
:param timeout_s: (beta) (Optional) Maximum time to wait before
645666
the call raises an error. Note that this is not accurate to the
646667
second, but only polls every 5 seconds.
668+
:param extra_body_parameters: (Optional) Additional key/value pairs to
669+
include in the JSON request body sent to the API. If provided,
670+
keys in this dict will be added to the request body. Existing
671+
keys set by the client will not be overwritten by entries in
672+
extra_body_parameters.
647673
:return: DocumentStatus when document translation completed, this
648674
allows the number of billed characters to be queried.
649675
@@ -659,6 +685,7 @@ def translate_document(
659685
glossary=glossary,
660686
filename=filename,
661687
output_format=output_format,
688+
extra_body_parameters=extra_body_parameters,
662689
)
663690

664691
try:
@@ -688,6 +715,7 @@ def translate_document_upload(
688715
] = None,
689716
filename: Optional[str] = None,
690717
output_format: Optional[str] = None,
718+
extra_body_parameters: Optional[dict] = None,
691719
) -> DocumentHandle:
692720
"""Upload document to be translated and return handle associated with
693721
request.
@@ -707,6 +735,11 @@ def translate_document_upload(
707735
if uploading string or bytes containing file content.
708736
:param output_format: (Optional) Desired output file extension, if
709737
it differs from the input file format.
738+
:param extra_body_parameters: (Optional) Additional key/value pairs to
739+
include in the JSON request body sent to the API. If provided,
740+
keys in this dict will be added to the request body. Existing
741+
keys set by the client will not be overwritten by entries in
742+
extra_body_parameters.
710743
:return: DocumentHandle with ID and key identifying document.
711744
"""
712745

@@ -726,6 +759,12 @@ def translate_document_upload(
726759
files = {"file": (filename, input_document)}
727760
else:
728761
files = {"file": input_document}
762+
if extra_body_parameters:
763+
for k, v in extra_body_parameters.items():
764+
if k in request_data:
765+
continue
766+
request_data[k] = v
767+
729768
status, content, json = self._api_call(
730769
"v2/document", data=request_data, files=files
731770
)

0 commit comments

Comments
 (0)