Skip to content

Commit ced2c57

Browse files
hive-botclaude
andcommitted
feat: add extra_body_parameters to the spoken-terms write methods
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 62f9d7d commit ced2c57

4 files changed

Lines changed: 61 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5757
`update_spoken_terms()`, `get_spoken_terms_entries()`,
5858
`replace_spoken_terms_list()`, `delete_spoken_terms()` and
5959
`delete_spoken_terms_list()`, with the `SpokenTerms`, `SpokenTermsList` and
60-
`SpokenTermsEntries` types.
60+
`SpokenTermsEntries` types. `create_spoken_terms()`, `update_spoken_terms()`
61+
and `replace_spoken_terms_list()` accept `extra_body_parameters`. Collection
62+
IDs are URL-encoded in the request path, terms are stripped of leading and
63+
trailing whitespace before they are validated and sent, and a malformed
64+
response raises `DeepLException`.
6165
- Added the style profile API (`/v3/style_profiles`) to the `DeepLClient`
6266
class: `list_style_profiles()`, `create_style_profile()`,
6367
`get_style_profile()`, `update_style_profile()` and

‎README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,10 @@ term, then checks it like a glossary term (`validate_glossary_term()`): an
646646
empty term or one with a control character, such as a newline, raises
647647
`ValueError`. The API validates the rest (at most 300 characters per list, no
648648
duplicates). Methods that take a collection accept either its ID or a
649-
`SpokenTerms` object.
649+
`SpokenTerms` object. `create_spoken_terms()`, `update_spoken_terms()` and
650+
`replace_spoken_terms_list()` accept the keyword argument
651+
`extra_body_parameters`: a dictionary of extra fields to send in the JSON
652+
request body, replacing fields the library sets with the same key.
650653

651654
```python
652655
# Create a collection with an English term list

‎deepl/deepl_client.py‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,8 @@ def create_spoken_terms(
730730
self,
731731
name: str,
732732
term_lists: List[SpokenTermsEntries],
733+
*,
734+
extra_body_parameters: Optional[Dict[str, Any]] = None,
733735
) -> SpokenTerms:
734736
"""Creates a spoken-terms collection with the given name and term
735737
lists, one per language. Spoken terms improve the transcription of
@@ -738,18 +740,22 @@ def create_spoken_terms(
738740
:param name: User-defined name to attach to the collection.
739741
:param term_lists: List of SpokenTermsEntries, each containing the
740742
terms of one language.
743+
:param extra_body_parameters: (Optional) Additional key/value pairs
744+
to send in the JSON request body; they replace fields the library
745+
sets with the same key.
741746
:return: SpokenTerms containing information about the created
742747
collection.
743748
:raises ValueError: If a term, after stripping leading and trailing
744749
whitespace, is empty or contains a control character, as
745750
validate_glossary_term() checks.
746751
"""
747-
request_data = {
752+
request_data: Dict[str, Any] = {
748753
"name": name,
749754
"term_lists": [
750755
self._spoken_terms_json(entries) for entries in term_lists
751756
],
752757
}
758+
request_data.update(extra_body_parameters or {})
753759
status, content, json = self._api_call(
754760
"v3/spoken-terms", json=request_data
755761
)
@@ -795,6 +801,7 @@ def update_spoken_terms(
795801
*,
796802
name: Optional[str] = None,
797803
term_lists: Optional[List[SpokenTermsEntries]] = None,
804+
extra_body_parameters: Optional[Dict[str, Any]] = None,
798805
) -> SpokenTerms:
799806
"""Updates a spoken-terms collection: renames it and/or merges the
800807
given terms into the existing term list of each given language.
@@ -803,6 +810,9 @@ def update_spoken_terms(
803810
:param name: (Optional) New name of the collection.
804811
:param term_lists: (Optional) List of SpokenTermsEntries whose terms
805812
are merged into the term list of the same language.
813+
:param extra_body_parameters: (Optional) Additional key/value pairs
814+
to send in the JSON request body; they replace fields the library
815+
sets with the same key.
806816
:return: SpokenTerms containing information about the updated
807817
collection.
808818
:raises ValueError: If a term, after stripping leading and trailing
@@ -817,6 +827,7 @@ def update_spoken_terms(
817827
request_data["term_lists"] = [
818828
self._spoken_terms_json(entries) for entries in term_lists
819829
]
830+
request_data.update(extra_body_parameters or {})
820831
status, content, json = self._api_call(
821832
f"v3/spoken-terms/{spoken_terms_id}",
822833
method="PATCH",
@@ -849,23 +860,30 @@ def replace_spoken_terms_list(
849860
self,
850861
spoken_terms: Union[str, SpokenTerms],
851862
entries: SpokenTermsEntries,
863+
*,
864+
extra_body_parameters: Optional[Dict[str, Any]] = None,
852865
) -> SpokenTermsList:
853866
"""Replaces the term list of one language in a spoken-terms
854867
collection, or adds it if the collection has no list for the
855868
language.
856869
857870
:param spoken_terms: SpokenTerms or ID of the collection.
858871
:param entries: SpokenTermsEntries with the language and its terms.
872+
:param extra_body_parameters: (Optional) Additional key/value pairs
873+
to send in the JSON request body; they replace fields the library
874+
sets with the same key.
859875
:return: SpokenTermsList containing information about the term list.
860876
:raises ValueError: If a term, after stripping leading and trailing
861877
whitespace, is empty or contains a control character, as
862878
validate_glossary_term() checks.
863879
"""
864880
spoken_terms_id = self._spoken_terms_id(spoken_terms)
881+
request_data = self._spoken_terms_json(entries)
882+
request_data.update(extra_body_parameters or {})
865883
status, content, json = self._api_call(
866884
f"v3/spoken-terms/{spoken_terms_id}/term-lists",
867885
method="PUT",
868-
json=self._spoken_terms_json(entries),
886+
json=request_data,
869887
)
870888
self._raise_for_status(status, content, json)
871889
return self._parse_spoken_terms_response(

‎tests/offline/test_spoken_terms.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,35 @@ def test_raises_deepl_exception_on_a_malformed_response(
145145

146146
with pytest.raises(deepl.DeepLException, match="Malformed"):
147147
call(make_deepl_client())
148+
149+
150+
def test_sends_extra_body_parameters_last(mocked):
151+
mocked.post(
152+
url("v3/spoken-terms"),
153+
json=SPOKEN_TERMS_JSON,
154+
status=201,
155+
match=json_request({"name": "override", "term_lists": [], "x": 1}),
156+
)
157+
mocked.patch(
158+
url("v3/spoken-terms/st-1"),
159+
json=SPOKEN_TERMS_JSON,
160+
match=json_request({"name": "renamed", "x": True}),
161+
)
162+
mocked.put(
163+
url("v3/spoken-terms/st-1/term-lists"),
164+
json={"lang": "de", "entry_count": 1},
165+
match=json_request({"lang": "de", "entries": "a", "x": None}),
166+
)
167+
client = make_deepl_client()
168+
169+
client.create_spoken_terms(
170+
"terms", [], extra_body_parameters={"name": "override", "x": 1}
171+
)
172+
client.update_spoken_terms(
173+
"st-1", name="renamed", extra_body_parameters={"x": True}
174+
)
175+
client.replace_spoken_terms_list(
176+
"st-1",
177+
deepl.SpokenTermsEntries("en", ["a"]),
178+
extra_body_parameters={"lang": "de", "x": None},
179+
)

0 commit comments

Comments
 (0)