Skip to content

Commit 7b4117a

Browse files
committed
fix: Validate sync batch_delete_requests against RequestDraftDelete
The sync `RequestQueueClient.batch_delete_requests` was validating each input against `RequestDraft` (which requires `url`), so a `{uniqueKey: ...}` dict — the documented delete-by-unique-key shape — raised `ValidationError`. The async variant was already correct; this aligns the sync path and adds unit coverage for both. Regression introduced earlier in this branch during the `RequestDeleteInput` -> `RequestDraftDelete` rename.
1 parent 9b30e7f commit 7b4117a

2 files changed

Lines changed: 34 additions & 11 deletions

File tree

‎src/apify_client/_resource_clients/request_queue.py‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -475,17 +475,10 @@ def batch_delete_requests(
475475
timeout: Timeout for the API HTTP request.
476476
"""
477477
requests_as_dicts = [
478-
(
479-
request
480-
if isinstance(request, RequestDraft)
481-
else RequestDraft.model_validate(
482-
request,
483-
)
484-
).model_dump(
485-
by_alias=True,
486-
exclude_none=True,
478+
(r if isinstance(r, RequestDraftDelete) else RequestDraftDelete.model_validate(r)).model_dump(
479+
by_alias=True, exclude_none=True
487480
)
488-
for request in requests
481+
for r in requests
489482
]
490483

491484
request_params = self._build_params(clientKey=self.client_key)

‎tests/unit/test_client_request_queue.py‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
if TYPE_CHECKING:
1212
from pytest_httpserver import HTTPServer
1313

14-
from apify_client._typeddicts import RequestDraftDict
14+
from apify_client._typeddicts import RequestDraftDeleteDict, RequestDraftDict
1515

1616
_PARTIALLY_ADDED_BATCH_RESPONSE_CONTENT = """{
1717
"data": {
@@ -117,3 +117,33 @@ def test_batch_processed_partially_sync(httpserver: HTTPServer) -> None:
117117
assert requests[0]['unique_key'] in {request.unique_key for request in batch_response.processed_requests}
118118
assert len(batch_response.unprocessed_requests) == 1
119119
assert batch_response.unprocessed_requests[0].unique_key == requests[1]['unique_key']
120+
121+
122+
_BATCH_DELETE_RESPONSE_CONTENT = """{"data": {"processedRequests": [], "unprocessedRequests": []}}"""
123+
124+
125+
def test_batch_delete_requests_accepts_unique_key_only_dict_sync(httpserver: HTTPServer) -> None:
126+
"""`RequestDraftDelete` is one of `{id} | {uniqueKey}`. A dict with only `unique_key` must validate
127+
against the right variant — not `RequestDraft`, which would require `url`."""
128+
server_url = httpserver.url_for('/').removesuffix('/')
129+
client = ApifyClient(token='placeholder_token', api_url=server_url, api_public_url=server_url)
130+
httpserver.expect_oneshot_request(re.compile(r'.*'), method='DELETE').respond_with_data(
131+
status=200, response_data=_BATCH_DELETE_RESPONSE_CONTENT
132+
)
133+
requests: list[RequestDraftDeleteDict] = [{'unique_key': 'delete-1'}]
134+
rq_client = client.request_queue(request_queue_id='whatever')
135+
136+
rq_client.batch_delete_requests(requests=requests)
137+
138+
139+
async def test_batch_delete_requests_accepts_unique_key_only_dict_async(httpserver: HTTPServer) -> None:
140+
"""Async counterpart of `test_batch_delete_requests_accepts_unique_key_only_dict_sync`."""
141+
server_url = httpserver.url_for('/').removesuffix('/')
142+
client = ApifyClientAsync(token='placeholder_token', api_url=server_url, api_public_url=server_url)
143+
httpserver.expect_oneshot_request(re.compile(r'.*'), method='DELETE').respond_with_data(
144+
status=200, response_data=_BATCH_DELETE_RESPONSE_CONTENT
145+
)
146+
requests: list[RequestDraftDeleteDict] = [{'unique_key': 'delete-1'}]
147+
rq_client = client.request_queue(request_queue_id='whatever')
148+
149+
await rq_client.batch_delete_requests(requests=requests)

0 commit comments

Comments
 (0)