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::common::{PaginationList, QueryParams, StorageListOptions};
5use crate::error::ApifyClientResult;
6use crate::http_client::HttpClient;
7use crate::models::Dataset;
8
9/// Client for listing datasets and getting-or-creating a dataset by name.
10#[derive(Debug, Clone)]
11pub struct DatasetCollectionClient {
12    ctx: ResourceContext,
13}
14
15impl DatasetCollectionClient {
16    pub(crate) fn new(http: HttpClient, base_url: &str) -> Self {
17        Self {
18            ctx: ResourceContext::collection(http, base_url, "datasets"),
19        }
20    }
21
22    /// Lists datasets with offset/limit pagination, optionally filtering by `unnamed`/`ownership`.
23    pub async fn list(
24        &self,
25        options: StorageListOptions,
26    ) -> ApifyClientResult<PaginationList<Dataset>> {
27        let mut params = QueryParams::new();
28        options.apply(&mut params);
29        list_resource(&self.ctx, None, &params).await
30    }
31
32    /// Gets the dataset with the given `name`, creating it if it does not exist.
33    ///
34    /// Passing `None` for `name` creates an unnamed dataset.
35    pub async fn get_or_create(&self, name: Option<&str>) -> ApifyClientResult<Dataset> {
36        get_or_create_named(&self.ctx, name).await
37    }
38}