Skip to main content

Definite SDK for Python

Project description

Definite SDK

A Python client for interacting with the Definite API, providing a convenient interface for key-value store operations, SQL query execution, secrets management, messaging capabilities, and DLT (Data Load Tool) integration with state persistence.

Installation

pip:

pip install definite-sdk

# For dlt support
pip install "definite-sdk[dlt]"

poetry:

poetry add definite-sdk

# For dlt support
poetry add "definite-sdk[dlt]"

Quick Start

from definite_sdk import DefiniteClient

# Initialize the client
client = DefiniteClient("YOUR_API_KEY")

Features

  • Key-Value Store: Persistent storage with version control and transactional commits
  • SQL Query Execution: Execute SQL queries against your connected database integrations
  • Cube Query Execution: Execute Cube queries for advanced analytics and data modeling
  • Secret Management: Secure storage and retrieval of application secrets
  • Integration Store: Read-only access to integration configurations
  • Messaging: Send messages through various channels (Slack, and more coming soon)
  • dlt Integration: Run dlt pipelines with automatic state persistence to Definite
  • DuckLake Integration: Easy attachment of your team's DuckLake to DuckDB connections
  • DuckDB Support: Automatic discovery and connection to team's DuckDB integrations

Basic Usage

🗄️ Key-Value Store

Store and retrieve key-value pairs that can be accessed by custom Python scripts hosted on Definite.

# Initialize or retrieve an existing key-value store
store = client.get_kv_store('test_store')
# Or use the alias method
store = client.kv_store('test_store')

# Add or update key-value pairs
store['replication_key'] = 'created_at'
store['replication_state'] = '2024-05-20'
store["key1"] = "value1"
store["key2"] = {"nested": "data"}

# Commit changes
store.commit()

# Retrieve values
print(store['replication_key'])  # 'created_at'
value = store["key1"]

🗃️ SQL Query Execution

Execute SQL queries against your connected database integrations.

# Initialize the SQL client
sql_client = client.get_sql_client()

# Execute a SQL query
result = sql_client.execute("SELECT * FROM users LIMIT 10")
print(result)

# Execute a SQL query with a specific integration
result = sql_client.execute(
    "SELECT COUNT(*) FROM orders WHERE status = 'completed'",
    integration_id="my_database_integration"
)
print(result)

📊 Cube Query Execution

Execute Cube queries for advanced analytics and data modeling.

# Prepare a Cube query
cube_query = {
    "dimensions": [],
    "measures": ["sales.total_amount"],
    "timeDimensions": [{
        "dimension": "sales.date", 
        "granularity": "month"
    }],
    "limit": 1000
}

# Execute the Cube query
result = sql_client.execute_cube_query(
    cube_query, 
    integration_id="my_cube_integration"
)
print(result)

🔒 Secret Store

Securely store and retrieve secrets for your integrations.

# Initialize the secret store
secret_store = client.get_secret_store()
# Or use the alias method
secret_store = client.secret_store()

# Set a secret
secret_store.set_secret("database_password", "my_secure_password")

# Get a secret
password = secret_store.get_secret("database_password")

# List all secrets
secrets = list(secret_store.list_secrets())

🔗 Integration Management

Manage your data integrations and connections.

# Initialize the integration store
integration_store = client.get_integration_store()
# Or use the alias method
integration_store = client.integration_store()

# List all integrations
integrations = list(integration_store.list_integrations())

# Get a specific integration
integration = integration_store.get_integration("my_integration")

💬 Messaging

Send messages through various channels using the messaging client.

# Initialize the message client
message_client = client.get_message_client()
# Or use the alias method
message_client = client.message_client()

# Send a Slack message using the unified interface
result = message_client.send_message(
    channel="slack",
    integration_id="your_slack_integration_id",
    to="C0920MVPWFN",  # Slack channel ID
    content="Hello from Definite SDK! 👋"
)

# Send a Slack message with blocks and threading
result = message_client.send_message(
    channel="slack",
    integration_id="your_slack_integration_id",
    to="C0920MVPWFN",
    content="Fallback text",
    blocks=[{
        "type": "section",
        "text": {"type": "mrkdwn", "text": "*Important Update*"}
    }],
    thread_ts="1234567890.123456"  # Reply in thread
)

# Or use the convenience method for Slack
result = message_client.send_slack_message(
    integration_id="your_slack_integration_id",
    channel_id="C0920MVPWFN",
    text="Quick message using the convenience method!",
    blocks=[{
        "type": "section",
        "text": {"type": "mrkdwn", "text": "Message with *rich* _formatting_"}
    }]
)

