PyWatt SDK
Standardized SDK for building PyWatt modules in Rust.
Overview
This crate provides the core building blocks for creating PyWatt modules that integrate seamlessly with the PyWatt orchestrator. It handles:
- IPC Handshake: Standardized startup communication (
read_init,send_announce). - Logging: Consistent logging to
stderrwith secret redaction (init_module). - Secret Management: Secure retrieval and rotation handling via the integrated
secret_clientmodule (get_secret,subscribe_secret_rotations). - Typed Secrets: Type-safe secret retrieval with automatic parsing (
get_typed_secret,client.get_typed<T>, etc.) - Runtime IPC: Background task for processing orchestrator messages (
process_ipc_messages). - Core Types: Re-exports essential types from the integrated
ipc_typesmodule (OrchestratorInit,ModuleAnnounce, etc.). - (Optional) Macros: Proc macros for simplifying module definition (requires
proc_macrosfeature). - (Optional) JWT Auth: Middleware for Axum route protection (requires
jwt_authfeature).
Installation
Add this to your module's Cargo.toml:
[]
# Use the version from crates.io
= "0.2.5"
# Or use a path dependency during development
# pywatt_sdk = { path = "../pywatt_sdk" }
# Other dependencies like axum, tokio, etc.
= { = "1", = ["full"] }
= "0.7"
= "0.1"
To enable the #[pywatt_sdk::module] attribute macro, enable the proc_macros feature on your PyWatt SDK dependency:
[]
= { = "0.2.9", = ["proc_macros"] }
Quickstart Example
Here's a minimal module using the SDK with Axum:
// Use the prelude for common types and functions
use *;
use ;
use ;
use Arc;
// Define your module-specific state if needed
// Use the SDK's AppState wrapper for shared state
type SharedState = ;
async
async
async
## Python SDK
Install the Python SDK:
pip install pywatt_sdk
Or use the development requirements:
pip install -r requirements.txt
### Quickstart Example
```python
from pywatt_sdk import (
init_module,
read_init,
send_announce,
process_ipc_messages,
get_module_secret_client,
get_secret,
subscribe_secret_rotations,
AppState,
EndpointAnnounce,
serve_module,
ModuleBuilder,
)
from starlette.applications import Starlette
from starlette.routing import Route
# Define a simple ASGI app
async def health(request):
return JSONResponse({"status": "OK"})
# Entry point
if __name__ == "__main__":
def state_builder(init, secrets):
# Build user state here
return {"secrets": secrets}
def app_builder(app_state):
routes = [Route("/health", health)]
app = Starlette(routes=routes)
return app
# Define endpoints to announce
endpoints = [EndpointAnnounce(path="/health", methods=["GET"])]
# Serve module
serve_module(
secret_keys=["MY_SECRET_KEY"],
endpoints=endpoints,
state_builder=state_builder,
app_builder=app_builder,
)
Type-Safe Secrets
The SDK provides type-safe secret retrieval to make working with strongly-typed configuration values easier:
use ;
use Arc;
// Fetch and parse secrets of different types
let client = new;
// Using the typed_secret module directly
let api_key: = get_typed_secret.await?;
let timeout: = get_typed_secret.await?;
let debug_mode: = get_typed_secret.await?;
// Or using convenience methods on SecretClient
let pool_size = client..await?;
let api_url = client.get_string.await?;
let feature_enabled = client.get_bool.await?;
// Values are protected from accidental exposure in logs
println!; // Prints: "API key: Secret([REDACTED])"
// Use values only when needed
if *debug_mode.expose_secret
Database Model Manager
The PyWatt SDK includes a powerful Database Model Manager (feature-gated by database) that simplifies schema definition and management for SQL databases. It allows you to:
- Define database tables (models, columns, types, constraints, indexes) in a database-agnostic way using Rust structs.
- Generate Data Definition Language (DDL) statements (e.g.,
CREATE TABLE,CREATE INDEX) for supported database backends:- SQLite
- MySQL / MariaDB
- PostgreSQL
- Apply these models to a live database connection, creating or synchronizing tables.
- Use a command-line tool (
database-tool) for generating DDL, applying schemas, validating models, and generating Rust structs from YAML model definitions.
This toolkit promotes code reusability, enhances developer productivity by streamlining schema operations, and leverages Rust's type safety.
Example Usage (Conceptual):
use ;
use ;
async
For detailed information on defining models, generating SQL, CLI usage, and more, please refer to the Model Manager Documentation.
Core Functions & Types
(See the `