API Reference¶
Complete reference for axium.core.api - the high-level Python API for spokes and gears.
Use API over IPC
Spokes and gears should always use this API instead of calling IPC commands directly. The API provides:
- Error handling - Graceful failures with logging
- Type safety - Proper type hints and validation
- Better ergonomics - Simpler function signatures
- Permission checks - Automatic validation for gears
- Future compatibility - IPC protocol changes won't break your code
Quick Start¶
from axium.core import api
# Environment management
env = api.get_active_env()
api.set_active_env("prod")
# Configuration
config = api.load_config("myspoke", "config.yaml")
# Notifications
api.send_notification("myspoke", "Alert", "Something happened", level="warning")
# Daemon control
api.reload_daemon()
status = api.daemon_status()
API vs IPC¶
| Use Case | Recommended Approach |
|---|---|
| Spokes | Use api.* functions exclusively |
| Gears | Use api.* functions (permission-checked) |
| Core daemon | Direct IPC for internal operations |
| CLI commands | Use api.* for most operations |
| Testing | Mock api functions, not IPC |
See IPC Commands Reference for low-level protocol details.
Core Functions¶
Axium Core API - High-level interface for spokes.
This module provides a simplified, public API for spokes to interact with the Axium daemon, environment state, configuration, and other core services.
Spokes should import from this module instead of using axium.core.ipc directly. All functions handle errors gracefully and return None on failure.
Example
from axium.core import api
# Get current environment
env = api.get_active_env()
# Load spoke config
config = api.load_config("myspoke", "myspoke.yaml")
# Update HUD
api.update_hud_segment("myspoke", "[myspoke:OK]")
# Send notification
api.send_notification("myspoke", "Alert", "Something happened")
# Execute command via daemon
api.daemon_exec("myspoke", "some-command")
get_active_env()
¶
Get the name of the currently active environment.
Returns:
| Type | Description |
|---|---|
str | None
|
Active environment name, or None if not set or unreachable. |
Source code in axium/core/api.py
get_env_data(name)
¶
Get configuration data for a specific environment.
Returns all configuration for the named environment, including variables, inventory paths, and other environment-specific settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Environment name to query |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
Environment configuration dict, or None if environment doesn't exist |
Example
Source code in axium/core/api.py
set_active_env(name, pane_id=None)
¶
Set the active environment (global or pane-specific).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Environment name to activate |
required |
pane_id
|
str | None
|
Optional tmux pane ID for pane-specific environment |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False otherwise |
Source code in axium/core/api.py
get_pane_env(pane_id)
¶
Get environment for specific tmux pane.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pane_id
|
str
|
Tmux pane ID |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Environment name or None if not set |
Source code in axium/core/api.py
clear_pane_env(pane_id)
¶
Clear pane-specific environment (falls back to global).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pane_id
|
str
|
Tmux pane ID |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False otherwise |
Source code in axium/core/api.py
list_environments()
¶
List all defined environments.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of environment names (empty list on error) |
Source code in axium/core/api.py
get_state()
¶
Get current daemon state.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
State dictionary containing active_env, started time, etc. |
dict[str, Any] | None
|
Returns None if daemon unreachable. |
Source code in axium/core/api.py
reload_spokes()
¶
Reload all spokes from disk.
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False otherwise |
Source code in axium/core/api.py
reload_daemon()
¶
Reload daemon configuration from disk.
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False otherwise |
Source code in axium/core/api.py
Notifications & HUD¶
Axium Core API - High-level interface for spokes.
This module provides a simplified, public API for spokes to interact with the Axium daemon, environment state, configuration, and other core services.
Spokes should import from this module instead of using axium.core.ipc directly. All functions handle errors gracefully and return None on failure.
Example
from axium.core import api
# Get current environment
env = api.get_active_env()
# Load spoke config
config = api.load_config("myspoke", "myspoke.yaml")
# Update HUD
api.update_hud_segment("myspoke", "[myspoke:OK]")
# Send notification
api.send_notification("myspoke", "Alert", "Something happened")
# Execute command via daemon
api.daemon_exec("myspoke", "some-command")
send_notification(spoke, title, body, level='info')
¶
Send notification via daemon.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spoke
|
str
|
Spoke name |
required |
title
|
str
|
Notification title |
required |
body
|
str
|
Notification body |
required |
level
|
str
|
Notification level (info, warning, error) |
'info'
|
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False otherwise |
Source code in axium/core/api.py
show_toast(title, body)
¶
Show a simple toast notification (info level).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Notification title |
required |
body
|
str
|
Notification body |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False otherwise |
Source code in axium/core/api.py
drain_notifications()
¶
Get and clear all queued notifications from daemon.
This drains the notification queue - notifications are removed after reading.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of notification dicts, each containing: |
list[dict[str, Any]]
|
|
list[dict[str, Any]]
|
|
list[dict[str, Any]]
|
|
list[dict[str, Any]]
|
|
list[dict[str, Any]]
|
|
Example
Source code in axium/core/api.py
clear_notifications()
¶
Clear all queued notifications without reading them.
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False otherwise |
Source code in axium/core/api.py
hud_get(pane_id=None)
¶
Get HUD status line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pane_id
|
str | None
|
Optional tmux pane ID for pane-specific HUD |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
HUD status line string or None on error |
Source code in axium/core/api.py
Daemon Control¶
Axium Core API - High-level interface for spokes.
This module provides a simplified, public API for spokes to interact with the Axium daemon, environment state, configuration, and other core services.
Spokes should import from this module instead of using axium.core.ipc directly. All functions handle errors gracefully and return None on failure.
Example
from axium.core import api
# Get current environment
env = api.get_active_env()
# Load spoke config
config = api.load_config("myspoke", "myspoke.yaml")
# Update HUD
api.update_hud_segment("myspoke", "[myspoke:OK]")
# Send notification
api.send_notification("myspoke", "Alert", "Something happened")
# Execute command via daemon
api.daemon_exec("myspoke", "some-command")
daemon_exec(spoke, command, mode='background')
¶
Execute command via daemon.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spoke
|
str
|
Spoke name requesting execution |
required |
command
|
str
|
Shell command to execute |
required |
mode
|
str
|
Execution mode (background, foreground) |
'background'
|
Returns:
| Type | Description |
|---|---|
bool
|
True if request accepted, False otherwise |
Source code in axium/core/api.py
daemon_status()
¶
Get daemon status information.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
Status dictionary or None if unreachable |
Source code in axium/core/api.py
daemon_logs(tail=False, lines=100)
¶
Get daemon logs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tail
|
bool
|
If True, follow logs (streaming) |
False
|
lines
|
int
|
Number of lines to return |
100
|
Returns:
| Type | Description |
|---|---|
str | None
|
Log content as string, or None if unreachable |
Source code in axium/core/api.py
daemon_start(debug=False)
¶
Start the Axium daemon.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
debug
|
bool
|
If True, run daemon in debug mode |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False otherwise |
Source code in axium/core/api.py
daemon_stop(force=True)
¶
Stop the daemon gracefully (or forcefully if needed).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
force
|
bool
|
If True and graceful stop fails, kill daemon process |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False otherwise |
Source code in axium/core/api.py
Tmux Integration (Gears Only)¶
Permission Required
These functions require explicit permissions in your gear's manifest.yaml:
Axium Core API - High-level interface for spokes.
This module provides a simplified, public API for spokes to interact with the Axium daemon, environment state, configuration, and other core services.
Spokes should import from this module instead of using axium.core.ipc directly. All functions handle errors gracefully and return None on failure.
Example
from axium.core import api
# Get current environment
env = api.get_active_env()
# Load spoke config
config = api.load_config("myspoke", "myspoke.yaml")
# Update HUD
api.update_hud_segment("myspoke", "[myspoke:OK]")
# Send notification
api.send_notification("myspoke", "Alert", "Something happened")
# Execute command via daemon
api.daemon_exec("myspoke", "some-command")
tmux_split_run(spoke, command, height=20)
¶
Create tmux split pane and run command.
Requires 'tmux_split_run' in spoke/gear's ipc permissions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spoke
|
str
|
Spoke/gear name (for permission check) |
required |
command
|
str
|
Shell command to execute in split |
required |
height
|
int
|
Split height as percentage (default 20%, range 1-99) |
20
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Response dict with: |
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
Example
Note
Command runs with shell=True in the split pane. Returns immediately (does not wait for command completion).
Source code in axium/core/api.py
tmux_send_keys(spoke, pane_id, keys)
¶
Send keys to specific tmux pane.
Requires 'tmux_send_keys' in spoke/gear's ipc permissions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spoke
|
str
|
Spoke/gear name (for permission check) |
required |
pane_id
|
str
|
Target pane ID (e.g., "%42") |
required |
keys
|
str
|
Keys to send to pane |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False otherwise |
Source code in axium/core/api.py
tmux_capture_pane(spoke, pane_id)
¶
Capture visible contents of tmux pane.
Requires 'tmux_capture_pane' in spoke/gear's ipc permissions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spoke
|
str
|
Spoke/gear name (for permission check) |
required |
pane_id
|
str
|
Target pane ID (e.g., "%42") |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Pane contents as string, or None if failed |
Example
Source code in axium/core/api.py
File Operations (Gears Only)¶
Permission Required
These functions require explicit file path permissions in your gear's manifest.yaml:
Axium Core API - High-level interface for spokes.
This module provides a simplified, public API for spokes to interact with the Axium daemon, environment state, configuration, and other core services.
Spokes should import from this module instead of using axium.core.ipc directly. All functions handle errors gracefully and return None on failure.
Example
from axium.core import api
# Get current environment
env = api.get_active_env()
# Load spoke config
config = api.load_config("myspoke", "myspoke.yaml")
# Update HUD
api.update_hud_segment("myspoke", "[myspoke:OK]")
# Send notification
api.send_notification("myspoke", "Alert", "Something happened")
# Execute command via daemon
api.daemon_exec("myspoke", "some-command")
read_file(spoke, path)
¶
Read file contents (permission checked).
Daemon checks path against spoke/gear's fs_read glob patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spoke
|
str
|
Spoke/gear name (for permission check) |
required |
path
|
str
|
File path to read |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
File contents as string, or None if denied/failed |
Example
Note
Max file size: 10MB (larger files return error). Tilde (~) expansion performed automatically.
Source code in axium/core/api.py
write_file(spoke, path, content)
¶
Write file contents (permission checked).
Daemon checks path against spoke/gear's fs_write glob patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spoke
|
str
|
Spoke/gear name (for permission check) |
required |
path
|
str
|
File path to write |
required |
content
|
str
|
Content to write |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False if denied/failed |
Example
Note
Parent directories created automatically if they don't exist. Existing files are overwritten. Tilde (~) expansion performed automatically.
Source code in axium/core/api.py
write_log(name, message, level='info')
¶
Write to gear/spoke log file at ~/.config/axium/logs/
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Gear/spoke name |
required |
message
|
str
|
Log message |
required |
level
|
str
|
Log level (debug, info, warning, error) |
'info'
|
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False otherwise |
Example
Note
Log files automatically created in ~/.config/axium/logs/. Logs are timestamped and include level indicator.
Source code in axium/core/api.py
Prefix System¶
Axium Core API - High-level interface for spokes.
This module provides a simplified, public API for spokes to interact with the Axium daemon, environment state, configuration, and other core services.
Spokes should import from this module instead of using axium.core.ipc directly. All functions handle errors gracefully and return None on failure.
Example
from axium.core import api
# Get current environment
env = api.get_active_env()
# Load spoke config
config = api.load_config("myspoke", "myspoke.yaml")
# Update HUD
api.update_hud_segment("myspoke", "[myspoke:OK]")
# Send notification
api.send_notification("myspoke", "Alert", "Something happened")
# Execute command via daemon
api.daemon_exec("myspoke", "some-command")
register_prefix(spoke, command, wrapper)
¶
Register command prefix rule programmatically.
Allows gears/spokes to intercept shell commands and wrap them. Includes conflict detection - returns False if command already registered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spoke
|
str
|
Spoke/gear name (owner of this rule) |
required |
command
|
str
|
Command to intercept (e.g., "ansible-playbook") |
required |
wrapper
|
str
|
Replacement command (e.g., "axium ansible-run") |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if registered, False if conflict detected |
Example
Note
Conflict occurs when another spoke/gear already registered same command. Use this during gear's register() function. Rules are automatically unregistered when gear is unloaded.
Source code in axium/core/api.py
Configuration¶
Axium Core API - High-level interface for spokes.
This module provides a simplified, public API for spokes to interact with the Axium daemon, environment state, configuration, and other core services.
Spokes should import from this module instead of using axium.core.ipc directly. All functions handle errors gracefully and return None on failure.
Example
from axium.core import api
# Get current environment
env = api.get_active_env()
# Load spoke config
config = api.load_config("myspoke", "myspoke.yaml")
# Update HUD
api.update_hud_segment("myspoke", "[myspoke:OK]")
# Send notification
api.send_notification("myspoke", "Alert", "Something happened")
# Execute command via daemon
api.daemon_exec("myspoke", "some-command")
load_config(name, filename, env_aware=True, component_type='spoke')
¶
Load spoke or gear configuration.
Wraps axium.core.config.load_spoke_config / load_gear_config with simplified interface.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Component name (spoke or gear) |
required |
filename
|
str
|
Config filename (e.g., "myspoke.yaml", "ansible.yaml") |
required |
env_aware
|
bool
|
Whether to apply environment-specific overrides |
True
|
component_type
|
str
|
"spoke" or "gear" (default: "spoke") |
'spoke'
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Configuration dictionary (empty dict on error) |
Example
Source code in axium/core/api.py
get_permissions()
¶
Get current daemon permissions.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
Permissions dictionary or None if unreachable |
Source code in axium/core/api.py
check_permission(spoke, permission)
¶
Check if spoke has specific permission.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spoke
|
str
|
Spoke name |
required |
permission
|
str
|
Permission type (exec, network, etc.) |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if permission granted, False otherwise |
Source code in axium/core/api.py
config_show()
¶
Get current configuration as YAML string.
Returns:
| Type | Description |
|---|---|
str | None
|
Configuration YAML or None on error |
Source code in axium/core/api.py
config_edit()
¶
Open configuration in editor.
Returns:
| Type | Description |
|---|---|
bool
|
True if editor launched successfully, False otherwise |
Source code in axium/core/api.py
Spoke Management¶
Axium Core API - High-level interface for spokes.
This module provides a simplified, public API for spokes to interact with the Axium daemon, environment state, configuration, and other core services.
Spokes should import from this module instead of using axium.core.ipc directly. All functions handle errors gracefully and return None on failure.
Example
from axium.core import api
# Get current environment
env = api.get_active_env()
# Load spoke config
config = api.load_config("myspoke", "myspoke.yaml")
# Update HUD
api.update_hud_segment("myspoke", "[myspoke:OK]")
# Send notification
api.send_notification("myspoke", "Alert", "Something happened")
# Execute command via daemon
api.daemon_exec("myspoke", "some-command")
list_spokes()
¶
List all installed spokes.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of spoke names (empty list on error) |
Source code in axium/core/api.py
install_spoke(name)
¶
Install a spoke.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Spoke name |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False otherwise |
Note
Stub for future implementation
Source code in axium/core/api.py
validate_spoke(name)
¶
Validate spoke schema and structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Spoke name |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if valid, False otherwise |
Note
Stub for future implementation
Source code in axium/core/api.py
list_commands(sort_by='grouped')
¶
List all registered commands with full metadata.
Returns all commands from the command registry, including core commands and spoke-registered commands. Each command includes name, summary, description, category, source, and group information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sort_by
|
str
|
Sort method - "alpha" (alphabetical), "grouped" (source grouped), or "usage" (future: by usage frequency). Default: "grouped" |
'grouped'
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of command dictionaries with complete metadata, or empty list on error |
Example
Note
- Used by palette for command discovery
- Sorted by category by default (core first, then spokes)
- Safe to call even if registry is empty (returns [])
Source code in axium/core/api.py
spoke_new(name, prefix=None, check_type=None, check_command=None)
¶
Create new spoke from template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Spoke name |
required |
prefix
|
str | None
|
Command prefix (optional) |
None
|
check_type
|
str | None
|
Check type (command, mtime, etc.) |
None
|
check_command
|
str | None
|
Check command if check_type is 'command' |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
Path to created spoke directory, or None on error |
Example
Source code in axium/core/api.py
spoke_install_from_path(path)
¶
Install spoke from local path or git URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Local directory path or git URL |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
Spoke metadata dict or None on error |
Example
Source code in axium/core/api.py
spoke_reload_all()
¶
Reload all spokes from disk.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of reloaded spoke names (empty list on error) |
Source code in axium/core/api.py
spoke_reload_one(name)
¶
Reload specific spoke from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Spoke name |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False otherwise |
Source code in axium/core/api.py
Command Execution¶
Axium Core API - High-level interface for spokes.
This module provides a simplified, public API for spokes to interact with the Axium daemon, environment state, configuration, and other core services.
Spokes should import from this module instead of using axium.core.ipc directly. All functions handle errors gracefully and return None on failure.
Example
from axium.core import api
# Get current environment
env = api.get_active_env()
# Load spoke config
config = api.load_config("myspoke", "myspoke.yaml")
# Update HUD
api.update_hud_segment("myspoke", "[myspoke:OK]")
# Send notification
api.send_notification("myspoke", "Alert", "Something happened")
# Execute command via daemon
api.daemon_exec("myspoke", "some-command")
run_command(command, args)
¶
Execute prefixed command via Axium run system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
Command name (e.g., "aws", "terraform") |
required |
args
|
list[str]
|
Command arguments |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if command executed successfully, False otherwise |
Source code in axium/core/api.py
notify_send_cli(spoke, title, body, level='info')
¶
Send notification via CLI (not from spoke).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spoke
|
str
|
Spoke name (for categorization) |
required |
title
|
str
|
Notification title |
required |
body
|
str
|
Notification body |
required |
level
|
str
|
Notification level (info, warning, error) |
'info'
|
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False otherwise |
Source code in axium/core/api.py
notify_drain()
¶
Drain all pending notifications.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of notification dictionaries (empty list on error) |
Example
Source code in axium/core/api.py
Usage Examples¶
Environment-Aware Configuration¶
from axium.core import api
def register(app, events):
"""Spoke registration with environment-aware config."""
@app.command("mycommand")
def my_command():
# Get current environment
env = api.get_active_env()
# Load config with env-specific overrides
config = api.load_config("myspoke", "config.yaml", env_aware=True)
# Access environment-specific settings
endpoint = config.get("endpoint")
timeout = config.get("timeout", 30)
print(f"Running in {env} with endpoint: {endpoint}")
Notification Patterns¶
from axium.core import api
# Simple info notification
api.show_toast("Success", "Operation completed")
# Categorized notification with level
api.send_notification(
spoke="creds",
title="Credentials Expired",
body="Run 'axium creds-refresh' to renew",
level="warning"
)
# Error notification
api.send_notification(
spoke="deploy",
title="Deployment Failed",
body=f"Error: {error_message}",
level="error"
)
Gear with Permissions¶
# manifest.yaml
permissions:
exec: true
notify: true
ipc:
- tmux_split_run
fs_write:
- ~/.my-gear/logs/**
# main.py
from axium.core import api
def register(app, events):
@app.command("my-gear-run")
def run_command():
# Execute command (requires exec permission)
api.daemon_exec("my-gear", "long-running-task")
# Create split pane (requires tmux_split_run permission)
result = api.tmux_split_run(
spoke="my-gear",
command="tail -f /var/log/app.log",
height=25
)
if result["ok"]:
pane_id = result["pane_id"]
# Write log (requires fs_write permission)
api.write_log(
"my-gear",
f"Created monitoring pane: {pane_id}",
level="info"
)
# Notify user (requires notify permission)
api.send_notification(
"my-gear",
"Monitoring Started",
f"Logs visible in pane {pane_id}"
)
Event Handlers with State Updates¶
from axium.core import api
def register(app, events):
def on_env_change(new_env: str, old_env: str, **kwargs):
"""React to environment changes."""
# Load new environment config
config = api.load_config("myspoke", "config.yaml", env_aware=True)
# Validate credentials for new environment
is_valid = check_credentials(new_env, config)
if not is_valid:
api.send_notification(
"myspoke",
"Credentials Required",
f"Please authenticate for {new_env}",
level="warning"
)
events.on("env_change", on_env_change)
Error Handling¶
All API functions handle errors gracefully and return sensible defaults:
# Returns None on failure (never raises)
env = api.get_active_env()
if env is None:
print("Daemon not reachable or env not set")
# Returns False on failure
success = api.set_active_env("prod")
if not success:
print("Failed to set environment")
# Returns empty dict/list on failure
config = api.load_config("myspoke", "config.yaml") # {} on error
envs = api.list_environments() # [] on error
Check logs for detailed error information:
See Also¶
- IPC Commands Reference - Low-level IPC protocol
- Events Reference - Event system documentation
- Gears Guide - Gear permissions and development
- Spokes Guide - Spoke development guide