fundaia 0.53.0

Command line for the Fundaia deployment platform: projects, services, variables, deployments, logs and metrics
use super::*;

impl ApiClient {
    pub async fn whoami(&self) -> Result<Option<PrincipalIdentity>> {
        Ok(self.get::<Principal>("/api/auth/session").await?.principal)
    }

    pub async fn version(&self) -> Result<ServerVersion> {
        self.get("/api/version").await
    }

    pub async fn workspaces(&self) -> Result<Vec<Workspace>> {
        Ok(self
            .get::<WorkspacesPage>("/api/infra/workspaces")
            .await?
            .workspaces)
    }

    pub async fn find_workspace(&self, reference: Option<&str>) -> Result<Workspace> {
        let workspaces = self.workspaces().await?;

        let Some(reference) = reference else {
            return workspaces
                .into_iter()
                .next()
                .ok_or_else(|| anyhow!("this account is in no workspace"));
        };

        one_named(workspaces, reference, "workspaces", || {
            format!("there is no workspace called `{reference}`")
        })
    }

    pub async fn sign_ins(&self, limit: usize) -> Result<Vec<SignIn>> {
        Ok(self
            .get::<SignInPage>(&format!("/api/auth/sign-ins?limit={limit}"))
            .await?
            .sign_ins)
    }

    pub async fn auth_sessions(&self) -> Result<Vec<AuthSession>> {
        Ok(self
            .get::<AuthSessionPage>("/api/auth/sessions")
            .await?
            .sessions)
    }

    pub async fn revoke_session(&self, session_id: &str) -> Result<()> {
        self.delete(&format!("/api/auth/sessions/{session_id}"))
            .await
    }

    pub async fn members(&self, workspace_id: &str) -> Result<Vec<Member>> {
        Ok(self
            .get::<MembersPage>(&format!("/api/infra/workspaces/{workspace_id}/members"))
            .await?
            .members)
    }

    pub async fn require_second_factor(
        &self,
        workspace_id: &str,
        required: bool,
    ) -> Result<Answer<WorkspacePage>> {
        self.answer(
            Method::PUT,
            &format!("/api/infra/workspaces/{workspace_id}/second-factor"),
            Some(&serde_json::json!({ "required": required })),
        )
        .await
    }

    pub async fn credits(&self, workspace_id: &str) -> Result<Credits> {
        self.get::<Credits>(&format!("/api/infra/workspaces/{workspace_id}/credits"))
            .await
    }

    pub async fn invitations(&self, workspace_id: &str) -> Result<Vec<Invitation>> {
        Ok(self
            .get::<InvitationsPage>(&format!("/api/infra/workspaces/{workspace_id}/invitations"))
            .await?
            .invitations)
    }

    pub async fn invite(
        &self,
        workspace_id: &str,
        email: &str,
        role: &str,
    ) -> Result<SentInvitation> {
        self.post(
            &format!("/api/infra/workspaces/{workspace_id}/invitations"),
            &NewInvitation {
                email: email.to_string(),
                role: role.to_string(),
            },
        )
        .await
    }

    pub async fn revoke_invitation(&self, workspace_id: &str, invitation_id: &str) -> Result<()> {
        self.delete(&format!(
            "/api/infra/workspaces/{workspace_id}/invitations/{invitation_id}"
        ))
        .await
    }

    pub async fn remove_member(&self, workspace_id: &str, user_id: &str) -> Result<()> {
        self.delete(&format!(
            "/api/infra/workspaces/{workspace_id}/members/{user_id}"
        ))
        .await
    }
}