Skip to main content

Python SDK for the HaaS (Harness as a Service) API

Project description

haas-client

Python SDK for the HaaS (Harness as a Service) API.

Spin up isolated Docker containers, run commands, and manage files — all from Python.

Installation

pip install haas-py

Quick start

import haas

client = haas.Client("https://your-haas-host", "your-api-key")

# Create an environment
env = client.create_environment(image="ubuntu:22.04")

# Run a command
result = client.exec(env.id, ["bash", "-c", "echo hello"])
print(result.stdout)   # "hello\n"
print(result.ok)       # True

# Clean up
client.destroy_environment(env.id)

Use as a context manager to close the HTTP connection automatically:

with haas.Client("https://your-haas-host", "your-api-key") as client:
    env = client.create_environment(image="python:3.12")
    result = client.exec(env.id, ["python", "-c", "print(2 + 2)"])
    print(result.stdout)  # "4\n"

API reference

Client(base_url, api_key, *, timeout=30.0)

All methods map 1-to-1 to the HaaS REST API. Errors raise typed exceptions (see Error handling).

Environments

# Create
env = client.create_environment(
    image="ubuntu:22.04",
    cpu=1.0,           # cores (default: server config)
    memory_mb=2048,    # MB (default: server config)
    disk_mb=4096,      # MB (default: server config)
    network_policy="none",   # "none" | "egress-limited" | "full"
    env_vars={"FOO": "bar"},
)

# List — scoped to your API key
envs = client.list_environments()

# Get
env = client.get_environment("env_abc123")

# Destroy
client.destroy_environment("env_abc123")

Exec

# Blocking — waits for the command to exit, collects all output
result = client.exec(
    env.id,
    ["bash", "-c", "ls -la"],
    working_dir="/tmp",       # optional
    timeout_seconds=30,       # optional
)
print(result.stdout)
print(result.stderr)
print(result.exit_code)  # "0", "1", etc.
print(result.ok)         # True if exit_code == "0"

# Streaming — yields ExecEvent objects in real time
for event in client.exec_stream(env.id, ["bash", "-c", "for i in 1 2 3; do echo $i; sleep 1; done"]):
    if event.stream == "stdout":
        print(event.data, end="", flush=True)
    elif event.stream == "exit":
        print(f"\nexited with {event.data}")

Files

# List files at a path
files = client.list_files(env.id, "/tmp")
for f in files:
    print(f.name, f.size, f.is_dir)

# Read a file (returns bytes)
data = client.read_file(env.id, "/etc/hosts")
print(data.decode())

# Write a file (str or bytes; parent dirs created automatically)
client.write_file(env.id, "/tmp/script.py", "print('hello')")

Error handling

All errors inherit from haas.HaasError and carry a status_code attribute.

Exception HTTP status
AuthenticationError 401
ForbiddenError 403 (e.g. image not on allowlist)
NotFoundError 404
ServerError 5xx
HaasError any other 4xx
try:
    env = client.get_environment("env_gone")
except haas.NotFoundError:
    print("environment does not exist")
except haas.AuthenticationError:
    print("bad API key")
except haas.HaasError as e:
    print(f"API error {e.status_code}: {e}")

Types

Type Fields
Environment id, status, spec, created_at, last_used_at, expires_at, container_id
EnvironmentSpec image, cpu, memory_mb, disk_mb, network_policy, env_vars
ExecResult stdout, stderr, exit_code, ok
ExecEvent stream ("stdout" / "stderr" / "exit"), data
FileInfo name, path, size, is_dir, mod_time

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

haas_py-0.2.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

haas_py-0.2.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file haas_py-0.2.0.tar.gz.

File metadata

  • Download URL: haas_py-0.2.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for haas_py-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3fdde969980a5ae773cec57e74f65cc00fb506ec7790d5980a5eb2cd002152a9
MD5 6a7f5f0719e1b702e89c4325c539f612
BLAKE2b-256 43d7573a30194896dca486f2a33c397bdc6730fa240b642d7074f6583a5c5cd0

See more details on using hashes here.

File details

Details for the file haas_py-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: haas_py-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for haas_py-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bca627b3ab2660de2a86facf10967fc2234c628a61374f24787f93ecbe3902cb
MD5 0c352f85dd6e304fe474c51c49c359ab
BLAKE2b-256 114879e26874523987c7a11f5d39b5bf232e870ec7b20d664449daef8682add3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page