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-client

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.1.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.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: haas_py-0.1.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.1.0.tar.gz
Algorithm Hash digest
SHA256 56e09c1945e4fb58d04c18fc1569cf1fb9d36c7b3593d127f686926b7682cb00
MD5 d282ce3cbd1dcddf465c32ae938f1e59
BLAKE2b-256 13dbee9ab4010299ad8a99bd4f4b36d42d228b28d031e8a1a95f541cda0b0699

See more details on using hashes here.

File details

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

File metadata

  • Download URL: haas_py-0.1.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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b82e55f2f9a781a3ab71343c95bd20ba2910d0dc68120ac8d1ca1f77ccbc98ad
MD5 a71be72d0bbc0d7756eb595bf15551a9
BLAKE2b-256 e1086f9b05f9774c76ab0829141e834ea8f3a628f7b2405070c8af41ad2b7184

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