Skip to content

Commit 00ca727

Browse files
hive-botclaude
andcommitted
fix: encode a space in query values as %20
All query strings are built with quote instead of urlencode's default quote_plus, so a space is sent as %20 rather than +. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8f8af74 commit 00ca727

5 files changed

Lines changed: 25 additions & 16 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
233233
- TLS and certificate failures, and errors reported by a proxy, are no longer
234234
retried and raise `ConnectionException` with `should_retry` `False`. A
235235
failure to connect to the proxy is still retried.
236+
- Query string values encode a space as `%20` instead of `+`.
236237
- HTTP 401 responses now raise `AuthorizationException`, like 403. Before they
237238
raised `DeepLException` with "Unexpected status code: 401".
238239
- `DeepLException.should_retry` is now `True` for every HTTP 5xx error. It was

‎deepl/admin_client.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from deepl import util
1717
import datetime
1818
import re
19-
import urllib.parse
2019
from typing import Dict, List, Optional, Union
2120

2221

@@ -118,7 +117,7 @@ def get_usage_analytics(
118117
params["group_by"] = str(group_by)
119118

120119
status, content, json = self._api_call(
121-
f"v2/admin/analytics?{urllib.parse.urlencode(params)}",
120+
f"v2/admin/analytics?{util.encode_query(params)}",
122121
method="GET",
123122
)
124123
self._raise_for_status(status, content, json)
@@ -159,7 +158,7 @@ def get_custom_tag_usage_analytics(
159158
params["page"] = str(page)
160159

161160
status, content, json = self._api_call(
162-
f"v2/admin/analytics/custom-tags?{urllib.parse.urlencode(params)}",
161+
f"v2/admin/analytics/custom-tags?{util.encode_query(params)}",
163162
method="GET",
164163
)
165164
self._raise_for_status(status, content, json)

‎deepl/deepl_client.py‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ def _multilingual_glossary_id(
957957
@staticmethod
958958
def _language_pair_query(source_lang: str, target_lang: str) -> str:
959959
# glossaries are only supported for base language types
960-
return urllib.parse.urlencode(
960+
return util.encode_query(
961961
{
962962
"source_lang": Language.remove_regional_variant(source_lang),
963963
"target_lang": Language.remove_regional_variant(target_lang),
@@ -1106,7 +1106,7 @@ def get_spoken_terms_entries(
11061106
:return: SpokenTermsEntries containing the terms.
11071107
"""
11081108
spoken_terms_id = self._spoken_terms_id(spoken_terms)
1109-
query = urllib.parse.urlencode({"lang": lang})
1109+
query = util.encode_query({"lang": lang})
11101110
status, content, json = self._api_call(
11111111
f"v3/spoken-terms/{spoken_terms_id}/entries?{query}",
11121112
method="GET",
@@ -1173,7 +1173,7 @@ def delete_spoken_terms_list(
11731173
:param lang: Language of the term list to delete, e.g. "de".
11741174
"""
11751175
spoken_terms_id = self._spoken_terms_id(spoken_terms)
1176-
query = urllib.parse.urlencode({"lang": lang})
1176+
query = util.encode_query({"lang": lang})
11771177
status, content, json = self._api_call(
11781178
f"v3/spoken-terms/{spoken_terms_id}/term-lists?{query}",
11791179
method="DELETE",
@@ -1231,7 +1231,7 @@ def get_languages_for_resource(
12311231
if include is not None:
12321232
params.extend(("include", str(value)) for value in include)
12331233

1234-
endpoint = f"v3/languages?{urllib.parse.urlencode(params)}"
1234+
endpoint = f"v3/languages?{util.encode_query(params)}"
12351235
status, content, json = self._api_call(endpoint, method="GET")
12361236
self._raise_for_status(status, content, json)
12371237

@@ -1290,7 +1290,7 @@ def get_all_style_rules(
12901290

12911291
endpoint = "v3/style_rules"
12921292
if params:
1293-
query_string = urllib.parse.urlencode(params)
1293+
query_string = util.encode_query(params)
12941294
endpoint += f"?{query_string}"
12951295

12961296
status, content, json = self._api_call(endpoint, method="GET")
@@ -1584,7 +1584,7 @@ def list_style_profiles(
15841584

15851585
endpoint = "v3/style_profiles"
15861586
if params:
1587-
endpoint += f"?{urllib.parse.urlencode(params)}"
1587+
endpoint += f"?{util.encode_query(params)}"
15881588

15891589
status, content, json = self._api_call(endpoint, method="GET")
15901590
self._raise_for_status(status, content, json)
@@ -1850,7 +1850,7 @@ def list_translation_memories(
18501850

18511851
endpoint = "v3/translation_memories"
18521852
if params:
1853-
query_string = urllib.parse.urlencode(params)
1853+
query_string = util.encode_query(params)
18541854
endpoint += f"?{query_string}"
18551855

18561856
status, content, json = self._api_call(endpoint, method="GET")
@@ -1959,12 +1959,7 @@ def list_translation_memory_segments(
19591959

19601960
endpoint = f"v3/translation_memories/{tm_id}/segments"
19611961
if params:
1962-
# quote_via=quote percent-encodes spaces in filter_text as %20.
1963-
# urlencode defaults to quote_plus, which encodes them as "+" —
1964-
# correct for a form body, but not for a URI query string.
1965-
query_string = urllib.parse.urlencode(
1966-
params, quote_via=urllib.parse.quote
1967-
)
1962+
query_string = util.encode_query(params)
19681963
endpoint += f"?{query_string}"
19691964

19701965
status, content, json = self._api_call(endpoint, method="GET")

‎deepl/util.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import itertools
99
import logging
1010
import re
11+
import urllib.parse
1112
import warnings
1213
from typing import Any, BinaryIO, Dict, Optional, TextIO, Union
1314

@@ -54,6 +55,11 @@ def _add_extra_body_parameters(
5455
request_data.update(extra_body_parameters)
5556

5657

58+
def encode_query(params) -> str:
59+
"""Encodes params as a URI query string, a space as %20 (RFC 3986)."""
60+
return urllib.parse.urlencode(params, quote_via=urllib.parse.quote)
61+
62+
5763
def log_debug(message, **kwargs):
5864
text = _get_log_text(message, **kwargs)
5965
logger.debug(text)

‎tests/offline/test_languages.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ def test_sends_resource_and_include_when_getting_languages(
168168
make_deepl_client().get_languages_for_resource(resource, include=include)
169169

170170

171+
def test_encodes_space_in_query_value_as_percent_20(mocked):
172+
mocked.get(url("v3/languages"), json=LANGUAGES_JSON)
173+
174+
make_deepl_client().get_languages_for_resource("translate text")
175+
176+
assert "resource=translate%20text" in mocked.calls[0].request.url
177+
178+
171179
def test_parses_languages_for_resource(mocked):
172180
mocked.get(url("v3/languages"), json=LANGUAGES_JSON)
173181

0 commit comments

Comments
 (0)