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