dfns-sdk-rust 0.2.0

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

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

/// Delegated client for permissions operations. Signed operations are split into
/// init/complete pairs so the challenge can be signed on the user side.
#[derive(Clone)]
pub struct DelegatedPermissionsClient {
    client: crate::client::Client,
}

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

    /// Starts delegated signing for archivePermission: returns the challenge to sign.
    /// Pass the signed assertion to archive_permission_complete with the same arguments.
    pub async fn archive_permission_init(
        &self,
        permission_id: String,
        body: ArchivePermissionRequest,
    ) -> Result<crate::signer::UserActionChallenge, crate::error::Error> {
        let path = format!(
            "/permissions/{}/archive",
            urlencoding::encode(&permission_id)
        );
        let body = serde_json::to_value(&body)?;
        self.client
            .create_user_action_challenge(reqwest::Method::PUT, &path, Some(&body))
            .await
    }

    /// Finishes delegated signing for archivePermission: submits the signed challenge
    /// and issues the request.
    pub async fn archive_permission_complete(
        &self,
        permission_id: String,
        body: ArchivePermissionRequest,
        challenge_identifier: String,
        assertion: crate::signer::CredentialAssertion,
    ) -> Result<ArchivePermissionResponse, crate::error::Error> {
        let path = format!(
            "/permissions/{}/archive",
            urlencoding::encode(&permission_id)
        );
        let body = serde_json::to_value(&body)?;
        let user_action = self
            .client
            .complete_user_action_signing(challenge_identifier, &assertion)
            .await?;
        self.client
            .request_with_user_action::<ArchivePermissionResponse>(
                reqwest::Method::PUT,
                &path,
                Some(&body),
                &user_action,
            )
            .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
    }

    /// Starts delegated signing for assignPermission: returns the challenge to sign.
    /// Pass the signed assertion to assign_permission_complete with the same arguments.
    pub async fn assign_permission_init(
        &self,
        permission_id: String,
        body: AssignPermissionRequest,
    ) -> Result<crate::signer::UserActionChallenge, crate::error::Error> {
        let path = format!(
            "/permissions/{}/assignments",
            urlencoding::encode(&permission_id)
        );
        let body = serde_json::to_value(&body)?;
        self.client
            .create_user_action_challenge(reqwest::Method::POST, &path, Some(&body))
            .await
    }

    /// Finishes delegated signing for assignPermission: submits the signed challenge
    /// and issues the request.
    pub async fn assign_permission_complete(
        &self,
        permission_id: String,
        body: AssignPermissionRequest,
        challenge_identifier: String,
        assertion: crate::signer::CredentialAssertion,
    ) -> Result<AssignPermissionResponse, crate::error::Error> {
        let path = format!(
            "/permissions/{}/assignments",
            urlencoding::encode(&permission_id)
        );
        let body = serde_json::to_value(&body)?;
        let user_action = self
            .client
            .complete_user_action_signing(challenge_identifier, &assertion)
            .await?;
        self.client
            .request_with_user_action::<AssignPermissionResponse>(
                reqwest::Method::POST,
                &path,
                Some(&body),
                &user_action,
            )
            .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
    }

    /// Starts delegated signing for createPermission: returns the challenge to sign.
    /// Pass the signed assertion to create_permission_complete with the same arguments.
    pub async fn create_permission_init(
        &self,
        body: CreatePermissionRequest,
    ) -> Result<crate::signer::UserActionChallenge, crate::error::Error> {
        let path = String::from("/permissions");
        let body = serde_json::to_value(&body)?;
        self.client
            .create_user_action_challenge(reqwest::Method::POST, &path, Some(&body))
            .await
    }

    /// Finishes delegated signing for createPermission: submits the signed challenge
    /// and issues the request.
    pub async fn create_permission_complete(
        &self,
        body: CreatePermissionRequest,
        challenge_identifier: String,
        assertion: crate::signer::CredentialAssertion,
    ) -> Result<CreatePermissionResponse, crate::error::Error> {
        let path = String::from("/permissions");
        let body = serde_json::to_value(&body)?;
        let user_action = self
            .client
            .complete_user_action_signing(challenge_identifier, &assertion)
            .await?;
        self.client
            .request_with_user_action::<CreatePermissionResponse>(
                reqwest::Method::POST,
                &path,
                Some(&body),
                &user_action,
            )
            .await
    }

    /// Starts delegated signing for revokePermission: returns the challenge to sign.
    /// Pass the signed assertion to revoke_permission_complete with the same arguments.
    pub async fn revoke_permission_init(
        &self,
        permission_id: String,
        assignment_id: String,
        query: Option<RevokePermissionQuery>,
    ) -> Result<crate::signer::UserActionChallenge, 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
            .create_user_action_challenge(reqwest::Method::DELETE, &path, None)
            .await
    }

    /// Finishes delegated signing for revokePermission: submits the signed challenge
    /// and issues the request.
    pub async fn revoke_permission_complete(
        &self,
        permission_id: String,
        assignment_id: String,
        query: Option<RevokePermissionQuery>,
        challenge_identifier: String,
        assertion: crate::signer::CredentialAssertion,
    ) -> 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("&"));
            }
        }
        let user_action = self
            .client
            .complete_user_action_signing(challenge_identifier, &assertion)
            .await?;
        self.client
            .request_no_content_with_user_action(reqwest::Method::DELETE, &path, None, &user_action)
            .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
    }

    /// Starts delegated signing for updatePermission: returns the challenge to sign.
    /// Pass the signed assertion to update_permission_complete with the same arguments.
    pub async fn update_permission_init(
        &self,
        permission_id: String,
        body: UpdatePermissionRequest,
    ) -> Result<crate::signer::UserActionChallenge, crate::error::Error> {
        let path = format!("/permissions/{}", urlencoding::encode(&permission_id));
        let body = serde_json::to_value(&body)?;
        self.client
            .create_user_action_challenge(reqwest::Method::PUT, &path, Some(&body))
            .await
    }

    /// Finishes delegated signing for updatePermission: submits the signed challenge
    /// and issues the request.
    pub async fn update_permission_complete(
        &self,
        permission_id: String,
        body: UpdatePermissionRequest,
        challenge_identifier: String,
        assertion: crate::signer::CredentialAssertion,
    ) -> Result<UpdatePermissionResponse, crate::error::Error> {
        let path = format!("/permissions/{}", urlencoding::encode(&permission_id));
        let body = serde_json::to_value(&body)?;
        let user_action = self
            .client
            .complete_user_action_signing(challenge_identifier, &assertion)
            .await?;
        self.client
            .request_with_user_action::<UpdatePermissionResponse>(
                reqwest::Method::PUT,
                &path,
                Some(&body),
                &user_action,
            )
            .await
    }
}