dfns-sdk-rust 0.2.0

Dfns API SDK for Rust
Documentation
// Code generated by rust-sdk-generator. DO NOT EDIT.

pub mod delegated;
pub mod types;

#[allow(unused_imports)]
use types::*;

/// Client for webhooks operations.
#[derive(Clone)]
pub struct WebhooksClient {
    client: crate::client::Client,
}

impl WebhooksClient {
    pub fn new(client: crate::client::Client) -> Self {
        WebhooksClient { client }
    }

    /// List all webhooks for the authenticated user's organization. The results are paginated.
    pub async fn list_webhooks(
        &self,
        query: Option<ListWebhooksQuery>,
    ) -> Result<ListWebhooksResponse, crate::error::Error> {
        let mut path = String::from("/webhooks");
        if let Some(query) = &query {
            let mut q: Vec<String> = Vec::new();
            if let Some(v) = &query.limit {
                q.push(format!("limit={}", urlencoding::encode(&v.to_string())));
            }
            if let Some(v) = &query.pagination_token {
                q.push(format!(
                    "paginationToken={}",
                    urlencoding::encode(&v.to_string())
                ));
            }
            if !q.is_empty() {
                path.push('?');
                path.push_str(&q.join("&"));
            }
        }
        self.client
            .request::<ListWebhooksResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Register a new webhook.
    pub async fn create_webhook(
        &self,
        body: CreateWebhookRequest,
    ) -> Result<CreateWebhookResponse, crate::error::Error> {
        let path = String::from("/webhooks");
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<CreateWebhookResponse>(reqwest::Method::POST, &path, Some(&body), true)
            .await
    }

    /// Retrieve information about a specific webhook.
    pub async fn get_webhook(
        &self,
        webhook_id: String,
    ) -> Result<GetWebhookResponse, crate::error::Error> {
        let path = format!("/webhooks/{}", urlencoding::encode(&webhook_id));
        self.client
            .request::<GetWebhookResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Update the definition of an existing webhook.
    pub async fn update_webhook(
        &self,
        webhook_id: String,
        body: UpdateWebhookRequest,
    ) -> Result<UpdateWebhookResponse, crate::error::Error> {
        let path = format!("/webhooks/{}", urlencoding::encode(&webhook_id));
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<UpdateWebhookResponse>(reqwest::Method::PUT, &path, Some(&body), true)
            .await
    }

    /// Deletes an existing webhook registration.
    pub async fn delete_webhook(
        &self,
        webhook_id: String,
    ) -> Result<DeleteWebhookResponse, crate::error::Error> {
        let path = format!("/webhooks/{}", urlencoding::encode(&webhook_id));
        self.client
            .request::<DeleteWebhookResponse>(reqwest::Method::DELETE, &path, None, true)
            .await
    }

    /// This endpoint is meant for webhook setup and troubleshooting. Calling the endpoint will trigger a fake test event that will be pushed to the webhook URL. The fake event will not be saved and not appear in further requests to Webhook Events.
    pub async fn ping_webhook(
        &self,
        webhook_id: String,
    ) -> Result<PingWebhookResponse, crate::error::Error> {
        let path = format!("/webhooks/{}/ping", urlencoding::encode(&webhook_id));
        self.client
            .request::<PingWebhookResponse>(reqwest::Method::POST, &path, None, true)
            .await
    }

    /// Retrieve a specific webhook event details by its ID.
    ///   
    /// <Warning>
    /// We only keep a trace of those Webhook Events in our system for a **retention period of 31 days**. Past that, they are discarded, so you cannot see them using [List Webhook Events](https://docs.dfns.co/api-reference/webhooks/list-webhook-events) or [Get Webhook Event](https://docs.dfns.co/api-reference/webhooks/get-webhook-event) endpoints.
    pub async fn get_webhook_event(
        &self,
        webhook_id: String,
        webhook_event_id: String,
    ) -> Result<GetWebhookEventResponse, crate::error::Error> {
        let path = format!(
            "/webhooks/{}/events/{}",
            urlencoding::encode(&webhook_id),
            urlencoding::encode(&webhook_event_id)
        );
        self.client
            .request::<GetWebhookEventResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Lists all events for a given webhook.
    ///
    ///
    /// <Warning>
    pub async fn list_webhook_events(
        &self,
        webhook_id: String,
        query: Option<ListWebhookEventsQuery>,
    ) -> Result<ListWebhookEventsResponse, crate::error::Error> {
        let mut path = format!("/webhooks/{}/events", urlencoding::encode(&webhook_id));
        if let Some(query) = &query {
            let mut q: Vec<String> = Vec::new();
            if let Some(v) = &query.kind {
                q.push(format!("kind={}", urlencoding::encode(&v.to_string())));
            }
            if let Some(v) = &query.delivery_failed {
                q.push(format!(
                    "deliveryFailed={}",
                    urlencoding::encode(&v.to_string())
                ));
            }
            if let Some(v) = &query.limit {
                q.push(format!("limit={}", urlencoding::encode(&v.to_string())));
            }
            if let Some(v) = &query.pagination_token {
                q.push(format!(
                    "paginationToken={}",
                    urlencoding::encode(&v.to_string())
                ));
            }
            if !q.is_empty() {
                path.push('?');
                path.push_str(&q.join("&"));
            }
        }
        self.client
            .request::<ListWebhookEventsResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }
}