spiral-sdk 0.1.10

Rust SDK for Spiral resource and table APIs
Documentation
// Generated by tools/service-contracts/rust.py. Do not edit.
#![doc = "Generated implementation of authenticated SDK resource facades."]
use spiral_contract::call::CallError;

use crate::api::workloads::WorkloadsClient;
use crate::api::workloads::objects::*;
use crate::auth::TokenSource;
use crate::http::HttpClient;
/// Service operations sharing a connection pool and resolving credentials for every request.
#[derive(Clone, Debug)]
pub struct AuthenticatedClient {
    http: HttpClient,
    client: WorkloadsClient,
}
impl AuthenticatedClient {
    /// Connect with bounded requests; configure credentials before authenticated calls.
    pub fn new(base: &str) -> Result<Self, CallError<WorkloadsError>> {
        Self::from_http(HttpClient::connect(base).map_err(CallError::transport)?)
    }
    pub(crate) fn from_http(http: HttpClient) -> Result<Self, CallError<WorkloadsError>> {
        let client =
            WorkloadsClient::from_transport(http.base_url().as_str(), http.middleware_client())?;
        Ok(Self { http, client })
    }
    /// Reuse a connection pool with redirects disabled to protect bearer credentials.
    pub fn with_client(mut self, client: reqwest::Client) -> Self {
        self.http = self.http.with_client(client);
        self.client = self.client.with_transport(self.http.middleware_client());
        self
    }
    /// Resolve the provider's current credential before each request and pagination fetch.
    pub fn with_token_source(mut self, provider: TokenSource) -> Self {
        self.http = self.http.with_token_source(provider);
        self
    }
    /// Register a public key with workload manage credentials permission.
    pub async fn add_key(
        &self,
        workload_id: String,
        request: CreateKeyRequest,
    ) -> Result<KeyResponse, CallError<WorkloadsError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client.add_key_http(auth_header, workload_id, request),
        )
        .await
        .map_err(CallError::transport)?
    }
    /// Trust an external issuer with workload manage policies permission.
    pub async fn create_policy(
        &self,
        workload_id: String,
        request: CreatePolicyRequest,
    ) -> Result<PolicyResponse, CallError<WorkloadsError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client
                .create_policy_http(auth_header, workload_id, request),
        )
        .await
        .map_err(CallError::transport)?
    }
    /// Create a machine identity with workload create permission in the Project.
    pub async fn create_workload(
        &self,
        project_id: String,
        request: CreateWorkloadRequest,
    ) -> Result<WorkloadResponse, CallError<WorkloadsError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client
                .create_workload_http(auth_header, project_id, request),
        )
        .await
        .map_err(CallError::transport)?
    }
    /// Deactivate a machine identity and revoke its credentials and trust policies with workload
    /// create permission.
    pub async fn deactivate_workload(
        &self,
        workload_id: String,
    ) -> Result<(), CallError<WorkloadsError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client
                .deactivate_workload_http(auth_header, workload_id),
        )
        .await
        .map_err(CallError::transport)?
    }
    /// List public keys with workload view permission.
    pub async fn list_keys(
        &self,
        workload_id: String,
        request: PageRequest,
    ) -> Result<KeyPage, CallError<WorkloadsError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client
                .list_keys_http(auth_header, workload_id, request),
        )
        .await
        .map_err(CallError::transport)?
    }
    /// Build a lazy collection, retaining filters and resolving fresh credentials for each page.
    pub fn list_keys_list(
        &self,
        workload_id: String,
        request: PageRequest,
    ) -> crate::List<'_, Key> {
        let options = crate::pagination::PageOptions {
            token: request.page_token.clone(),
            size: request.page_size,
        };
        crate::List::new(move |options| {
            let workload_id = ToOwned::to_owned(&workload_id);
            let mut request = request.clone();
            request.page_token = options.token;
            request.page_size = options.size;
            Box::pin(async move {
                let page = self.list_keys(workload_id, request).await?;
                Ok(crate::Page {
                    items: page.items,
                    next_page_token: page.next_page_token,
                })
            })
        })
        .with_options(options)
    }
    /// List trust policies with workload view permission.
    pub async fn list_policies(
        &self,
        workload_id: String,
        request: PageRequest,
    ) -> Result<PolicyPage, CallError<WorkloadsError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client
                .list_policies_http(auth_header, workload_id, request),
        )
        .await
        .map_err(CallError::transport)?
    }
    /// Build a lazy collection, retaining filters and resolving fresh credentials for each page.
    pub fn list_policies_list(
        &self,
        workload_id: String,
        request: PageRequest,
    ) -> crate::List<'_, Policy> {
        let options = crate::pagination::PageOptions {
            token: request.page_token.clone(),
            size: request.page_size,
        };
        crate::List::new(move |options| {
            let workload_id = ToOwned::to_owned(&workload_id);
            let mut request = request.clone();
            request.page_token = options.token;
            request.page_size = options.size;
            Box::pin(async move {
                let page = self.list_policies(workload_id, request).await?;
                Ok(crate::Page {
                    items: page.items,
                    next_page_token: page.next_page_token,
                })
            })
        })
        .with_options(options)
    }
    /// List active machine identities with workload view permission in the Project.
    pub async fn list_workloads(
        &self,
        project_id: String,
        request: PageRequest,
    ) -> Result<WorkloadPage, CallError<WorkloadsError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client
                .list_workloads_http(auth_header, project_id, request),
        )
        .await
        .map_err(CallError::transport)?
    }
    /// Build a lazy collection, retaining filters and resolving fresh credentials for each page.
    pub fn list_workloads_list(
        &self,
        project_id: String,
        request: PageRequest,
    ) -> crate::List<'_, Workload> {
        let options = crate::pagination::PageOptions {
            token: request.page_token.clone(),
            size: request.page_size,
        };
        crate::List::new(move |options| {
            let project_id = ToOwned::to_owned(&project_id);
            let mut request = request.clone();
            request.page_token = options.token;
            request.page_size = options.size;
            Box::pin(async move {
                let page = self.list_workloads(project_id, request).await?;
                Ok(crate::Page {
                    items: page.items,
                    next_page_token: page.next_page_token,
                })
            })
        })
        .with_options(options)
    }
    /// Read a machine identity with workload view permission.
    pub async fn read_workload(
        &self,
        workload_id: String,
    ) -> Result<Workload, CallError<WorkloadsError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client.read_workload_http(auth_header, workload_id),
        )
        .await
        .map_err(CallError::transport)?
    }
    /// Remove a public key with workload manage credentials permission.
    pub async fn remove_key(
        &self,
        workload_id: String,
        kid: String,
    ) -> Result<(), CallError<WorkloadsError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client.remove_key_http(auth_header, workload_id, kid),
        )
        .await
        .map_err(CallError::transport)?
    }
    /// Remove a trust policy with workload manage policies permission.
    pub async fn remove_policy(&self, policy_id: String) -> Result<(), CallError<WorkloadsError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client.remove_policy_http(auth_header, policy_id),
        )
        .await
        .map_err(CallError::transport)?
    }
    /// Revoke an existing client credential with workload manage credentials permission.
    pub async fn revoke_credentials(
        &self,
        client_id: String,
    ) -> Result<(), CallError<WorkloadsError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client.revoke_credentials_http(auth_header, client_id),
        )
        .await
        .map_err(CallError::transport)?
    }
}