|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | from typing import Any
|
| 4 | +import time |
4 | 5 |
|
5 | 6 | from apify_shared.utils import filter_out_none_values_recursively, ignore_docs, parse_date_fields
|
6 | 7 |
|
|
11 | 12 | from apify_client.clients.resource_clients.log import LogClient, LogClientAsync
|
12 | 13 | from apify_client.clients.resource_clients.request_queue import RequestQueueClient, RequestQueueClientAsync
|
13 | 14 |
|
| 15 | +RUN_CHARGE_IDEMPOTENCY_HEADER = 'idempotency-key' |
14 | 16 |
|
15 | 17 | class RunClient(ActorJobBaseClient):
|
16 | 18 | """Sub-client for manipulating a single Actor run."""
|
@@ -440,3 +442,37 @@ def log(self: RunClientAsync) -> LogClientAsync:
|
440 | 442 | return LogClientAsync(
|
441 | 443 | **self._sub_resource_init_options(resource_path='log'),
|
442 | 444 | )
|
| 445 | + |
| 446 | + async def charge( |
| 447 | + self: RunClient, |
| 448 | + eventName: str, |
| 449 | + count: int | None = None, |
| 450 | + idempotencyKey: str | None = None, |
| 451 | + ) -> dict: |
| 452 | + """Charge for an event of a Pay-Per-Event Actor run. |
| 453 | +
|
| 454 | + TODO: docs url |
| 455 | +
|
| 456 | + Returns: |
| 457 | + dict: Status and message of the charge event. |
| 458 | + """ |
| 459 | + |
| 460 | + if not eventName: |
| 461 | + raise ValueError("eventName is required for charging an event") |
| 462 | + |
| 463 | + idempotencyKey = idempotencyKey or "{runId}-{eventName}-{timestamp}".format( |
| 464 | + runId=self.resource_id, |
| 465 | + eventName=eventName, |
| 466 | + timestamp=int(time.time() * 1000), |
| 467 | + ) |
| 468 | + |
| 469 | + response = await self.http_client.call( |
| 470 | + url=self._url('charge'), |
| 471 | + method='POST', |
| 472 | + headers={RUN_CHARGE_IDEMPOTENCY_HEADER: idempotencyKey}, |
| 473 | + data={ |
| 474 | + 'eventName': eventName, |
| 475 | + 'count': count or 1, |
| 476 | + } |
| 477 | + ) |
| 478 | + return response |
0 commit comments