-
Notifications
You must be signed in to change notification settings - Fork 31
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
feat: Add charge
method to the run client for "pay per event"
#613
Changes from 6 commits
ed6be6e
30b4e39
385f21c
8bee496
c097836
e12e84c
eff7526
7e9def7
5fe2f6f
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 |
---|---|---|
|
@@ -8,12 +8,15 @@ import { LogClient } from './log'; | |
import { RequestQueueClient } from './request_queue'; | ||
import { ApiClientOptionsWithOptionalResourcePath } from '../base/api_client'; | ||
import { ResourceClient } from '../base/resource_client'; | ||
import type { ApifyResponse } from '../http_client'; | ||
import { | ||
pluckData, | ||
parseDateFields, | ||
cast, | ||
} from '../utils'; | ||
|
||
const RUN_CHARGE_IDEMPOTENCY_HEADER = 'idempotency-key'; | ||
|
||
export class RunClient extends ResourceClient { | ||
/** | ||
* @hidden | ||
|
@@ -113,7 +116,7 @@ export class RunClient extends ResourceClient { | |
return cast(parseDateFields(pluckData(response.data))); | ||
} | ||
|
||
async update(newFields: RunUpdateOptions) : Promise<ActorRun> { | ||
async update(newFields: RunUpdateOptions): Promise<ActorRun> { | ||
ow(newFields, ow.object); | ||
|
||
return this._update(newFields); | ||
|
@@ -138,6 +141,35 @@ export class RunClient extends ResourceClient { | |
return cast(parseDateFields(pluckData(response.data))); | ||
} | ||
|
||
/** | ||
* TODO: docs url | ||
* https://github.com/apify/apify-client-js/issues/614 | ||
*/ | ||
async charge(options: RunChargeOptions): Promise<ApifyResponse<Record<string, never>>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity - what's the expected return type of this method? I see that most of the Is this return type ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We discussed this in person. The endpoint uses a memorized version of the run from the database for performance reasons, as the run's pricing model cannot change during the run. Therefore we cannot return the modified run object and the only important output is the response status code. |
||
ow(options, ow.object.exactShape({ | ||
eventName: ow.string, | ||
count: ow.optional.number, | ||
idempotencyKey: ow.optional.string, | ||
})); | ||
|
||
const count = options.count ?? 1; | ||
const idempotencyKey = options.idempotencyKey ?? `${this.id}-${options.eventName}-${Date.now()}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same note here as in the Python client, the idempotency key has to be a unique string. This way, if someone charged for two occurences of the same event in the same millisecond, only one of the charges would go through. |
||
|
||
const request: AxiosRequestConfig = { | ||
url: this._url('charge'), | ||
method: 'POST', | ||
data: { | ||
eventName: options.eventName, | ||
count, | ||
}, | ||
headers: { | ||
[RUN_CHARGE_IDEMPOTENCY_HEADER]: idempotencyKey, | ||
}, | ||
}; | ||
const response = await this.httpClient.call(request); | ||
return response; | ||
} | ||
|
||
/** | ||
* Returns a promise that resolves with the finished Run object when the provided actor run finishes | ||
* or with the unfinished Run object when the `waitSecs` timeout lapses. The promise is NOT rejected | ||
|
@@ -221,7 +253,7 @@ export interface RunMetamorphOptions { | |
} | ||
export interface RunUpdateOptions { | ||
statusMessage?: string; | ||
isStatusMessageTerminal? : boolean; | ||
isStatusMessageTerminal?: boolean; | ||
} | ||
|
||
export interface RunResurrectOptions { | ||
|
@@ -230,6 +262,15 @@ export interface RunResurrectOptions { | |
timeout?: number; | ||
} | ||
|
||
export type RunChargeOptions = { | ||
/** Name of the event to charge. Must be defined in the Actor's pricing info else the API will throw. */ | ||
eventName: string; | ||
/** Defaults to 1 */ | ||
count?: number; | ||
/** Defaults to runId-eventName-timestamp */ | ||
idempotencyKey?: string; | ||
}; | ||
|
||
export interface RunWaitForFinishOptions { | ||
/** | ||
* Maximum time to wait for the run to finish, in seconds. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add the URL here right ahead? even if it doesn't exist yet, feels better than publishing with a TODO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have the URL. I'll ask Mat'o what's the status on the endpoint docs 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am rather sure we can guess it before it exists, its just another endpoint, the URLs for them are not random.