whop_sdk 1.0.13

The official Rust SDK for the Whop API.
use crate::api::*;
use crate::{ApiError, ClientConfig, HttpClient, QueryBuilder, RequestOptions};
use reqwest::Method;

pub struct DmChannelsClient {
    pub http_client: HttpClient,
}

impl DmChannelsClient {
    pub fn new(config: ClientConfig) -> Result<Self, ApiError> {
        Ok(Self {
            http_client: HttpClient::new(config.clone())?,
        })
    }

    /// Returns a paginated list of DM channels for the currently authenticated user, sorted by most recently active.
    ///
    /// Required permissions:
    /// - `dms:read`
    ///
    /// # Arguments
    ///
    /// * `after` - Returns the elements in the list that come after the specified cursor.
    /// * `before` - Returns the elements in the list that come before the specified cursor.
    /// * `first` - Returns the first _n_ elements from the list.
    /// * `last` - Returns the last _n_ elements from the list.
    /// * `company_id` - The unique identifier of a company to filter DM channels by. Only returns channels scoped to this company.
    /// * `options` - Additional request options such as headers, timeout, etc.
    ///
    /// # Returns
    ///
    /// JSON response from the API
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use whop_sdk::prelude::*;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let config = ClientConfig {
    ///         token: Some("<token>".to_string()),
    ///         ..Default::default()
    ///     };
    ///     let client = Whop::new(config).expect("Failed to build client");
    ///     client
    ///         .dm_channels
    ///         .list(
    ///             &DmChannelsListQueryRequest {
    ///                 first: Some(42),
    ///                 last: Some(42),
    ///                 company_id: Some("biz_xxxxxxxxxxxxxx".to_string()),
    ///                 ..Default::default()
    ///             },
    ///             None,
    ///         )
    ///         .await;
    /// }
    /// ```
    pub async fn list(
        &self,
        request: &DmChannelsListQueryRequest,
        options: Option<RequestOptions>,
    ) -> Result<ListDmChannelsResponse, ApiError> {
        self.http_client
            .execute_request(
                Method::GET,
                "dm_channels",
                None,
                QueryBuilder::new()
                    .string("after", request.after.clone())
                    .string("before", request.before.clone())
                    .int("first", request.first.clone())
                    .int("last", request.last.clone())
                    .string("company_id", request.company_id.clone())
                    .build(),
                options,
            )
            .await
    }

    /// Create a new DM channel between two or more users, optionally scoped to a specific company. Returns the existing channel if one already exists.
    ///
    /// Required permissions:
    /// - `dms:channel:manage`
    ///
    /// # Arguments
    ///
    /// * `options` - Additional request options such as headers, timeout, etc.
    ///
    /// # Returns
    ///
    /// JSON response from the API
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use whop_sdk::prelude::*;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let config = ClientConfig {
    ///         token: Some("<token>".to_string()),
    ///         ..Default::default()
    ///     };
    ///     let client = Whop::new(config).expect("Failed to build client");
    ///     client
    ///         .dm_channels
    ///         .create(
    ///             &CreateDmChannelsRequest {
    ///                 with_user_ids: vec!["with_user_ids".to_string()],
    ///                 company_id: None,
    ///                 custom_name: None,
    ///                 notifications_enabled: None,
    ///             },
    ///             None,
    ///         )
    ///         .await;
    /// }
    /// ```
    pub async fn create(
        &self,
        request: &CreateDmChannelsRequest,
        options: Option<RequestOptions>,
    ) -> Result<DmChannel, ApiError> {
        self.http_client
            .execute_request(
                Method::POST,
                "dm_channels",
                Some(serde_json::to_value(request).map_err(ApiError::Serialization)?),
                None,
                options,
            )
            .await
    }

    /// Retrieves the details of an existing DM channel.
    ///
    /// Required permissions (one of):
    /// - `dms:read`
    /// - `support_chat:read`
    ///
    /// # Arguments
    ///
    /// * `id` - The unique identifier of the DM channel to retrieve.
    /// * `options` - Additional request options such as headers, timeout, etc.
    ///
    /// # Returns
    ///
    /// JSON response from the API
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use whop_sdk::prelude::*;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let config = ClientConfig {
    ///         token: Some("<token>".to_string()),
    ///         ..Default::default()
    ///     };
    ///     let client = Whop::new(config).expect("Failed to build client");
    ///     client.dm_channels.retrieve(&"id".to_string(), None).await;
    /// }
    /// ```
    pub async fn retrieve(
        &self,
        id: &str,
        options: Option<RequestOptions>,
    ) -> Result<DmChannel, ApiError> {
        self.http_client
            .execute_request(
                Method::GET,
                &format!("dm_channels/{}", id),
                None,
                None,
                options,
            )
            .await
    }

    /// Permanently delete a DM channel and all of its messages. Only an admin of the channel can perform this action.
    ///
    /// Required permissions (one of):
    /// - `dms:channel:manage`
    /// - `support_chat:create`
    ///
    /// # Arguments
    ///
    /// * `id` - The unique identifier of the DM channel to delete.
    /// * `options` - Additional request options such as headers, timeout, etc.
    ///
    /// # Returns
    ///
    /// JSON response from the API
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use whop_sdk::prelude::*;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let config = ClientConfig {
    ///         token: Some("<token>".to_string()),
    ///         ..Default::default()
    ///     };
    ///     let client = Whop::new(config).expect("Failed to build client");
    ///     client.dm_channels.delete(&"id".to_string(), None).await;
    /// }
    /// ```
    pub async fn delete(
        &self,
        id: &str,
        options: Option<RequestOptions>,
    ) -> Result<bool, ApiError> {
        self.http_client
            .execute_request(
                Method::DELETE,
                &format!("dm_channels/{}", id),
                None,
                None,
                options,
            )
            .await
    }

    /// Update the settings of an existing DM channel, such as its display name. Only an admin of the channel can perform this action.
    ///
    /// Required permissions (one of):
    /// - `dms:channel:manage`
    /// - `support_chat:create`
    ///
    /// # Arguments
    ///
    /// * `id` - The unique identifier of the DM channel to update.
    /// * `options` - Additional request options such as headers, timeout, etc.
    ///
    /// # Returns
    ///
    /// JSON response from the API
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use whop_sdk::prelude::*;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let config = ClientConfig {
    ///         token: Some("<token>".to_string()),
    ///         ..Default::default()
    ///     };
    ///     let client = Whop::new(config).expect("Failed to build client");
    ///     client
    ///         .dm_channels
    ///         .update(
    ///             &"id".to_string(),
    ///             &UpdateDmChannelsRequest {
    ///                 ..Default::default()
    ///             },
    ///             None,
    ///         )
    ///         .await;
    /// }
    /// ```
    pub async fn update(
        &self,
        id: &str,
        request: &UpdateDmChannelsRequest,
        options: Option<RequestOptions>,
    ) -> Result<DmChannel, ApiError> {
        self.http_client
            .execute_request(
                Method::PATCH,
                &format!("dm_channels/{}", id),
                Some(serde_json::to_value(request).map_err(ApiError::Serialization)?),
                None,
                options,
            )
            .await
    }
}