Skip to content

Commit 5ecbe24

Browse files
authored
chore: Send Booleans as true or false to API (#673)
Sending `1` and `0` is working, but the validator is complaining about that. Lets align with the JS client. [JS client uses `false`, `true`](https://github.com/apify/apify-client-js/blob/v2.22.2/src/utils.ts) [OpenAPI spec does not define](https://spec.openapis.org/oas/v3.2.0.html#boolean-query-parameter-examples) serialization of Booleans, but tools frequently choose `false/true` ### Issues: Related to: apify/apify-docs#2286
1 parent 93d66a2 commit 5ecbe24

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

src/apify_client/_http_clients/_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@ def __init__(
145145
def _parse_params(params: dict[str, Any] | None) -> dict[str, Any] | None:
146146
"""Convert request parameters to Apify API-compatible formats.
147147
148-
Converts booleans to 0/1, lists to comma-separated strings, datetimes to ISO 8601 Zulu format.
148+
Converts booleans to 'false'/'true', lists to comma-separated strings, datetimes to ISO 8601 Zulu format.
149149
"""
150150
if params is None:
151151
return None
152152

153153
parsed_params: dict[str, Any] = {}
154154
for key, value in params.items():
155155
if isinstance(value, bool):
156-
parsed_params[key] = int(value)
156+
parsed_params[key] = (str(value)).lower()
157157
elif isinstance(value, list):
158158
parsed_params[key] = ','.join(value)
159159
elif isinstance(value, datetime):

tests/unit/test_http_clients.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ def test_parse_params_none() -> None:
183183

184184

185185
def test_parse_params_boolean() -> None:
186-
"""Test _parse_params converts booleans to integers."""
186+
"""Test _parse_params converts booleans to `false` or `true`."""
187187
result = HttpClient._parse_params({'flag': True, 'disabled': False})
188-
assert result == {'flag': 1, 'disabled': 0}
188+
assert result == {'flag': 'true', 'disabled': 'false'}
189189

190190

191191
def test_parse_params_list() -> None:
@@ -224,7 +224,7 @@ def test_parse_params_mixed() -> None:
224224
assert result == {
225225
'limit': 10,
226226
'offset': 0,
227-
'flag': 1,
227+
'flag': 'true',
228228
'tags': 'tag1,tag2',
229229
'created_at': '2024-01-15T10:30:45.123Z',
230230
'name': 'test',
@@ -377,7 +377,7 @@ def test_prepare_request_call_with_params() -> None:
377377

378378
_headers, params, _data = client._prepare_request_call(params={'limit': 10, 'flag': True})
379379

380-
assert params == {'limit': 10, 'flag': 1}
380+
assert params == {'limit': 10, 'flag': 'true'}
381381

382382

383383
def test_build_url_with_params_none() -> None:

tests/unit/test_logging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def mock_api(httpserver: HTTPServer) -> None:
206206
)
207207

208208
httpserver.expect_request(
209-
f'/v2/actor-runs/{_MOCKED_RUN_ID}/log', method='GET', query_string='stream=1&raw=1'
209+
f'/v2/actor-runs/{_MOCKED_RUN_ID}/log', method='GET', query_string='stream=true&raw=true'
210210
).respond_with_handler(_streaming_log_handler)
211211

212212

@@ -486,7 +486,7 @@ async def test_streamed_log_async_restart_after_normal_completion(httpserver: HT
486486
"""Test that StreamedLogAsync cannot be restarted after task completes normally."""
487487
# Set up a quick-completing stream endpoint
488488
httpserver.expect_request(
489-
f'/v2/actor-runs/{_MOCKED_RUN_ID}/log', method='GET', query_string='stream=1&raw=1'
489+
f'/v2/actor-runs/{_MOCKED_RUN_ID}/log', method='GET', query_string='stream=true&raw=true'
490490
).respond_with_data(b'Quick log\n', content_type='application/octet-stream')
491491

492492
# Set up actor info endpoint (needed for get_streamed_log)

0 commit comments

Comments
 (0)