Skip to main content

apify_client/clients/
actor_collection.rs

1//! Client for the Actor collection endpoint (`/v2/actors`).
2
3use serde::Serialize;
4
5use crate::clients::base::{create_resource, list_resource, ResourceContext};
6use crate::common::{PaginationList, QueryParams};
7use crate::error::ApifyClientResult;
8use crate::http_client::HttpClient;
9use crate::models::Actor;
10
11/// Options for listing Actors (`GET /v2/actors`).
12#[derive(Debug, Default, Clone)]
13pub struct ActorListOptions {
14    /// Number of Actors to skip.
15    pub offset: Option<i64>,
16    /// Maximum number of Actors to return.
17    pub limit: Option<i64>,
18    /// Return Actors newest-first.
19    pub desc: Option<bool>,
20    /// If `true`, return only Actors owned by the current user.
21    pub my: Option<bool>,
22    /// Sort key, e.g. `createdAt` or `stats.lastRunStartedAt`.
23    pub sort_by: Option<String>,
24}
25
26/// Client for listing and creating Actors.
27#[derive(Debug, Clone)]
28pub struct ActorCollectionClient {
29    ctx: ResourceContext,
30}
31
32impl ActorCollectionClient {
33    pub(crate) fn new(http: HttpClient, base_url: &str) -> Self {
34        Self {
35            ctx: ResourceContext::collection(http, base_url, "actors"),
36        }
37    }
38
39    /// Lists the Actors in your account.
40    ///
41    /// Use [`ActorListOptions::my`] to restrict the result to Actors you own.
42    pub async fn list(
43        &self,
44        options: ActorListOptions,
45    ) -> ApifyClientResult<PaginationList<Actor>> {
46        let mut params = QueryParams::new();
47        params
48            .add_int("offset", options.offset)
49            .add_int("limit", options.limit)
50            .add_bool("desc", options.desc)
51            .add_bool("my", options.my)
52            .add_str("sortBy", options.sort_by);
53        list_resource(&self.ctx, None, &params).await
54    }
55
56    /// Creates a new Actor with the given definition.
57    ///
58    /// `actor` is any JSON-serializable Actor definition (at minimum a `name`).
59    pub async fn create<T: Serialize>(&self, actor: &T) -> ApifyClientResult<Actor> {
60        create_resource(&self.ctx, &QueryParams::new(), actor).await
61    }
62}