Skip to main content

apify_client/clients/
task_collection.rs

1//! Client for the Actor task collection (`/v2/actor-tasks`).
2
3use serde::Serialize;
4
5use crate::clients::base::{create_resource, list_resource, ResourceContext};
6use crate::clients::pagination::{list_iterator, ListIterator};
7use crate::common::{ListOptions, PaginationList, QueryParams};
8use crate::error::ApifyClientResult;
9use crate::http_client::HttpClient;
10use crate::models::Task;
11
12/// Client for listing and creating Actor tasks.
13#[derive(Debug, Clone)]
14pub struct TaskCollectionClient {
15    ctx: ResourceContext,
16}
17
18impl TaskCollectionClient {
19    pub(crate) fn new(http: HttpClient, base_url: &str) -> Self {
20        Self {
21            ctx: ResourceContext::collection(http, base_url, "actor-tasks"),
22        }
23    }
24
25    /// Lists tasks with offset/limit pagination.
26    pub async fn list(&self, options: ListOptions) -> ApifyClientResult<PaginationList<Task>> {
27        let mut params = QueryParams::new();
28        params
29            .add_int("offset", options.offset)
30            .add_int("limit", options.limit)
31            .add_bool("desc", options.desc);
32        list_resource(&self.ctx, None, &params).await
33    }
34
35    /// Lazily iterates over all tasks matching `options`, fetching pages on demand.
36    ///
37    /// `options.limit` caps the *total* number of items yielded across all pages, unlike
38    /// [`list`](Self::list) where `limit` is a single page's size. Set the per-page fetch size
39    /// with [`with_chunk_size`](crate::ListIterator::with_chunk_size); see
40    /// [`ListIterator`] for details.
41    pub fn iterate(&self, options: ListOptions) -> ListIterator<Task> {
42        list_iterator!(self, options, list)
43    }
44
45    /// Creates a new task from the given definition.
46    pub async fn create<T: Serialize>(&self, task: &T) -> ApifyClientResult<Task> {
47        create_resource(&self.ctx, &QueryParams::new(), task).await
48    }
49}