Rust SDK Configuration
Advanced configuration options for the Tenzro Platform Rust SDK.
Environment Variables
The SDK can read configuration from environment variables:
| Variable | Description | Default |
|---|---|---|
TENZRO_API_KEY | API key for authentication | Required |
TENZRO_TENANT_ID | Tenant identifier | Required |
TENZRO_BASE_URL | API base URL | https://api.platform.tenzro.com |
TENZRO_TIMEOUT_SECS | Request timeout in seconds | 30 |
Builder Pattern
use tenzro_platform::TenzroPlatform;
use std::time::Duration;
let platform = TenzroPlatform::builder()
// Required
.api_key("tz_prod_xxxxxxxxxxxx")
.tenant_id("tenant-123")
// Optional
.base_url("https://api.platform.tenzro.com")
.timeout(Duration::from_secs(60))
.max_retries(5)
.retry_delay(Duration::from_millis(500))
.build()?;From Environment
use tenzro_platform::TenzroPlatform;
// Reads TENZRO_API_KEY, TENZRO_TENANT_ID, etc.
let platform = TenzroPlatform::from_env()?;
// Or with overrides
let platform = TenzroPlatform::builder()
.from_env() // Load defaults from env
.timeout(Duration::from_secs(120)) // Override specific values
.build()?;Custom HTTP Client
Provide a custom reqwest client:
use tenzro_platform::TenzroPlatform;
use reqwest::Client;
let http_client = Client::builder()
.pool_max_idle_per_host(10)
.tcp_keepalive(Duration::from_secs(60))
.build()?;
let platform = TenzroPlatform::builder()
.api_key(api_key)
.tenant_id(tenant_id)
.http_client(http_client)
.build()?;Retry Configuration
use tenzro_platform::{TenzroPlatform, RetryPolicy};
let platform = TenzroPlatform::builder()
.api_key(api_key)
.tenant_id(tenant_id)
.retry_policy(RetryPolicy {
max_retries: 3,
initial_delay: Duration::from_millis(100),
max_delay: Duration::from_secs(10),
multiplier: 2.0,
retry_on: vec![408, 429, 500, 502, 503, 504],
})
.build()?;Custom Headers
use tenzro_platform::TenzroPlatform;
use reqwest::header::{HeaderMap, HeaderValue};
let mut headers = HeaderMap::new();
headers.insert("X-Request-Source", HeaderValue::from_static("my-app"));
headers.insert("X-Correlation-ID", HeaderValue::from_str(&correlation_id)?);
let platform = TenzroPlatform::builder()
.api_key(api_key)
.tenant_id(tenant_id)
.default_headers(headers)
.build()?;Request Middleware
use tenzro_platform::{TenzroPlatform, Middleware, Request, Response};
use async_trait::async_trait;
struct LoggingMiddleware;
#[async_trait]
impl Middleware for LoggingMiddleware {
async fn handle(&self, req: Request, next: Next<'_>) -> Result<Response> {
let start = std::time::Instant::now();
tracing::info!("Request: {} {}", req.method(), req.url());
let response = next.run(req).await?;
tracing::info!(
"Response: {} in {:?}",
response.status(),
start.elapsed()
);
Ok(response)
}
}
let platform = TenzroPlatform::builder()
.api_key(api_key)
.tenant_id(tenant_id)
.middleware(LoggingMiddleware)
.build()?;Tracing Integration
Enable the tracing feature for automatic instrumentation:
[dependencies]
tenzro-platform = { version = "0.1", features = ["tracing"] }
tracing = "0.1"
tracing-subscriber = "0.3"use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.init();
// All SDK calls are now traced
let platform = TenzroPlatform::from_env()?;
let wallets = platform.wallet().list().await?;
// Logs: tenzro_platform::wallet: list wallets, span_id=..., trace_id=...Multiple Tenants
use tenzro_platform::TenzroPlatform;
let tenant1 = TenzroPlatform::builder()
.api_key(&api_key)
.tenant_id("tenant-1")
.build()?;
let tenant2 = TenzroPlatform::builder()
.api_key(&api_key)
.tenant_id("tenant-2")
.build()?;
// Operations are isolated per tenant
let wallet1 = tenant1.wallet().create(...).await?;
let wallet2 = tenant2.wallet().create(...).await?;Connection Pooling
use tenzro_platform::TenzroPlatform;
use reqwest::Client;
// Share a connection pool across multiple clients
let http_client = Client::builder()
.pool_max_idle_per_host(20)
.pool_idle_timeout(Duration::from_secs(90))
.build()?;
let tenant1 = TenzroPlatform::builder()
.api_key(&api_key)
.tenant_id("tenant-1")
.http_client(http_client.clone())
.build()?;
let tenant2 = TenzroPlatform::builder()
.api_key(&api_key)
.tenant_id("tenant-2")
.http_client(http_client)
.build()?;TLS Configuration
// With rustls (default)
[dependencies]
tenzro-platform = { version = "0.1", features = ["rustls"] }
// With native-tls
[dependencies]
tenzro-platform = { version = "0.1", default-features = false, features = ["native-tls"] }
// Custom certificates
use tenzro_platform::TenzroPlatform;
use reqwest::Certificate;
let cert = Certificate::from_pem(&cert_bytes)?;
let client = Client::builder()
.add_root_certificate(cert)
.build()?;
let platform = TenzroPlatform::builder()
.api_key(api_key)
.tenant_id(tenant_id)
.http_client(client)
.build()?;