Skip to content

Commit fde68cd

Browse files
committed
Add charge to run client
1 parent bcb8b32 commit fde68cd

File tree

1 file changed

+36
-0
lines changed
  • src/apify_client/clients/resource_clients

1 file changed

+36
-0
lines changed

src/apify_client/clients/resource_clients/run.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from typing import Any
4+
import time
45

56
from apify_shared.utils import filter_out_none_values_recursively, ignore_docs, parse_date_fields
67

@@ -11,6 +12,7 @@
1112
from apify_client.clients.resource_clients.log import LogClient, LogClientAsync
1213
from apify_client.clients.resource_clients.request_queue import RequestQueueClient, RequestQueueClientAsync
1314

15+
RUN_CHARGE_IDEMPOTENCY_HEADER = 'idempotency-key'
1416

1517
class RunClient(ActorJobBaseClient):
1618
"""Sub-client for manipulating a single Actor run."""
@@ -440,3 +442,37 @@ def log(self: RunClientAsync) -> LogClientAsync:
440442
return LogClientAsync(
441443
**self._sub_resource_init_options(resource_path='log'),
442444
)
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

Comments
 (0)