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::authentication_api::{self as api};
use antimatter_api::models::{
    CapabilityDefinition, CapabilityDefinitionList, NewCapabilityDefinition,
};

impl Session {
    /// Lists the capabilities for the session's domain.
    ///
    /// # Returns
    ///
    /// A `Result` vector of `CapabilityDefinitionList` containing the list of
    /// capabilities.
    pub fn list_capabilities(&mut self) -> Result<CapabilityDefinitionList, SessionError> {
        let conf = self.get_configuration()?;

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

        Ok(res)
    }

    /// Get details about a specific capability in this session's domain.
    ///
    /// # Arguments
    ///
    /// * `name` - A &str containing the capability name to get.
    ///
    /// # Returns
    ///
    /// A `Result` containing the result `CapabilityDefinition` detailing the
    /// capability.
    pub fn get_capability(&mut self, name: &str) -> Result<CapabilityDefinition, SessionError> {
        let conf = self.get_configuration()?;

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

        Ok(res)
    }

    /// Puts a capability for the session's domain.
    ///
    /// # Arguments
    ///
    /// * `name` - A &str containing the capability name to put.
    /// * `summary` - A String containing the capability's summary.
    /// * `description` - An Option<String> containing the capability's description.
    /// * `unary` - A bool indicating whether the capability is unary.
    /// * `create_only` - An Option<bool> indicating whether this request should be created only.
    pub fn put_capability(
        &mut self,
        name: &str,
        summary: String,
        description: Option<String>,
        unary: bool,
        create_only: Option<bool>,
    ) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

        RUNTIME
            .block_on(api::domain_put_capability(
                &conf,
                self.get_domain_id().as_str(),
                name,
                NewCapabilityDefinition {
                    unary,
                    summary,
                    description: description.unwrap_or_default(),
                },
                create_only,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(())
    }

    /// Deletes a capability in the session's domain.
    ///
    /// # Arguments
    ///
    /// * `name` - A &str containing the capability name to delete.
    pub fn delete_capability(&mut self, name: &str) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

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

        Ok(())
    }
}