Skip to main content

Python SDK for the Pragmatiks platform

Project description

Pragmatiks SDK

Python SDK for building providers and interacting with the Pragmatiks platform.

Installation

uv add pragmatiks-sdk

Quick Start

HTTP Clients

Interact with the Pragma API using sync or async clients:

from pragma_sdk import PragmaClient, AsyncPragmaClient, Resource

# Synchronous client
with PragmaClient() as client:
    resources = client.list_resources(provider="postgres")
    db = client.get_resource("postgres", "database", "my-db")

    resource = client.apply_resource(
        Resource(
            provider="postgres",
            resource="database",
            name="my-db",
            config={"name": "analytics", "size_gb": 100}
        )
    )

    client.delete_resource("postgres", "database", "my-db")

# Asynchronous client
async with AsyncPragmaClient() as client:
    resources = await client.list_resources(provider="postgres")
    db = await client.get_resource("postgres", "database", "my-db")
    await client.delete_resource("postgres", "database", "my-db")

Provider Authoring

Build providers that manage infrastructure resources with type-safe configuration and outputs.

Basic Provider

from pragma_sdk import Provider, Resource, Config, Outputs, Field

# Create a provider namespace
postgres = Provider(name="postgres")

# Define configuration schema
class DatabaseConfig(Config):
    db_name: Field[str]
    size_gb: Field[int] = 10
    owner: Field[str] = "postgres"

# Define output schema
class DatabaseOutputs(Outputs):
    connection_url: str
    created_at: str

# Implement resource lifecycle
@postgres.resource("database")
class Database(Resource[DatabaseConfig, DatabaseOutputs]):
    async def on_create(self) -> DatabaseOutputs:
        # Create the database
        return DatabaseOutputs(
            connection_url=f"postgres://localhost/{self.config.db_name}",
            created_at="2025-01-01T00:00:00Z"
        )

    async def on_update(self, previous_config: DatabaseConfig) -> DatabaseOutputs:
        # Update the database if config changed
        if previous_config.size_gb != self.config.size_gb:
            # Resize database
            pass
        return self.outputs  # type: ignore

    async def on_delete(self) -> None:
        # Delete the database
        pass

Resource Lifecycle

Resources follow a 5-state lifecycle:

DRAFT → PENDING (commit)
PENDING → PROCESSING (provider picks up)
PROCESSING → READY (success) | FAILED (error)
READY/FAILED → PENDING (update/retry)
READY/FAILED → DRAFT (uncommit)

Field References

Reference outputs from other resources:

from pragma_sdk import FieldReference

app_config = AppConfig(
    name="my-app",
    # Reference database URL instead of hardcoding
    database_url=FieldReference(
        provider="postgres",
        resource="database",
        name="my-db",
        field="outputs.connection_url"
    )
)

Testing Providers

Use ProviderHarness to test lifecycle methods locally without platform infrastructure:

from pragma_sdk.provider import ProviderHarness

async def test_database_creation():
    harness = ProviderHarness()

    # Test on_create
    result = await harness.invoke_create(
        Database,
        name="test-db",
        config=DatabaseConfig(db_name="test-db", size_gb=10)
    )

    assert result.success
    assert result.outputs.connection_url == "postgres://localhost/test-db"

    # Test on_update
    result = await harness.invoke_update(
        Database,
        name="test-db",
        config=DatabaseConfig(db_name="test-db", size_gb=20),
        previous_config=DatabaseConfig(db_name="test-db", size_gb=10),
        current_outputs=result.outputs
    )

    assert result.success

    # Test on_delete
    result = await harness.invoke_delete(
        Database,
        name="test-db",
        config=DatabaseConfig(db_name="test-db")
    )

    assert result.success

Authentication

Credentials are discovered automatically in this order:

  1. Explicit auth_token parameter
  2. Context-specific environment variable: PRAGMA_AUTH_TOKEN_<CONTEXT>
  3. Generic environment variable: PRAGMA_AUTH_TOKEN
  4. Credentials file: ~/.config/pragma/credentials
# Auto-discover from environment or credentials file
client = PragmaClient()

# Explicit token
client = PragmaClient(auth_token="eyJhbGc...")

# Specific context
client = PragmaClient(context="production")

# Require authentication (fail if no token found)
client = PragmaClient(context="production", require_auth=True)

Environment Variables

# Generic token (all contexts)
export PRAGMA_AUTH_TOKEN=sk_test_...

# Context-specific token
export PRAGMA_AUTH_TOKEN_PRODUCTION=sk_prod_...
export PRAGMA_CONTEXT=production

API Reference

HTTP Clients

Both PragmaClient and AsyncPragmaClient provide:

  • is_healthy() - Check API health
  • list_resources(provider, resource, tags) - List resources
  • get_resource(provider, resource, name) - Get a resource
  • apply_resource(resource) - Create or update a resource
  • delete_resource(provider, resource, name) - Delete a resource
  • register_resource(provider, resource, schema, description, tags) - Register a resource type
  • unregister_resource(provider, resource) - Unregister a resource type

Provider Classes

  • Provider(name) - Provider namespace

    • @provider.resource(name) - Decorator to register resources
  • Resource[ConfigT, OutputsT] - Base class for resources

    • on_create() - Handle resource creation
    • on_update(previous_config) - Handle resource updates
    • on_delete() - Handle resource deletion
  • Config - Base class for resource configuration

  • Outputs - Base class for resource outputs

  • Field[T] - Type alias for T | FieldReference

Testing

  • ProviderHarness - Local test harness
    • invoke_create(resource_class, name, config, tags) - Test on_create
    • invoke_update(resource_class, name, config, previous_config, current_outputs, tags) - Test on_update
    • invoke_delete(resource_class, name, config, current_outputs, tags) - Test on_delete
    • events - List of lifecycle events
    • results - List of lifecycle results
    • clear() - Clear event and result history

Development

# Run tests
pytest

# Format code
ruff format

# Lint
ruff check

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

pragmatiks_sdk-0.3.1.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

pragmatiks_sdk-0.3.1-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file pragmatiks_sdk-0.3.1.tar.gz.

File metadata

  • Download URL: pragmatiks_sdk-0.3.1.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pragmatiks_sdk-0.3.1.tar.gz
Algorithm Hash digest
SHA256 cc2af1e1b70c4d5bbc1068780cdc5b1e12751e09c252ef1657366d95f9142e50
MD5 e730278dc5214d35ba946fc0aa1daaeb
BLAKE2b-256 4dbfb680e4ab75e22ee094039023ca279e7bb816d2cf45d101bf6c2fe2b33333

See more details on using hashes here.

Provenance

The following attestation bundles were made for pragmatiks_sdk-0.3.1.tar.gz:

Publisher: publish.yaml on pragmatiks/sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pragmatiks_sdk-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: pragmatiks_sdk-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pragmatiks_sdk-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eaa90dc71cf1ce293e8d8d13df9416475c0da006624a9e9bba6af3c48e5b5393
MD5 12fa9ee77a982f5b184df2ce022e4672
BLAKE2b-256 f83c5ada0b202849b6c21c80d1595350abb15e8c0f29a9ac891f579eeac5a524

See more details on using hashes here.

Provenance

The following attestation bundles were made for pragmatiks_sdk-0.3.1-py3-none-any.whl:

Publisher: publish.yaml on pragmatiks/sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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