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::capsules_api::{self as api};
use antimatter_api::apis::internal_api;
use antimatter_api::apis::Error::ResponseError;
use antimatter_api::models::{
    CapsuleInfo, CapsuleList, CapsuleOpenRequest, CapsuleOpenResponse, DeleteTags,
    DomainUpsertCapsuleTagsRequest, Tag,
};

impl Session {
    /// Lists the capsules for the session's domain, allowing for optional
    /// filtering.
    ///
    /// # Arguments
    ///
    /// * `start_date` - An Option<String> to retrieve capsule after
    ///     (format "YYYY-MM-DD HH:MM:SS").
    /// * `end_date` - An Option<String> to retrieve capsule before
    ///     (format "YYYY-MM-DD HH:MM:SS").
    /// * `num_results` - An Option<i32> indicating the number of capsules to
    ///     retrieve.
    /// * `span_tags` - An Option<&str> to filter by span tag name.
    /// * `sort_on` - An Option<&str> to sort results on ("created", "size").
    /// * `start_after` - An Option<&str> page key to get results after/before.
    /// * `ascending` - An Option<bool> indicates result order (false=descending).
    ///
    /// # Returns
    ///
    /// A `Result` containing a `CapsuleList` with a vector of `CapsuleInfo`.
    pub fn list_capsules(
        &mut self,
        start_date: Option<String>,
        end_date: Option<String>,
        num_results: Option<i32>,
        span_tags: Option<&str>,
        sort_on: Option<&str>,
        start_after: Option<&str>,
        ascending: Option<bool>,
    ) -> Result<CapsuleList, SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_list_capsules(
                &conf,
                self.get_domain_id().as_str(),
                start_date,
                end_date,
                num_results,
                span_tags,
                sort_on,
                start_after,
                ascending,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(res)
    }

    /// Gets details for a capsule in the session's domain.
    ///
    /// # Arguments
    ///
    /// * `capsule_id` - A &str containing the capsule's ID.
    ///
    /// # Returns
    ///
    /// A `Result` containing a `CapsuleInfo` detailing the capsule.
    pub fn get_capsule_info(&mut self, capsule_id: &str) -> Result<CapsuleInfo, SessionError> {
        let conf = self.get_configuration()?;

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

        Ok(res)
    }

    /// Upsert a capsule's capsule tags with the provided vector of tags.
    ///
    /// # Arguments
    ///
    /// * `capsule_id` - A &str containing the capsule's ID.
    /// * `tags` - A Vec<Tag> containing capsule tags.
    pub fn upsert_capsule_tags(
        &mut self,
        capsule_id: &str,
        tags: Vec<Tag>,
    ) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

        RUNTIME
            .block_on(api::domain_upsert_capsule_tags(
                &conf,
                self.get_domain_id().as_str(),
                capsule_id,
                DomainUpsertCapsuleTagsRequest { tags: Some(tags) },
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(())
    }

    /// Deletes capsule tags from a capsule's capsule tags.
    ///
    /// # Arguments
    ///
    /// * `capsule_id` - A &str containing the capsule's ID.
    /// * `tags` - A DeleteTags containing capsule tags to delete.
    pub fn delete_capsule_tags(
        &mut self,
        capsule_id: &str,
        tags: DeleteTags,
    ) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

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

        Ok(())
    }

    /// Given a capsule ID, read context and encrypted DEK, open the capsule and return the decrypted DEK
    ///
    /// # Arguments
    ///
    /// * `capsule_id`   - A &str containing the capsule's ID.
    /// * `read_context` - A &str containing the read context to use when opening the capsule.
    /// * `domain_id`.   - An optional &str. If provided, will be used in the request. If not, the
    ///                    default domain will be used instead.
    /// * `req`          - An open capsule request containing the encrypted REK, its key ID and any
    ///                    optional read parameters.
    pub fn open_capsule(
        &mut self,
        capsule_id: &str,
        read_context: &str,
        domain_id: Option<&str>,
        req: CapsuleOpenRequest,
    ) -> Result<CapsuleOpenResponse, SessionError> {
        let conf = self.get_configuration()?;
        RUNTIME
            .block_on(internal_api::domain_open_capsule(
                &conf,
                domain_id.unwrap_or(self.get_domain_id().as_str()),
                capsule_id,
                read_context,
                req,
            ))
            .map_err(|e| match e {
                ResponseError(e) => match e.status {
                    reqwest::StatusCode::UNAUTHORIZED => {
                        SessionError::Status401(format!(" {}", e.content))
                    }
                    code => SessionError::APIError(format!(
                        "open request failed ({}): {}",
                        code, e.content
                    )),
                },
                e => SessionError::APIError(format!("unknown error opening capsule: {}", e)),
            })
    }
}