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::authorization::AuthorizationClient;
use crate::api::authorization::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: AuthorizationClient,
}
impl AuthenticatedClient {
    /// Connect with bounded requests; configure credentials before authenticated calls.
    pub fn new(base: &str) -> Result<Self, CallError<AuthorizationError>> {
        Self::from_http(HttpClient::connect(base).map_err(CallError::transport)?)
    }
    pub(crate) fn from_http(http: HttpClient) -> Result<Self, CallError<AuthorizationError>> {
        let client = AuthorizationClient::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
    }
    /// Approve requested resource permissions as an authorized resource owner.
    pub async fn approve_installation(
        &self,
        installation_id: String,
    ) -> Result<ReviewResponse, CallError<AuthorizationError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client
                .approve_installation_http(auth_header, installation_id),
        )
        .await
        .map_err(CallError::transport)?
    }
    /// Grant a Project role with project grant role permission.
    pub async fn grant_role(
        &self,
        project_id: String,
        request: GrantRoleRequest,
    ) -> Result<GrantResponse, CallError<AuthorizationError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client
                .grant_role_http(auth_header, project_id, request),
        )
        .await
        .map_err(CallError::transport)?
    }
    /// List active Project role grants with project view grants permission. Revoked grants are
    /// excluded.
    pub async fn list_grants(
        &self,
        project_id: String,
        request: ListGrantsRequest,
    ) -> Result<GrantPage, CallError<AuthorizationError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client
                .list_grants_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_grants_list(
        &self,
        project_id: String,
        request: ListGrantsRequest,
    ) -> crate::List<'_, Grant> {
        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_grants(project_id, request).await?;
                Ok(crate::Page {
                    items: page.items,
                    next_page_token: page.next_page_token,
                })
            })
        })
        .with_options(options)
    }
    /// Read a role grant by its durable ID with project view grants permission, including revoked
    /// grants.
    pub async fn read_grant(
        &self,
        grant_id: String,
    ) -> Result<Grant, CallError<AuthorizationError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client.read_grant_http(auth_header, grant_id),
        )
        .await
        .map_err(CallError::transport)?
    }
    /// Read an installation and its requested resource permissions as an authorized resource owner.
    pub async fn read_installation(
        &self,
        installation_id: String,
    ) -> Result<InstallationResponse, CallError<AuthorizationError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client
                .read_installation_http(auth_header, installation_id),
        )
        .await
        .map_err(CallError::transport)?
    }
    /// Reject requested resource permissions as an authorized resource owner.
    pub async fn reject_installation(
        &self,
        installation_id: String,
    ) -> Result<ReviewResponse, CallError<AuthorizationError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client
                .reject_installation_http(auth_header, installation_id),
        )
        .await
        .map_err(CallError::transport)?
    }
    /// Review requested resource permissions as an authorized resource owner.
    pub async fn review_installation(
        &self,
        installation_id: String,
        request: ReviewRequest,
    ) -> Result<ReviewResponse, CallError<AuthorizationError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client
                .review_installation_http(auth_header, installation_id, request),
        )
        .await
        .map_err(CallError::transport)?
    }
    /// Revoke a Project role grant with project grant role permission.
    pub async fn revoke_grant(
        &self,
        grant_id: String,
    ) -> Result<(), CallError<AuthorizationError>> {
        let auth_header = self
            .http
            .auth_header()
            .await
            .map_err(CallError::transport)?;
        tokio::time::timeout(
            std::time::Duration::from_secs(60),
            self.client.revoke_grant_http(auth_header, grant_id),
        )
        .await
        .map_err(CallError::transport)?
    }
}