Skip to content

Commit 463e742

Browse files
vdusekclaude
andcommitted
fix: preserve count=0 in RunClient.charge
The billing endpoint body was built with `count or 1`, which silently rewrote a legitimate `count=0` to `count=1` — on a pay-per-event endpoint, the difference is financially material. Use an explicit `None` check instead. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 3e9fc3d commit 463e742

2 files changed

Lines changed: 95 additions & 4 deletions

File tree

‎src/apify_client/_resource_clients/run.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ def charge(
385385
386386
Args:
387387
event_name: The name of the event to charge for.
388-
count: The number of events to charge. Defaults to 1 if not provided.
388+
count: The number of events to charge. Defaults to 1 when `None`; other values,
389+
including 0, are sent to the server as-is.
389390
idempotency_key: A unique key to ensure idempotent charging. If not provided,
390391
one will be auto-generated.
391392
timeout: Timeout for the API HTTP request.
@@ -411,7 +412,7 @@ def charge(
411412
data=json.dumps(
412413
{
413414
'eventName': event_name,
414-
'count': count or 1,
415+
'count': count if count is not None else 1,
415416
}
416417
),
417418
timeout=timeout,
@@ -811,7 +812,8 @@ async def charge(
811812
812813
Args:
813814
event_name: The name of the event to charge for.
814-
count: The number of events to charge. Defaults to 1 if not provided.
815+
count: The number of events to charge. Defaults to 1 when `None`; other values,
816+
including 0, are sent to the server as-is.
815817
idempotency_key: A unique key to ensure idempotent charging. If not provided,
816818
one will be auto-generated.
817819
timeout: Timeout for the API HTTP request.
@@ -837,7 +839,7 @@ async def charge(
837839
data=json.dumps(
838840
{
839841
'eventName': event_name,
840-
'count': count or 1,
842+
'count': count if count is not None else 1,
841843
}
842844
),
843845
timeout=timeout,

‎tests/unit/test_run_charge.py‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
from __future__ import annotations
2+
3+
import gzip
4+
import json
5+
from typing import TYPE_CHECKING
6+
7+
import pytest
8+
from werkzeug import Request, Response
9+
10+
from apify_client import ApifyClient, ApifyClientAsync
11+
12+
if TYPE_CHECKING:
13+
from pytest_httpserver import HTTPServer
14+
15+
_MOCKED_RUN_ID = 'test_run_id'
16+
_CHARGE_PATH = f'/v2/actor-runs/{_MOCKED_RUN_ID}/charge'
17+
18+
19+
def _decode_body(request: Request) -> dict:
20+
raw = request.get_data()
21+
if request.headers.get('Content-Encoding') == 'gzip':
22+
raw = gzip.decompress(raw)
23+
return json.loads(raw)
24+
25+
26+
@pytest.mark.parametrize(
27+
('count', 'expected'),
28+
[
29+
(None, 1),
30+
(0, 0),
31+
(1, 1),
32+
(5, 5),
33+
],
34+
)
35+
def test_run_charge_preserves_count_sync(
36+
httpserver: HTTPServer,
37+
count: int | None,
38+
expected: int,
39+
) -> None:
40+
"""Ensure `count` is sent as-is; only `None` falls back to 1 (in particular, `0` is preserved)."""
41+
captured_requests: list[Request] = []
42+
43+
def capture_request(request: Request) -> Response:
44+
captured_requests.append(request)
45+
return Response(status=200, mimetype='application/json')
46+
47+
httpserver.expect_request(_CHARGE_PATH, method='POST').respond_with_handler(capture_request)
48+
49+
api_url = httpserver.url_for('/').removesuffix('/')
50+
client = ApifyClient(token='test_token', api_url=api_url)
51+
52+
client.run(_MOCKED_RUN_ID).charge(event_name='test-event', count=count)
53+
54+
assert len(captured_requests) == 1
55+
body = _decode_body(captured_requests[0])
56+
assert body['count'] == expected
57+
58+
59+
@pytest.mark.parametrize(
60+
('count', 'expected'),
61+
[
62+
(None, 1),
63+
(0, 0),
64+
(1, 1),
65+
(5, 5),
66+
],
67+
)
68+
async def test_run_charge_preserves_count_async(
69+
httpserver: HTTPServer,
70+
count: int | None,
71+
expected: int,
72+
) -> None:
73+
"""Async variant of `test_run_charge_preserves_count_sync`."""
74+
captured_requests: list[Request] = []
75+
76+
def capture_request(request: Request) -> Response:
77+
captured_requests.append(request)
78+
return Response(status=200, mimetype='application/json')
79+
80+
httpserver.expect_request(_CHARGE_PATH, method='POST').respond_with_handler(capture_request)
81+
82+
api_url = httpserver.url_for('/').removesuffix('/')
83+
client = ApifyClientAsync(token='test_token', api_url=api_url)
84+
85+
await client.run(_MOCKED_RUN_ID).charge(event_name='test-event', count=count)
86+
87+
assert len(captured_requests) == 1
88+
body = _decode_body(captured_requests[0])
89+
assert body['count'] == expected

0 commit comments

Comments
 (0)