dfns-sdk-rust 0.2.0

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

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

/// Delegated client for webhooks operations. Signed operations are split into
/// init/complete pairs so the challenge can be signed on the user side.
#[derive(Clone)]
pub struct DelegatedWebhooksClient {
    client: crate::client::Client,
}

impl DelegatedWebhooksClient {
    pub fn new(client: crate::client::Client) -> Self {
        DelegatedWebhooksClient { 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
    }

    /// Starts delegated signing for createWebhook: returns the challenge to sign.
    /// Pass the signed assertion to create_webhook_complete with the same arguments.
    pub async fn create_webhook_init(
        &self,
        body: CreateWebhookRequest,
    ) -> Result<crate::signer::UserActionChallenge, crate::error::Error> {
        let path = String::from("/webhooks");
        let body = serde_json::to_value(&body)?;
        self.client
            .create_user_action_challenge(reqwest::Method::POST, &path, Some(&body))
            .await
    }

    /// Finishes delegated signing for createWebhook: submits the signed challenge
    /// and issues the request.
    pub async fn create_webhook_complete(
        &self,
        body: CreateWebhookRequest,
        challenge_identifier: String,
        assertion: crate::signer::CredentialAssertion,
    ) -> Result<CreateWebhookResponse, crate::error::Error> {
        let path = String::from("/webhooks");
        let body = serde_json::to_value(&body)?;
        let user_action = self
            .client
            .complete_user_action_signing(challenge_identifier, &assertion)
            .await?;
        self.client
            .request_with_user_action::<CreateWebhookResponse>(
                reqwest::Method::POST,
                &path,
                Some(&body),
                &user_action,
            )
            .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
    }

    /// Starts delegated signing for updateWebhook: returns the challenge to sign.
    /// Pass the signed assertion to update_webhook_complete with the same arguments.
    pub async fn update_webhook_init(
        &self,
        webhook_id: String,
        body: UpdateWebhookRequest,
    ) -> Result<crate::signer::UserActionChallenge, crate::error::Error> {
        let path = format!("/webhooks/{}", urlencoding::encode(&webhook_id));
        let body = serde_json::to_value(&body)?;
        self.client
            .create_user_action_challenge(reqwest::Method::PUT, &path, Some(&body))
            .await
    }

    /// Finishes delegated signing for updateWebhook: submits the signed challenge
    /// and issues the request.
    pub async fn update_webhook_complete(
        &self,
        webhook_id: String,
        body: UpdateWebhookRequest,
        challenge_identifier: String,
        assertion: crate::signer::CredentialAssertion,
    ) -> Result<UpdateWebhookResponse, crate::error::Error> {
        let path = format!("/webhooks/{}", urlencoding::encode(&webhook_id));
        let body = serde_json::to_value(&body)?;
        let user_action = self
            .client
            .complete_user_action_signing(challenge_identifier, &assertion)
            .await?;
        self.client
            .request_with_user_action::<UpdateWebhookResponse>(
                reqwest::Method::PUT,
                &path,
                Some(&body),
                &user_action,
            )
            .await
    }

    /// Starts delegated signing for deleteWebhook: returns the challenge to sign.
    /// Pass the signed assertion to delete_webhook_complete with the same arguments.
    pub async fn delete_webhook_init(
        &self,
        webhook_id: String,
    ) -> Result<crate::signer::UserActionChallenge, crate::error::Error> {
        let path = format!("/webhooks/{}", urlencoding::encode(&webhook_id));
        self.client
            .create_user_action_challenge(reqwest::Method::DELETE, &path, None)
            .await
    }

    /// Finishes delegated signing for deleteWebhook: submits the signed challenge
    /// and issues the request.
    pub async fn delete_webhook_complete(
        &self,
        webhook_id: String,
        challenge_identifier: String,
        assertion: crate::signer::CredentialAssertion,
    ) -> Result<DeleteWebhookResponse, crate::error::Error> {
        let path = format!("/webhooks/{}", urlencoding::encode(&webhook_id));
        let user_action = self
            .client
            .complete_user_action_signing(challenge_identifier, &assertion)
            .await?;
        self.client
            .request_with_user_action::<DeleteWebhookResponse>(
                reqwest::Method::DELETE,
                &path,
                None,
                &user_action,
            )
            .await
    }

    /// Starts delegated signing for pingWebhook: returns the challenge to sign.
    /// Pass the signed assertion to ping_webhook_complete with the same arguments.
    pub async fn ping_webhook_init(
        &self,
        webhook_id: String,
    ) -> Result<crate::signer::UserActionChallenge, crate::error::Error> {
        let path = format!("/webhooks/{}/ping", urlencoding::encode(&webhook_id));
        self.client
            .create_user_action_challenge(reqwest::Method::POST, &path, None)
            .await
    }

    /// Finishes delegated signing for pingWebhook: submits the signed challenge
    /// and issues the request.
    pub async fn ping_webhook_complete(
        &self,
        webhook_id: String,
        challenge_identifier: String,
        assertion: crate::signer::CredentialAssertion,
    ) -> Result<PingWebhookResponse, crate::error::Error> {
        let path = format!("/webhooks/{}/ping", urlencoding::encode(&webhook_id));
        let user_action = self
            .client
            .complete_user_action_signing(challenge_identifier, &assertion)
            .await?;
        self.client
            .request_with_user_action::<PingWebhookResponse>(
                reqwest::Method::POST,
                &path,
                None,
                &user_action,
            )
            .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
    }
}