tensorlake/lib.rs
1//! # Tensorlake Cloud SDK
2//!
3//! A Rust SDK for interacting with Tensorlake Cloud APIs.
4//! This SDK provides a high-level, ergonomic interface for managing applications,
5//! functions, and execution requests in the Tensorlake Cloud platform.
6//!
7//! ## Quick Start
8//!
9//! ```rust,no_run
10//! use tensorlake::{Sdk, applications::models::ListApplicationsRequest};
11//!
12//! async fn example() -> Result<(), Box<dyn std::error::Error>> {
13//! // Create the SDK client
14//! let sdk = Sdk::new("https://api.tensorlake.ai", "your-api-key")?;
15//!
16//! // Get the applications client
17//! let apps_client = sdk.applications();
18//!
19//! // List applications in the default namespace
20//! let request = ListApplicationsRequest::builder()
21//! .namespace("default".to_string())
22//! .build()?;
23//! apps_client.list(&request).await?;
24//! Ok(())
25//! }
26//! ```
27//!
28//! ## Authentication
29//!
30//! The SDK uses Bearer token authentication, either a Personal Access Token (PAT) or a Project API key.
31//! Provide your token when creating the SDK:
32//!
33//! ```rust,no_run
34//! use tensorlake::Sdk;
35//!
36//! let sdk = Sdk::new("https://api.tensorlake.ai", "your-token").unwrap();
37//! ```
38//!
39//! ## Available Clients
40//!
41//! - [`ApplicationsClient`](applications::ApplicationsClient): Manage applications, functions, and requests
42//! - [`ImagesClient`](images::ImagesClient): Build and manage container images
43//! - [`SandboxesClient`](sandboxes::SandboxesClient): Manage sandbox lifecycle, pools, and snapshots
44//! - [`SecretsClient`](secrets::SecretsClient): Manage secrets for secure configuration
45//! - [`DocumentAiClient`](document_ai::DocumentAiClient): Manage Document AI APIs
46//!
47//! ## Error Handling
48//!
49//! The SDK provides detailed error types for different scenarios:
50//!
51//! ```rust,no_run
52//! use tensorlake::{Sdk, applications::models::ListApplicationsRequest};
53//!
54//! async fn example() -> Result<(), Box<dyn std::error::Error>> {
55//! let sdk = Sdk::new("https://api.tensorlake.ai", "your-api-key")?;
56//! let apps_client = sdk.applications();
57//!
58//! let request = ListApplicationsRequest::builder()
59//! .namespace("default".to_string())
60//! .build()?;
61//! match apps_client.list(&request).await {
62//! Ok(apps) => println!("Success: {:?}", apps.applications.len()),
63//! Err(e) => eprintln!("Error: {}", e),
64//! }
65//! Ok(())
66//! }
67//! ```
68
69pub mod applications;
70pub mod cron;
71pub mod document_ai;
72pub mod error;
73pub mod images;
74pub mod sandbox_images;
75pub mod sandbox_templates;
76pub mod sandboxes;
77pub mod secrets;
78use applications::*;
79use cron::*;
80use document_ai::*;
81use images::*;
82use sandbox_templates::*;
83use sandboxes::*;
84use secrets::*;
85
86mod client;
87pub use client::{Client, ClientBuilder, Traced};
88
89/// Derive the sandbox lifecycle base URL from the API URL.
90///
91/// Converts `api.tensorlake.*` → `sandbox.tensorlake.*`. Localhost is unchanged.
92pub fn resolve_sandbox_lifecycle_url(api_url: &str) -> String {
93 if let Ok(parsed) = url::Url::parse(api_url) {
94 let host = parsed.host_str().unwrap_or("");
95 if host == "localhost" || host == "127.0.0.1" {
96 return api_url.to_string();
97 }
98 if let Some(rest) = host.strip_prefix("api.") {
99 return format!("{}://sandbox.{}", parsed.scheme(), rest);
100 }
101 }
102 "https://sandbox.tensorlake.ai".to_string()
103}
104
105/// The main entry point for the Tensorlake Cloud SDK.
106///
107/// The `Sdk` struct provides a unified interface to all Tensorlake Cloud services.
108/// It manages authentication and provides access to various service clients.
109///
110/// ## Example
111///
112/// ```rust
113/// use tensorlake::Sdk;
114///
115/// let sdk = Sdk::new("https://api.tensorlake.ai", "your-api-key").unwrap();
116///
117/// // Access different service clients
118/// let apps_client = sdk.applications();
119/// let secrets_client = sdk.secrets();
120/// ```
121#[derive(Clone)]
122pub struct Sdk {
123 client: Client,
124 api_url: String,
125}
126
127impl Sdk {
128 /// Create a new SDK instance with the specified base URL and bearer token.
129 ///
130 /// # Arguments
131 ///
132 /// * `base_url` - The base URL of the Tensorlake Cloud API (e.g., "https://api.tensorlake.ai")
133 /// * `bearer_token` - Your API key for authentication
134 ///
135 /// # Returns
136 ///
137 /// Returns a new `Sdk` instance configured with the provided credentials.
138 ///
139 /// # Errors
140 ///
141 /// Returns an error if the HTTP client cannot be created or configured.
142 ///
143 /// # Example
144 ///
145 /// ```rust
146 /// use tensorlake::Sdk;
147 ///
148 /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
149 /// let sdk = Sdk::new("https://api.tensorlake.ai", "your-api-key").unwrap();
150 /// Ok(())
151 /// # }
152 /// ```
153 pub fn new(base_url: &str, bearer_token: &str) -> Result<Self, error::SdkError> {
154 let client = ClientBuilder::new(base_url)
155 .bearer_token(bearer_token)
156 .build()?;
157 Ok(Self {
158 client,
159 api_url: base_url.to_string(),
160 })
161 }
162
163 /// Create a new SDK instance using a client builder.
164 ///
165 /// This method allows for more flexible configuration of the SDK client,
166 /// including custom middleware, bearer tokens, and organization/project scopes.
167 ///
168 /// # Arguments
169 ///
170 /// * `builder` - A configured [`ClientBuilder`]
171 ///
172 /// # Returns
173 ///
174 /// Returns a new `Sdk` instance configured with the builder's settings.
175 ///
176 /// # Errors
177 ///
178 /// Returns an error if the HTTP client cannot be created or configured.
179 ///
180 /// # Example
181 ///
182 /// ```rust
183 /// use tensorlake::{Sdk, ClientBuilder};
184 ///
185 /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
186 /// let builder = ClientBuilder::new("https://api.tensorlake.ai")
187 /// .bearer_token("your-api-key")
188 /// .scope("org-id", "project-id");
189 /// let sdk = Sdk::with_client_builder(builder)?;
190 /// Ok(())
191 /// # }
192 /// ```
193 pub fn with_client_builder(builder: ClientBuilder) -> Result<Self, error::SdkError> {
194 let api_url = builder.base_url().to_string();
195 let client = builder.build()?;
196 Ok(Self { client, api_url })
197 }
198
199 /// Get a client for managing applications and requests.
200 ///
201 /// This method returns an [`ApplicationsClient`] that provides methods for:
202 /// - Listing, creating, updating, and deleting applications
203 /// - Invoking applications with data
204 /// - Managing execution requests
205 ///
206 /// # Returns
207 ///
208 /// Returns an [`ApplicationsClient`] instance configured with the SDK's authentication.
209 ///
210 /// # Example
211 ///
212 /// ```rust,no_run
213 /// use tensorlake::{Sdk, applications::models::ListApplicationsRequest};
214 ///
215 /// async fn example() -> Result<(), Box<dyn std::error::Error>> {
216 /// let sdk = Sdk::new("https://api.tensorlake.ai", "your-api-key")?;
217 /// let apps_client = sdk.applications();
218 ///
219 /// // Use the applications client
220 /// let request = ListApplicationsRequest::builder()
221 /// .namespace("default".to_string())
222 /// .build()?;
223 /// apps_client.list(&request).await?;
224 /// Ok(())
225 /// }
226 /// ```
227 pub fn applications(&self) -> ApplicationsClient {
228 ApplicationsClient::new(self.client.clone())
229 }
230
231 /// Get a client for building and managing container images.
232 ///
233 /// This method returns an [`ImagesClient`] that provides methods for:
234 /// - Building container images from source code and Dockerfiles
235 /// - Monitoring build progress and status
236 ///
237 /// # Returns
238 ///
239 /// Returns an [`ImagesClient`] instance configured with the SDK's authentication.
240 ///
241 /// # Example
242 ///
243 /// ```rust
244 /// use tensorlake::Sdk;
245 ///
246 /// let sdk = Sdk::new("https://api.tensorlake.ai", "your-api-key").unwrap();
247 /// let images_client = sdk.images();
248 ///
249 /// // Use the images client
250 /// // let result = images_client.build_image(request).await?;
251 /// ```
252 pub fn images(&self) -> ImagesClient {
253 ImagesClient::new(self.client.clone())
254 }
255
256 /// Get a client for managing snapshot-backed sandbox image registrations.
257 pub fn sandbox_templates(
258 &self,
259 organization_id: &str,
260 project_id: &str,
261 ) -> SandboxTemplatesClient {
262 SandboxTemplatesClient::new(
263 self.client.clone(),
264 organization_id.to_string(),
265 project_id.to_string(),
266 )
267 }
268
269 /// Get a client for managing sandbox lifecycle, pools, and snapshots.
270 ///
271 /// # Arguments
272 ///
273 /// * `namespace` - Namespace used when `use_namespaced_endpoints` is true
274 /// * `use_namespaced_endpoints` - If true, use `/v1/namespaces/{namespace}/...`
275 /// paths. If false, use cloud-style top-level paths such as `/sandboxes`.
276 pub fn sandboxes(&self, namespace: &str, use_namespaced_endpoints: bool) -> SandboxesClient {
277 let lifecycle_client = self
278 .client
279 .with_base_url(&resolve_sandbox_lifecycle_url(&self.api_url));
280 SandboxesClient::new(
281 lifecycle_client,
282 namespace.to_string(),
283 use_namespaced_endpoints,
284 )
285 }
286
287 /// Get a client for Document AI APIs.
288 pub fn document_ai(&self) -> DocumentAiClient {
289 DocumentAiClient::new(self.client.clone())
290 }
291
292 /// Get a client for managing secrets.
293 ///
294 /// This method returns a [`SecretsClient`] that provides methods for:
295 /// - Creating, updating, and deleting secrets
296 /// - Listing secrets in a project
297 /// - Retrieving individual secret details
298 ///
299 /// # Returns
300 ///
301 /// Returns a [`SecretsClient`] instance configured with the SDK's authentication.
302 ///
303 /// # Example
304 ///
305 /// ```rust,no_run
306 /// use tensorlake::{Sdk, secrets::models::ListSecretsRequest};
307 ///
308 /// async fn example() -> Result<(), Box<dyn std::error::Error>> {
309 /// let sdk = Sdk::new("https://api.tensorlake.ai", "your-api-key")?;
310 /// let secrets_client = sdk.secrets();
311 ///
312 /// // Use the secrets client
313 /// let request = ListSecretsRequest::builder()
314 /// .organization_id("org-id".to_string())
315 /// .project_id("project-id".to_string())
316 /// .build()?;
317 /// secrets_client.list(&request).await?;
318 /// Ok(())
319 /// }
320 /// ```
321 pub fn secrets(&self) -> SecretsClient {
322 SecretsClient::new(self.client.clone())
323 }
324
325 /// Get a client for managing cron schedules for applications.
326 pub fn cron(&self) -> CronClient {
327 CronClient::new(self.client.clone())
328 }
329}