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::{AddReadContext, ReadContextDetails, ReadContextList};

impl Session {
    /// Fetches a list of all read contexts for the session's domain.
    ///
    /// Returns:
    ///
    /// A `Result` containing a `ReadContextList` with a vector of read context
    /// information.
    pub fn list_read_context(&mut self) -> Result<ReadContextList, SessionError> {
        let conf = self.get_configuration()?;

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

        Ok(res)
    }

    /// Creates a new read contexts for the session's domain.
    ///
    /// Arguments:
    ///
    /// * `context_name` - A &str containing the read context's name.
    /// * `add_read_context` - An AddReadContext containing the read
    ///     context's configuration.
    pub fn add_read_context(
        &mut self,
        context_name: &str,
        add_read_context: AddReadContext,
    ) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

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

        Ok(())
    }

    /// Get the information for a read context in the session's domain. It can
    /// optionally return the policy bundle for the read context.
    ///
    /// Arguments:
    ///
    /// * `context_name` - A &str containing the read context's name.
    /// * `include_policy_bundle` - An Option<bool> indicates whether the policy
    ///     bundle for the read context should also be returned.
    ///
    /// Results:
    /// A `Result` containing a `ReadContextDetails` with the read context's
    /// information.
    pub fn describe_read_context(
        &mut self,
        context_name: &str,
        include_policy_bundle: Option<bool>,
    ) -> Result<ReadContextDetails, SessionError> {
        let conf = self.get_configuration()?;

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

        Ok(res)
    }

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

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

        Ok(())
    }
}