dlt Integration

from definite_sdk.dlt import DefiniteDLTPipeline
import dlt

# Create an incremental resource
@dlt.resource(primary_key="id", write_disposition="merge")
def orders(cursor=dlt.sources.incremental("created_at")):
    # Your data loading logic here
    pass

# Create and run pipeline
pipeline = DefiniteDLTPipeline("orders_sync")
pipeline.run(orders())

# State is automatically persisted to Definite
last_cursor = pipeline.get_state("orders")

DuckLake Integration

Attach your team's DuckLake to a DuckDB connection for seamless data access:

import duckdb
from definite_sdk import DefiniteClient

# Initialize the client
client = DefiniteClient("YOUR_API_KEY")

# Connect to DuckDB and attach DuckLake
conn = duckdb.connect()
conn.execute(client.attach_ducklake())

# Now you can use DuckLake tables
conn.execute("CREATE SCHEMA IF NOT EXISTS lake.my_schema;")
conn.execute("CREATE OR REPLACE TABLE lake.my_schema.users AS SELECT * FROM df")

# Query your DuckLake data
result = conn.sql("SELECT * FROM lake.my_schema.users").df()

You can also specify a custom alias for the attached DuckLake:

# Attach with custom alias
conn.execute(client.attach_ducklake(alias="warehouse"))

# Use the custom alias
conn.execute("SELECT * FROM warehouse.my_schema.users")

DuckDB Integration Discovery

from definite_sdk.dlt import get_duckdb_connection

# Automatically discovers DuckDB integration using DEFINITE_API_KEY env var
result = get_duckdb_connection()
if result:
    integration_id, connection = result
    # Use the DuckDB connection
    connection.execute("SELECT * FROM my_table")

Note: DuckDB integration discovery is currently limited as the API only exposes source integrations, not destination integrations. This functionality is provided for future compatibility.

State Management

# Set custom state
pipeline.set_state("custom_key", "custom_value")

# Get all state
all_state = pipeline.get_state()

# Resume from previous state
pipeline.resume_from_state()

# Reset state
pipeline.reset_state()

Authentication

To use the Definite SDK, you'll need an API key. You can find and copy your API key from the bottom left user menu in your Definite workspace.

For SQL queries, you'll also need your integration ID, which can be found in your integration's page URL.

Environment Variables

  • DEFINITE_API_KEY: Your Definite API key (auto-injected in Definite runtime)
  • DEF_API_KEY: Alternative environment variable for API key

Error Handling

The SDK uses standard HTTP status codes and raises requests.HTTPError for API errors:

import requests

try:
    result = sql_client.execute("SELECT * FROM invalid_table")
except requests.HTTPError as e:
    print(f"API Error: {e}")

Testing

# Run all tests
DEF_API_KEY=your_api_key poetry run pytest

# Run specific test file
DEF_API_KEY=your_api_key poetry run pytest tests/test_dlt.py

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.

Documentation

For more detailed documentation, visit: https://docs.definite.app/

Support

If you encounter any issues or have questions, please reach out to hello@definite.app

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

definite_sdk-0.1.20.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

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

definite_sdk-0.1.20-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file definite_sdk-0.1.20.tar.gz.

File metadata

  • Download URL: definite_sdk-0.1.20.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for definite_sdk-0.1.20.tar.gz
Algorithm Hash digest
SHA256 9e014cdfb8c2a32b7312a026d0ea5b12436c9f1659f222f136d48ecd2e024fd1
MD5 49a28f32dd09391945479c081094fc23
BLAKE2b-256 cfd1c2ab9361109822588bfdfa09851c30329aa0e8a76710aab3ea9737b63719

See more details on using hashes here.

Provenance

The following attestation bundles were made for definite_sdk-0.1.20.tar.gz:

Publisher: publish.yml on luabase/definite_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 definite_sdk-0.1.20-py3-none-any.whl.

File metadata

  • Download URL: definite_sdk-0.1.20-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for definite_sdk-0.1.20-py3-none-any.whl
Algorithm Hash digest
SHA256 665a8b90de5f03870adc8a45dca445e36f0d65927dca8d494c8dd040f2e52a4d
MD5 5c6db39ee3829953b148f385c87faf9f
BLAKE2b-256 b12afce204fa2e4f4e0d3cc2b9da310c38701a6689b1ec36d7cc60c37d51d57d

See more details on using hashes here.

Provenance

The following attestation bundles were made for definite_sdk-0.1.20-py3-none-any.whl:

Publisher: publish.yml on luabase/definite_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