dfns-sdk-rust 0.2.0

Dfns API SDK for Rust
Documentation
// Code generated by rust-sdk-generator. DO NOT EDIT.

pub mod delegated;
pub mod types;

#[allow(unused_imports)]
use types::*;

/// Client for permissions operations.
#[derive(Clone)]
pub struct PermissionsClient {
    client: crate::client::Client,
}

impl PermissionsClient {
    pub fn new(client: crate::client::Client) -> Self {
        PermissionsClient { client }
    }

    /// Archives or unarchives a permission (role). Archived permissions are effectively soft-deleted.
    pub async fn archive_permission(
        &self,
        permission_id: String,
        body: ArchivePermissionRequest,
    ) -> Result<ArchivePermissionResponse, crate::error::Error> {
        let path = format!(
            "/permissions/{}/archive",
            urlencoding::encode(&permission_id)
        );
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<ArchivePermissionResponse>(reqwest::Method::PUT, &path, Some(&body), true)
            .await
    }

    /// Lists all permission (role) assignments for a given permission.
    pub async fn list_permission_assignments(
        &self,
        permission_id: String,
        query: Option<ListPermissionAssignmentsQuery>,
    ) -> Result<ListPermissionAssignmentsResponse, crate::error::Error> {
        let mut path = format!(
            "/permissions/{}/assignments",
            urlencoding::encode(&permission_id)
        );
        if let Some(query) = &query {
            let mut q: Vec<String> = Vec::new();
            if let Some(v) = &query.limit {
                q.push(format!("limit={}", urlencoding::encode(&v.to_string())));
            }
            if let Some(v) = &query.pagination_token {
                q.push(format!(
                    "paginationToken={}",
                    urlencoding::encode(&v.to_string())
                ));
            }
            if !q.is_empty() {
                path.push('?');
                path.push_str(&q.join("&"));
            }
        }
        self.client
            .request::<ListPermissionAssignmentsResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Assigns a permission (role) to an identity (user, PAT or service account), granting it access to the operations defined in the permission. Returns the assignment on success (200), or a pending change request if approval is required (202).
    pub async fn assign_permission(
        &self,
        permission_id: String,
        body: AssignPermissionRequest,
    ) -> Result<AssignPermissionResponse, crate::error::Error> {
        let path = format!(
            "/permissions/{}/assignments",
            urlencoding::encode(&permission_id)
        );
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<AssignPermissionResponse>(reqwest::Method::POST, &path, Some(&body), true)
            .await
    }

    /// Lists all permissions (roles) in the organization.
    pub async fn list_permissions(
        &self,
        query: Option<ListPermissionsQuery>,
    ) -> Result<ListPermissionsResponse, crate::error::Error> {
        let mut path = String::from("/permissions");
        if let Some(query) = &query {
            let mut q: Vec<String> = Vec::new();
            if let Some(v) = &query.limit {
                q.push(format!("limit={}", urlencoding::encode(&v.to_string())));
            }
            if let Some(v) = &query.pagination_token {
                q.push(format!(
                    "paginationToken={}",
                    urlencoding::encode(&v.to_string())
                ));
            }
            if !q.is_empty() {
                path.push('?');
                path.push_str(&q.join("&"));
            }
        }
        self.client
            .request::<ListPermissionsResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Creates a new permission (also referred to as "role" in the dashboard) that grants access to the specified API operations.
    pub async fn create_permission(
        &self,
        body: CreatePermissionRequest,
    ) -> Result<CreatePermissionResponse, crate::error::Error> {
        let path = String::from("/permissions");
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<CreatePermissionResponse>(reqwest::Method::POST, &path, Some(&body), true)
            .await
    }

    /// Revokes a permission (role) assignment, removing the identity's access to the operations granted by the permission.
    pub async fn revoke_permission(
        &self,
        permission_id: String,
        assignment_id: String,
        query: Option<RevokePermissionQuery>,
    ) -> Result<(), crate::error::Error> {
        let mut path = format!(
            "/permissions/{}/assignments/{}",
            urlencoding::encode(&permission_id),
            urlencoding::encode(&assignment_id)
        );
        if let Some(query) = &query {
            let mut q: Vec<String> = Vec::new();
            if let Some(v) = &query.force {
                q.push(format!("force={}", urlencoding::encode(&v.to_string())));
            }
            if !q.is_empty() {
                path.push('?');
                path.push_str(&q.join("&"));
            }
        }
        self.client
            .request_no_content(reqwest::Method::DELETE, &path, None, true)
            .await
    }

    /// Retrieves a permission (role) by ID, including any pending change request.
    pub async fn get_permission(
        &self,
        permission_id: String,
    ) -> Result<GetPermissionResponse, crate::error::Error> {
        let path = format!("/permissions/{}", urlencoding::encode(&permission_id));
        self.client
            .request::<GetPermissionResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Updates the name or operations of an existing permission (role).
    pub async fn update_permission(
        &self,
        permission_id: String,
        body: UpdatePermissionRequest,
    ) -> Result<UpdatePermissionResponse, crate::error::Error> {
        let path = format!("/permissions/{}", urlencoding::encode(&permission_id));
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<UpdatePermissionResponse>(reqwest::Method::PUT, &path, Some(&body), true)
            .await
    }
}