spawnbox 0.1.1

Async Rust HTTP client SDK for the self-hosted Spawnbox sandbox runtime
Documentation

spawnbox

Async Rust HTTP client SDK for the self-hosted Spawnbox sandbox runtime.

Quickstart

use std::time::Duration;
use serde_json::json;
use spawnbox::{ForwardClient, Method, SpawnboxClient};

#[tokio::main]
async fn main() -> spawnbox::Result<()> {
    let client = SpawnboxClient::new("http://127.0.0.1:8080", "sbx_api_key")?;

    client.ensure_tenant("default", None).await?;
    client
        .ensure_template(
            "python-dev",
            &json!({ "name": "python-dev", "base": "base" }),
            Duration::from_secs(2),
            60,
        )
        .await?;

    let sandbox = client
        .create_sandbox(&json!({ "template": "python-dev" }))
        .await?;
    let sandbox_id = sandbox["id"].as_str().unwrap();

    let exec_result = client
        .exec(sandbox_id, &json!({ "cmd": ["python3", "--version"] }))
        .await?;
    println!("{exec_result}");

    let forward = ForwardClient::new("http://127.0.0.1:8082", Some("fwd_key".into()))?;
    let resp = forward
        .request(Method::GET, sandbox_id, 8000, "/events")
        .header("Accept", "text/event-stream")
        .send()
        .await?;
    println!("forward status: {}", resp.status());

    client.delete_sandbox(sandbox_id).await?;
    Ok(())
}