Skip to main content

systemprompt_cloud/api_client/
tenant_api.rs

1//! Tenant-scoped endpoints for [`super::CloudApiClient`].
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use serde::Serialize;
7use systemprompt_identifiers::TenantId;
8use systemprompt_models::modules::ApiPaths;
9
10use super::CloudApiClient;
11use super::types::{
12    ApiResponse, DeployResponse, RegistryToken, RotateCredentialsResponse, SetSecretsRequest,
13    TenantSecrets, TenantStatus,
14};
15use crate::error::CloudResult;
16
17#[derive(Serialize)]
18struct DeployRequest {
19    image: String,
20}
21
22impl CloudApiClient {
23    pub async fn get_tenant_status(&self, tenant_id: &TenantId) -> CloudResult<TenantStatus> {
24        let response: ApiResponse<TenantStatus> =
25            self.tenant_get(&ApiPaths::tenant_status(tenant_id)).await?;
26        Ok(response.data)
27    }
28
29    pub async fn get_registry_token(&self, tenant_id: &TenantId) -> CloudResult<RegistryToken> {
30        let response: ApiResponse<RegistryToken> = self
31            .tenant_get(&ApiPaths::tenant_registry_token(tenant_id))
32            .await?;
33        Ok(response.data)
34    }
35
36    pub async fn deploy(&self, tenant_id: &TenantId, image: &str) -> CloudResult<DeployResponse> {
37        let request = DeployRequest {
38            image: image.to_owned(),
39        };
40        let response: ApiResponse<DeployResponse> = self
41            .tenant_post(&ApiPaths::tenant_deploy(tenant_id), &request)
42            .await?;
43        Ok(response.data)
44    }
45
46    pub async fn fetch_secrets(&self, secrets_url: &str) -> CloudResult<TenantSecrets> {
47        let path = secrets_url
48            .strip_prefix(&self.api_url)
49            .unwrap_or(secrets_url);
50        self.tenant_get(path).await
51    }
52
53    pub async fn delete_tenant(&self, tenant_id: &TenantId) -> CloudResult<()> {
54        self.tenant_delete(&ApiPaths::tenant(tenant_id)).await
55    }
56
57    pub async fn set_secrets(
58        &self,
59        tenant_id: &TenantId,
60        secrets: std::collections::HashMap<String, String>,
61    ) -> CloudResult<Vec<String>> {
62        let keys: Vec<String> = secrets.keys().cloned().collect();
63        let request = SetSecretsRequest { secrets };
64        self.tenant_put_no_content(&ApiPaths::tenant_secrets(tenant_id), &request)
65            .await?;
66        Ok(keys)
67    }
68
69    pub async fn rotate_credentials(
70        &self,
71        tenant_id: &TenantId,
72    ) -> CloudResult<RotateCredentialsResponse> {
73        self.tenant_post_empty(&ApiPaths::tenant_rotate_credentials(tenant_id))
74            .await
75    }
76}