fundaia 0.55.0

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

impl ApiClient {
    pub async fn service_detail(&self, service_environment_id: &str) -> Result<ServiceDetail> {
        self.get(&format!("/api/infra/services/{service_environment_id}"))
            .await
    }

    pub async fn repository_access(
        &self,
        service_environment_id: &str,
    ) -> Result<RepositoryAccessPage> {
        self.get::<RepositoryAccessPage>(&format!(
            "/api/infra/services/{service_environment_id}/repository-access"
        ))
        .await
    }

    pub async fn request_repository_access(
        &self,
        service_environment_id: &str,
        email: Option<&str>,
    ) -> Result<SentRepositoryAccess> {
        let body = match email {
            Some(address) => serde_json::json!({ "email": address }),
            None => serde_json::json!({}),
        };

        self.post::<SentRepositoryAccess, _>(
            &format!("/api/infra/services/{service_environment_id}/repository-access"),
            &body,
        )
        .await
    }

    pub async fn usage(
        &self,
        service_environment_id: &str,
        minutes: u32,
    ) -> Result<Vec<UsageSample>> {
        Ok(self
            .get::<UsagePage>(&format!(
                "/api/infra/services/{service_environment_id}/usage?minutes={minutes}"
            ))
            .await?
            .samples)
    }

    pub async fn consumption(
        &self,
        service_environment_id: &str,
        range: &str,
    ) -> Result<ConsumptionPage> {
        self.get::<ConsumptionPage>(&format!(
            "/api/infra/services/{service_environment_id}/consumption?range={range}"
        ))
        .await
    }

    pub async fn agent_activity(
        &self,
        service_environment_id: &str,
        range: &str,
        outcome: Option<&str>,
    ) -> Result<AgentActivityPage> {
        let filter = outcome.map_or_else(String::new, |one| format!("&outcome={one}"));

        self.get::<AgentActivityPage>(&format!(
            "/api/infra/services/{service_environment_id}/activity?range={range}{filter}"
        ))
        .await
    }

    pub async fn templates(&self) -> Result<TemplatesPage> {
        self.get::<TemplatesPage>("/api/infra/templates").await
    }

    pub async fn agent_setup(&self, service_environment_id: &str) -> Result<AgentSetup> {
        self.get::<AgentSetup>(&format!(
            "/api/infra/services/{service_environment_id}/agent"
        ))
        .await
    }

    pub async fn configure_agent(
        &self,
        service_environment_id: &str,
        patch: &AgentSetupPatch,
    ) -> Result<AgentSetup> {
        self.put::<AgentSetup, _>(
            &format!("/api/infra/services/{service_environment_id}/agent"),
            patch,
        )
        .await
    }

    pub async fn service_config(&self, service_environment_id: &str) -> Result<ServiceConfigPage> {
        self.get::<ServiceConfigPage>(&format!(
            "/api/infra/services/{service_environment_id}/config"
        ))
        .await
    }

    pub async fn edit_service_config(
        &self,
        service_environment_id: &str,
        content: &str,
    ) -> Result<()> {
        self.put::<ServiceConfigResponse, _>(
            &format!("/api/infra/services/{service_environment_id}/config"),
            &serde_json::json!({ "content": content }),
        )
        .await?;
        Ok(())
    }

    pub async fn candidates(
        &self,
        service_environment_id: &str,
    ) -> Result<Vec<ConnectionCandidate>> {
        Ok(self
            .get::<ConnectionCandidates>(&format!(
                "/api/infra/services/{service_environment_id}/connections"
            ))
            .await?
            .candidates)
    }

    pub async fn connect(&self, consumer_id: &str, provider_id: &str) -> Result<Connection> {
        Ok(self
            .post::<ConnectionMade, _>(
                &format!("/api/infra/services/{consumer_id}/connections"),
                &serde_json::json!({ "providerId": provider_id }),
            )
            .await?
            .connection)
    }

    pub async fn capacity(&self) -> Result<Capacity> {
        self.get("/api/infra/capacity").await
    }

    pub async fn update_settings(
        &self,
        service_environment_id: &str,
        patch: &SettingsPatch,
    ) -> Result<()> {
        self.patch(
            &format!("/api/infra/services/{service_environment_id}"),
            patch,
        )
        .await
    }

    pub async fn set_encryption(
        &self,
        service_environment_id: &str,
        enabled: bool,
    ) -> Result<EncryptionOutcome> {
        self.put(
            &format!("/api/infra/services/{service_environment_id}/encryption"),
            &serde_json::json!({ "enabled": enabled }),
        )
        .await
    }

    pub async fn set_public_access(
        &self,
        service_environment_id: &str,
        public_access: bool,
    ) -> Result<ExposureOutcome> {
        self.put(
            &format!("/api/infra/services/{service_environment_id}/public-access"),
            &serde_json::json!({ "publicAccess": public_access }),
        )
        .await
    }

    pub async fn delete_service(&self, service_id: &str) -> Result<()> {
        self.delete(&format!("/api/infra/services/{service_id}"))
            .await
    }

    pub async fn rename_service(&self, service_id: &str, name: &str) -> Result<()> {
        self.patch(
            &format!("/api/infra/services/{service_id}/name"),
            &serde_json::json!({ "name": name }),
        )
        .await
    }

    pub async fn disconnect(&self, consumer_id: &str, provider_id: &str) -> Result<Vec<String>> {
        Ok(self
            .delete_for::<Disconnected>(&format!(
                "/api/infra/services/{consumer_id}/connections/{provider_id}"
            ))
            .await?
            .removed)
    }

    pub async fn recipe(&self, service_environment_id: &str) -> Result<Recipe> {
        self.get(&format!(
            "/api/infra/services/{service_environment_id}/recipe"
        ))
        .await
    }

    pub async fn edit_recipe(
        &self,
        service_environment_id: &str,
        containerfile: &str,
    ) -> Result<String> {
        Ok(self
            .put::<EditedRecipe, _>(
                &format!("/api/infra/services/{service_environment_id}/recipe"),
                &serde_json::json!({ "containerfile": containerfile }),
            )
            .await?
            .source)
    }
}