Skip to main content

apify_client/clients/
dataset_collection.rs

1//! Client for the dataset collection (`/v2/datasets`).
2
3use crate::clients::base::{get_or_create_named, list_resource, ResourceContext};
4use crate::clients::pagination::{list_iterator, ListIterator};
5use crate::common::{PaginationList, QueryParams, StorageListOptions};
6use crate::error::ApifyClientResult;
7use crate::http_client::HttpClient;
8use crate::models::Dataset;
9
10/// Client for listing datasets and getting-or-creating a dataset by name.
11#[derive(Debug, Clone)]
12pub struct DatasetCollectionClient {
13    ctx: ResourceContext,
14}
15
16impl DatasetCollectionClient {
17    pub(crate) fn new(http: HttpClient, base_url: &str) -> Self {
18        Self {
19            ctx: ResourceContext::collection(http, base_url, "datasets"),
20        }
21    }
22
23    /// Lists datasets with offset/limit pagination, optionally filtering by `unnamed`/`ownership`.
24    pub async fn list(
25        &self,
26        options: StorageListOptions,
27    ) -> ApifyClientResult<PaginationList<Dataset>> {
28        let mut params = QueryParams::new();
29        options.apply(&mut params);
30        list_resource(&self.ctx, None, &params).await
31    }
32
33    /// Lazily iterates over all datasets matching `options`, fetching pages on demand.
34    ///
35    /// `options.limit` caps the *total* number of items yielded across all pages, unlike
36    /// [`list`](Self::list) where `limit` is a single page's size. Set the per-page fetch size
37    /// with [`with_chunk_size`](crate::ListIterator::with_chunk_size); see
38    /// [`ListIterator`] for details.
39    pub fn iterate(&self, options: StorageListOptions) -> ListIterator<Dataset> {
40        list_iterator!(self, options, list)
41    }
42
43    /// Gets the dataset with the given `name`, creating it if it does not exist.
44    ///
45    /// Passing `None` for `name` creates an unnamed dataset.
46    pub async fn get_or_create(&self, name: Option<&str>) -> ApifyClientResult<Dataset> {
47        get_or_create_named(&self.ctx, name).await
48    }
49}