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::common::{ListOptions, PaginationList, QueryParams};
7use crate::error::ApifyClientResult;
8use crate::http_client::HttpClient;
9use crate::models::Task;
10
11/// Client for listing and creating Actor tasks.
12#[derive(Debug, Clone)]
13pub struct TaskCollectionClient {
14    ctx: ResourceContext,
15}
16
17impl TaskCollectionClient {
18    pub(crate) fn new(http: HttpClient, base_url: &str) -> Self {
19        Self {
20            ctx: ResourceContext::collection(http, base_url, "actor-tasks"),
21        }
22    }
23
24    /// Lists tasks with offset/limit pagination.
25    pub async fn list(&self, options: ListOptions) -> ApifyClientResult<PaginationList<Task>> {
26        let mut params = QueryParams::new();
27        params
28            .add_int("offset", options.offset)
29            .add_int("limit", options.limit)
30            .add_bool("desc", options.desc);
31        list_resource(&self.ctx, None, &params).await
32    }
33
34    /// Creates a new task from the given definition.
35    pub async fn create<T: Serialize>(&self, task: &T) -> ApifyClientResult<Task> {
36        create_resource(&self.ctx, &QueryParams::new(), task).await
37    }
38}