Skip to main content

apify_client/clients/
webhook_collection.rs

1//! Client for the webhook collection (`/v2/webhooks`, `/v2/actors/{id}/webhooks`, etc.).
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::Webhook;
11
12/// Client for listing and creating webhooks.
13#[derive(Debug, Clone)]
14pub struct WebhookCollectionClient {
15    ctx: ResourceContext,
16}
17
18impl WebhookCollectionClient {
19    pub(crate) fn new(http: HttpClient, base_url: &str) -> Self {
20        Self {
21            ctx: ResourceContext::collection(http, base_url, "webhooks"),
22        }
23    }
24
25    /// Creates a webhook collection client nested under another resource.
26    pub(crate) fn with_base(http: HttpClient, base_url: &str) -> Self {
27        Self {
28            ctx: ResourceContext::collection(http, base_url, "webhooks"),
29        }
30    }
31
32    /// Lists webhooks with offset/limit pagination.
33    pub async fn list(&self, options: ListOptions) -> ApifyClientResult<PaginationList<Webhook>> {
34        let mut params = QueryParams::new();
35        params
36            .add_int("offset", options.offset)
37            .add_int("limit", options.limit)
38            .add_bool("desc", options.desc);
39        list_resource(&self.ctx, None, &params).await
40    }
41
42    /// Lazily iterates over all webhooks matching `options`, fetching pages on demand.
43    ///
44    /// `options.limit` caps the *total* number of items yielded across all pages, unlike
45    /// [`list`](Self::list) where `limit` is a single page's size. Set the per-page fetch size
46    /// with [`with_chunk_size`](crate::ListIterator::with_chunk_size); see
47    /// [`ListIterator`] for details.
48    pub fn iterate(&self, options: ListOptions) -> ListIterator<Webhook> {
49        list_iterator!(self, options, list)
50    }
51
52    /// Creates a new webhook from the given definition.
53    pub async fn create<T: Serialize>(&self, webhook: &T) -> ApifyClientResult<Webhook> {
54        create_resource(&self.ctx, &QueryParams::new(), webhook).await
55    }
56}