pub mod applications;
pub mod artifact_storage;
pub mod cron;
pub mod document_ai;
pub mod error;
pub mod http_transport;
mod image_service_builds;
pub mod images;
pub mod retry;
pub mod sandbox_images;
pub mod sandbox_templates;
pub mod sandboxes;
pub mod secrets;
pub mod sse;
use applications::*;
use artifact_storage::*;
use cron::*;
use document_ai::*;
use images::*;
use sandbox_templates::*;
use sandboxes::*;
use secrets::*;
mod client;
pub use client::{Client, ClientBuilder, Traced};
pub const SANDBOX_API_URL_ENV: &str = "TENSORLAKE_SANDBOX_API_URL";
pub fn resolve_sandbox_lifecycle_url(api_url: &str) -> String {
if let Some(url) = sandbox_api_url_override() {
return url;
}
resolve_sandbox_lifecycle_url_from_api_url(api_url)
}
pub fn sandbox_api_url_override() -> Option<String> {
let raw = std::env::var(SANDBOX_API_URL_ENV).ok()?;
normalize_sandbox_api_url(&raw)
}
fn normalize_sandbox_api_url(raw: &str) -> Option<String> {
let trimmed = raw.trim();
if trimmed.is_empty() {
return None;
}
let parsed = url::Url::parse(trimmed).ok()?;
if !matches!(parsed.scheme(), "http" | "https") || parsed.host_str().is_none() {
return None;
}
if parsed.query().is_some()
|| parsed.fragment().is_some()
|| !parsed.username().is_empty()
|| parsed.password().is_some()
{
return None;
}
Some(trimmed.trim_end_matches('/').to_string())
}
fn resolve_sandbox_lifecycle_url_from_api_url(api_url: &str) -> String {
if let Ok(mut parsed) = url::Url::parse(api_url)
&& let Some(host) = parsed.host_str()
{
if host == "localhost" || host == "127.0.0.1" {
return api_url.to_string();
}
if let Some(rest) = host.strip_prefix("api.") {
let new_host = format!("sandbox.{rest}");
if parsed.set_host(Some(&new_host)).is_ok() {
return parsed.to_string().trim_end_matches('/').to_string();
}
}
}
"https://sandbox.tensorlake.ai".to_string()
}
#[cfg(test)]
mod sandbox_lifecycle_url_tests {
use super::*;
#[test]
fn derives_sandbox_host_from_api_host() {
assert_eq!(
resolve_sandbox_lifecycle_url_from_api_url("https://api.tensorlake.ai"),
"https://sandbox.tensorlake.ai"
);
assert_eq!(
resolve_sandbox_lifecycle_url_from_api_url("https://api.tensorlake.dev/"),
"https://sandbox.tensorlake.dev"
);
assert_eq!(
resolve_sandbox_lifecycle_url_from_api_url("http://api.example.test:8443"),
"http://sandbox.example.test:8443"
);
assert_eq!(
resolve_sandbox_lifecycle_url_from_api_url("http://localhost:8900"),
"http://localhost:8900"
);
assert_eq!(
resolve_sandbox_lifecycle_url_from_api_url("https://custom.example.test"),
"https://sandbox.tensorlake.ai"
);
}
#[test]
fn override_accepts_only_absolute_http_origins() {
assert_eq!(
normalize_sandbox_api_url(" https://byoc-acme.tensorlake.ai/ "),
Some("https://byoc-acme.tensorlake.ai".to_string())
);
assert_eq!(
normalize_sandbox_api_url("http://127.0.0.1:9443"),
Some("http://127.0.0.1:9443".to_string())
);
assert_eq!(normalize_sandbox_api_url(""), None);
assert_eq!(normalize_sandbox_api_url(" "), None);
assert_eq!(normalize_sandbox_api_url("byoc-acme.tensorlake.ai"), None);
assert_eq!(
normalize_sandbox_api_url("ftp://byoc-acme.tensorlake.ai"),
None
);
}
#[test]
fn override_rejects_anything_after_the_path() {
assert_eq!(
normalize_sandbox_api_url("https://byoc-acme.tensorlake.ai?q=1"),
None
);
assert_eq!(
normalize_sandbox_api_url("https://byoc-acme.tensorlake.ai/#fragment"),
None
);
assert_eq!(
normalize_sandbox_api_url("https://user:pw@byoc-acme.tensorlake.ai"),
None
);
assert_eq!(
normalize_sandbox_api_url("https://byoc-acme.tensorlake.ai/proxy/"),
Some("https://byoc-acme.tensorlake.ai/proxy".to_string())
);
}
}
pub fn resolve_artifact_storage_url(api_url: &str) -> String {
artifact_storage::resolve_artifact_storage_url(api_url)
}
#[derive(Clone)]
pub struct Sdk {
client: Client,
api_url: String,
}
impl Sdk {
pub fn new(base_url: &str, bearer_token: &str) -> Result<Self, error::SdkError> {
let client = ClientBuilder::new(base_url)
.bearer_token(bearer_token)
.build()?;
Ok(Self {
client,
api_url: base_url.to_string(),
})
}
pub fn with_client_builder(builder: ClientBuilder) -> Result<Self, error::SdkError> {
let api_url = builder.base_url().to_string();
let client = builder.build()?;
Ok(Self { client, api_url })
}
pub fn applications(&self) -> ApplicationsClient {
ApplicationsClient::new(self.client.clone())
}
pub fn images(&self) -> ImagesClient {
ImagesClient::new(self.client.clone())
}
pub fn artifact_storage(&self) -> Result<ArtifactStorageClient, error::SdkError> {
ArtifactStorageClient::new(
self.client.clone(),
resolve_artifact_storage_url(&self.api_url),
)
}
pub fn artifact_storage_with_url(
&self,
git_base_url: &str,
) -> Result<ArtifactStorageClient, error::SdkError> {
ArtifactStorageClient::new(self.client.clone(), git_base_url)
}
#[cfg(unix)]
pub fn artifact_storage_with_unix_socket(
&self,
git_base_url: &str,
socket_path: &std::path::Path,
) -> Result<ArtifactStorageClient, error::SdkError> {
ArtifactStorageClient::new_with_unix_socket(self.client.clone(), git_base_url, socket_path)
}
pub fn sandbox_templates(
&self,
organization_id: &str,
project_id: &str,
) -> SandboxTemplatesClient {
SandboxTemplatesClient::new(
self.client.clone(),
organization_id.to_string(),
project_id.to_string(),
)
}
pub fn sandbox_templates_for_api_key(&self) -> SandboxTemplatesClient {
SandboxTemplatesClient::new_api_key_scoped(self.client.clone())
}
pub fn sandboxes(&self, namespace: &str, use_namespaced_endpoints: bool) -> SandboxesClient {
let lifecycle_client = self
.client
.with_base_url(&resolve_sandbox_lifecycle_url(&self.api_url));
SandboxesClient::new(
lifecycle_client,
namespace.to_string(),
use_namespaced_endpoints,
)
.with_log_client(self.client.clone())
}
pub fn document_ai(&self) -> DocumentAiClient {
DocumentAiClient::new(self.client.clone())
}
pub fn secrets(&self) -> SecretsClient {
SecretsClient::new(self.client.clone())
}
pub fn cron(&self) -> CronClient {
CronClient::new(self.client.clone())
}
}