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::clients::pagination::{list_iterator, ListIterator};
7use crate::common::{PaginationList, QueryParams};
8use crate::error::ApifyClientResult;
9use crate::http_client::HttpClient;
10use crate::models::Actor;
11
12/// Options for listing Actors (`GET /v2/actors`).
13#[derive(Debug, Default, Clone)]
14pub struct ActorListOptions {
15    /// Number of Actors to skip.
16    pub offset: Option<i64>,
17    /// Maximum number of Actors to return.
18    pub limit: Option<i64>,
19    /// Return Actors newest-first.
20    pub desc: Option<bool>,
21    /// If `true`, return only Actors owned by the current user.
22    pub my: Option<bool>,
23    /// Sort key, e.g. `createdAt` or `stats.lastRunStartedAt`.
24    pub sort_by: Option<String>,
25}
26
27/// Client for listing and creating Actors.
28#[derive(Debug, Clone)]
29pub struct ActorCollectionClient {
30    ctx: ResourceContext,
31}
32
33impl ActorCollectionClient {
34    pub(crate) fn new(http: HttpClient, base_url: &str) -> Self {
35        Self {
36            ctx: ResourceContext::collection(http, base_url, "actors"),
37        }
38    }
39
40    /// Lists the Actors in your account.
41    ///
42    /// Use [`ActorListOptions::my`] to restrict the result to Actors you own.
43    pub async fn list(
44        &self,
45        options: ActorListOptions,
46    ) -> ApifyClientResult<PaginationList<Actor>> {
47        let mut params = QueryParams::new();
48        params
49            .add_int("offset", options.offset)
50            .add_int("limit", options.limit)
51            .add_bool("desc", options.desc)
52            .add_bool("my", options.my)
53            .add_str("sortBy", options.sort_by);
54        list_resource(&self.ctx, None, &params).await
55    }
56
57    /// Lazily iterates over all Actors matching `options`, fetching pages on demand.
58    ///
59    /// Returns a [`ListIterator`] whose `next()` yields one Actor at a time, transparently
60    /// fetching subsequent pages until the listing is exhausted.
61    ///
62    /// `options.limit` caps the *total* number of items yielded across all pages, unlike
63    /// [`list`](Self::list) where `limit` is a single page's size. Set the per-page fetch size
64    /// with [`with_chunk_size`](crate::ListIterator::with_chunk_size); see
65    /// [`ListIterator`] for details.
66    pub fn iterate(&self, options: ActorListOptions) -> ListIterator<Actor> {
67        list_iterator!(self, options, list)
68    }
69
70    /// Creates a new Actor with the given definition.
71    ///
72    /// `actor` is any JSON-serializable Actor definition (at minimum a `name`).
73    pub async fn create<T: Serialize>(&self, actor: &T) -> ApifyClientResult<Actor> {
74        create_resource(&self.ctx, &QueryParams::new(), actor).await
75    }
76}