Skip to main content

apify_client/clients/
webhook.rs

1//! Client for a single webhook (`/v2/webhooks/{webhookId}`).
2
3use serde::Serialize;
4
5use crate::clients::base::{
6    delete_resource, get_resource, post_action, update_resource, ResourceContext,
7};
8use crate::clients::webhook_dispatch_collection::WebhookDispatchCollectionClient;
9use crate::common::QueryParams;
10use crate::error::ApifyClientResult;
11use crate::http_client::HttpClient;
12use crate::models::{Webhook, WebhookDispatch};
13
14/// Client for a specific webhook.
15#[derive(Debug, Clone)]
16pub struct WebhookClient {
17    ctx: ResourceContext,
18}
19
20impl WebhookClient {
21    pub(crate) fn new(http: HttpClient, base_url: &str, id: &str) -> Self {
22        Self {
23            ctx: ResourceContext::single(http, base_url, "webhooks", id),
24        }
25    }
26
27    /// Fetches the webhook, or `None` if it does not exist.
28    pub async fn get(&self) -> ApifyClientResult<Option<Webhook>> {
29        get_resource(&self.ctx, None, &QueryParams::new()).await
30    }
31
32    /// Updates the webhook with the given fields.
33    pub async fn update<T: Serialize>(&self, new_fields: &T) -> ApifyClientResult<Webhook> {
34        update_resource(&self.ctx, None, new_fields).await
35    }
36
37    /// Deletes the webhook.
38    pub async fn delete(&self) -> ApifyClientResult<()> {
39        delete_resource(&self.ctx, None).await
40    }
41
42    /// Tests the webhook by dispatching it immediately, returning the dispatch.
43    pub async fn test(&self) -> ApifyClientResult<WebhookDispatch> {
44        post_action(&self.ctx, Some("test"), &QueryParams::new(), None, None).await
45    }
46
47    /// Returns a client for this webhook's dispatch collection.
48    pub fn dispatches(&self) -> WebhookDispatchCollectionClient {
49        WebhookDispatchCollectionClient::with_base(
50            self.ctx.http.clone(),
51            &self.ctx.url(None),
52            "dispatches",
53        )
54    }
55}