-
Notifications
You must be signed in to change notification settings - Fork 14
feat: Add charge method to the run client for "pay per event" #304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
fde68cd
037090f
01b09ba
7f18ac1
d912d6e
25e7e00
4b656a8
aec0b3c
0bf8a55
f074945
58d2674
f3ddf59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
import time | ||
from typing import Any | ||
|
||
from apify_shared.utils import filter_out_none_values_recursively, ignore_docs, parse_date_fields | ||
|
@@ -226,6 +228,38 @@ def log(self: RunClient) -> LogClient: | |
**self._sub_resource_init_options(resource_path='log'), | ||
) | ||
|
||
def charge( | ||
self: RunClient, | ||
event_name: str, | ||
count: int | None = None, | ||
idempotency_key: str | None = None, | ||
) -> dict: | ||
"""Charge for an event of a Pay-Per-Event Actor run. | ||
|
||
https://docs.apify.com/api/v2#/reference/actor-runs/charge-run/charge-run | ||
|
||
Returns: | ||
dict: Status and message of the charge event. | ||
""" | ||
if not event_name: | ||
raise ValueError('eventName is required for charging an event') | ||
|
||
response = self.http_client.call( | ||
url=self._url('charge'), | ||
method='POST', | ||
headers={ | ||
'idempotency-key': idempotency_key or f'{self.resource_id}-{event_name}-{int(time.time() * 1000)}', | ||
|
||
'content-type': 'application/json', | ||
}, | ||
data=json.dumps( | ||
{ | ||
'eventName': event_name, | ||
'count': count or 1, | ||
} | ||
), | ||
) | ||
return parse_date_fields(pluck_data(response.json())) | ||
|
||
|
||
class RunClientAsync(ActorJobBaseClientAsync): | ||
"""Async sub-client for manipulating a single Actor run.""" | ||
|
@@ -440,3 +474,35 @@ def log(self: RunClientAsync) -> LogClientAsync: | |
return LogClientAsync( | ||
**self._sub_resource_init_options(resource_path='log'), | ||
) | ||
|
||
async def charge( | ||
self: RunClientAsync, | ||
event_name: str, | ||
count: int | None = None, | ||
idempotency_key: str | None = None, | ||
) -> dict: | ||
"""Charge for an event of a Pay-Per-Event Actor run. | ||
|
||
https://docs.apify.com/api/v2#/reference/actor-runs/charge-run/charge-run | ||
|
||
Returns: | ||
dict: Status and message of the charge event. | ||
""" | ||
if not event_name: | ||
raise ValueError('eventName is required for charging an event') | ||
Jkuzz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
response = await self.http_client.call( | ||
url=self._url('charge'), | ||
method='POST', | ||
headers={ | ||
'idempotency-key': idempotency_key or f'{self.resource_id}-{event_name}-{int(time.time() * 1000)}', | ||
'content-type': 'application/json', | ||
}, | ||
data=json.dumps( | ||
{ | ||
'eventName': event_name, | ||
'count': count or 1, | ||
} | ||
), | ||
) | ||
return parse_date_fields(pluck_data(response.json())) |
Uh oh!
There was an error while loading. Please reload this page.