antimatter 2.0.13

antimatter.io Rust library for data control
Documentation
use super::Session;
use crate::session::session::SessionError;
use crate::session::RUNTIME;
use antimatter_api::apis::contexts_api::{self as api};
use antimatter_api::models::{
    AddWriteContext, ClassifierRule, DomainGetWriteContextClassifierRules200Response,
    DomainInsertWriteContextClassifierRule200Response, WriteContextConfigInfo, WriteContextDetails,
    WriteContextList,
};

impl Session {
    /// Create a new write context for the session's domain.
    ///
    /// Arguments:
    ///
    /// * `context_name` - A &str containing the write context's name.
    /// * `add_write_context` - An AddWriteContext containing the write
    ///     context's configuration.
    pub fn add_write_context(
        &mut self,
        context_name: &str,
        add_write_context: AddWriteContext,
    ) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

        RUNTIME
            .block_on(api::domain_upsert_write_context(
                &conf,
                self.get_domain_id().as_str(),
                context_name,
                add_write_context,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(())
    }

    /// Get a list of write contexts for the session's domain.
    ///
    /// Returns:
    ///
    /// A `Result` containing a `WriteContextList` with a vector of write
    /// context information.
    pub fn list_write_context(&mut self) -> Result<WriteContextList, SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_list_write_contexts(
                &conf,
                self.get_domain_id().as_str(),
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(res)
    }

    /// Get a write context's information for the session's domain.
    ///
    /// Arguments:
    ///
    /// * `context_name` - A &str containing the write context's name.
    ///
    /// Returns:
    ///
    /// A `Result` containing a `WriteContextDetails` with information about
    /// the write context.
    pub fn describe_write_context(
        &mut self,
        context_name: &str,
    ) -> Result<WriteContextDetails, SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_describe_write_context(
                &conf,
                self.get_domain_id().as_str(),
                context_name,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(res)
    }

    /// Upserts a write context in the session's domain.
    ///
    /// Arguments:
    ///
    /// * `context_name` - A &str containing the write context's name.
    /// * `write_context_config_info` - A WriteContextConfigInfo containing
    ///     the write context's configuration.
    pub fn upsert_write_context_configuration(
        &mut self,
        context_name: &str,
        write_context_config_info: WriteContextConfigInfo,
    ) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

        RUNTIME
            .block_on(api::domain_upsert_write_context_configuration(
                &conf,
                self.get_domain_id().as_str(),
                context_name,
                write_context_config_info,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(())
    }

    /// Delete a write context from the session's domain.
    ///
    /// Arguments:
    ///
    /// * `context_name` - A &str containing the write context's name.
    pub fn delete_write_context(&mut self, context_name: &str) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

        RUNTIME
            .block_on(api::domain_delete_write_context(
                &conf,
                self.get_domain_id().as_str(),
                context_name,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(())
    }

    /// Get a list of classifier rules for a write context in the
    /// session's domain.
    ///
    /// Arguments:
    ///
    /// * `context_name` - A &str containing the write context's name
    ///
    /// Returns:
    ///
    /// A `Result` containing a `DomainGetWriteContextClassifierRules200Response`
    /// with a vector of classifier rules.
    pub fn list_write_context_classifier_rules(
        &mut self,
        context_name: &str,
    ) -> Result<DomainGetWriteContextClassifierRules200Response, SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_get_write_context_classifier_rules(
                &conf,
                self.get_domain_id().as_str(),
                context_name,
            ))
            .map_err(|e| SessionError::APIError(e.to_string()))?;

        Ok(res)
    }

    /// Insert a classifier rule for a write context in the session's domain.
    ///
    /// Arguments:
    ///
    /// * `context_name` - A &str containing the write context's name.
    /// * `write_context_classifier_rule` - A ClassifierRule
    ///     containing the classifier rule's configuration.
    pub fn insert_write_context_classifier_rule(
        &mut self,
        context_name: &str,
        write_context_classifier_rule: ClassifierRule,
    ) -> Result<DomainInsertWriteContextClassifierRule200Response, SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_insert_write_context_classifier_rule(
                &conf,
                self.get_domain_id().as_str(),
                context_name,
                write_context_classifier_rule,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(res)
    }

    /// Delete a classifier rule from a write context in the session's domain.
    ///
    /// Arguments:
    ///
    /// * `context_name` - A &str containing the write context's name.
    /// * `rule_id` - A &str containing the classifier rule's ID.
    pub fn delete_write_context_classifier_rule(
        &mut self,
        context_name: &str,
        rule_id: &str,
    ) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

        RUNTIME
            .block_on(api::domain_delete_write_context_classifier_rule(
                &conf,
                self.get_domain_id().as_str(),
                context_name,
                rule_id,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(())
    }

    /// Delete all classifier rules from a write context in the
    /// session's domain.
    ///
    /// Arguments:
    ///
    /// * `context_name` - A &str containing the write context's name.
    pub fn delete_write_context_classifier_rules(
        &mut self,
        context_name: &str,
    ) -> Result<(), SessionError> {
        let rule_list = self
            .list_write_context_classifier_rules(context_name)
            .map_err(|e| e)?;

        for rule in rule_list.rules.unwrap_or_default().iter() {
            self.delete_write_context_classifier_rule(
                context_name,
                rule.clone().id.unwrap().as_str(),
            )
            .map_err(|e| e)?;
        }

        Ok(())
    }
}