Skip to main content

Python client SDK for Calimero Network

Project description

Calimero Network Python Client SDK

The Calimero Python Client SDK helps developers interact with decentralized apps by handling server communication. It simplifies the process, letting you focus on building your app while the SDK manages the technical details.

Features

  • JSON-RPC client for sending queries and updates to Calimero nodes
  • WebSocket client for real-time subscriptions
  • Authentication handling with Ed25519 keypairs
  • Configuration management
  • Type hints and comprehensive documentation

Installation

pip install calimero-client-py==0.1.2

Quick Start

Here's a complete example of using the SDK to interact with a key-value store:

import asyncio
import toml
import os
from pathlib import Path
from calimero import JsonRpcClient, Ed25519Keypair

async def main():
    # Load keypair from config file
    config_path = os.path.expanduser("~/.calimero/node1/config.toml")
    try:
        with open(config_path, 'r') as f:
            config_data = toml.load(f)
            keypair_value = config_data.get('identity', {}).get('keypair')
            if not keypair_value:
                raise ValueError("'keypair' not found in [identity] section")
            keypair = Ed25519Keypair.from_base58(keypair_value)
    except Exception as e:
        raise ValueError(f"Failed to load keypair from config: {str(e)}")

    # Initialize the client
    client = JsonRpcClient(
        base_url="http://localhost:2428",
        endpoint="/jsonrpc",
        keypair=keypair
    )

    # Example: Set a key-value pair
    set_params = {
        "applicationId": "your_application_id",
        "method": "set",
        "argsJson": {"key": "my_key", "value": "my_value"}
    }
    set_response = await client.mutate(set_params)
    print("Set response:", set_response)

    # Example: Get a value
    get_params = {
        "applicationId": "your_application_id",
        "method": "get",
        "argsJson": {"key": "my_key"}
    }
    get_response = await client.query(get_params)
    print("Get response:", get_response)

if __name__ == "__main__":
    asyncio.run(main())

WebSocket Example

Here's how to use the WebSocket client for real-time updates:

import asyncio
import toml
import os
from pathlib import Path
from calimero import WsSubscriptionsClient, Ed25519Keypair

async def main():
    # Load keypair from config file
    config_path = os.path.expanduser("~/.calimero/node1/config.toml")
    try:
        with open(config_path, 'r') as f:
            config_data = toml.load(f)
            keypair_value = config_data.get('identity', {}).get('keypair')
            if not keypair_value:
                raise ValueError("'keypair' not found in [identity] section")
            keypair = Ed25519Keypair.from_base58(keypair_value)
    except Exception as e:
        raise ValueError(f"Failed to load keypair from config: {str(e)}")

    # Initialize the client
    client = WsSubscriptionsClient(
        base_url="http://localhost:2428",
        endpoint="/ws",
        keypair=keypair
    )

    # Connect and subscribe
    await client.connect()
    client.subscribe(["your_application_id"])

    # Add callback for received messages
    def callback(data):
        print("Received update:", data)

    client.add_callback(callback)

    # Keep the connection alive
    try:
        while True:
            await asyncio.sleep(1)
    except KeyboardInterrupt:
        await client.disconnect()

if __name__ == "__main__":
    asyncio.run(main())

Documentation

For detailed documentation, please visit our documentation site.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Development

Setting Up Development Environment

# Create and activate virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install development dependencies
pip install -e ".[test]"

Running Tests

# Run all tests
pytest

# Run tests with coverage
pytest --cov=calimero

# Run specific test file
pytest tests/test_keypair.py

Building and Publishing

# Install build tools
pip install --upgrade build twine

# Build the package
python -m build

# Publish to PyPI
twine upload dist/*

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

calimero_client_py-0.1.4.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

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

calimero_client_py-0.1.4-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file calimero_client_py-0.1.4.tar.gz.

File metadata

  • Download URL: calimero_client_py-0.1.4.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for calimero_client_py-0.1.4.tar.gz
Algorithm Hash digest
SHA256 b8100beb867c3ea5b931aa7fa694d3c42a0f9314cf526d5ef396461e961b8b61
MD5 2bcb29a9b1bcd2709e802fcc5282ecdb
BLAKE2b-256 3a05b43b77d5766b29d3b774e739c7fa3e3a2fddf6236db7c159f1bc6d052ce8

See more details on using hashes here.

File details

Details for the file calimero_client_py-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for calimero_client_py-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c4ed9a25d159ae25610d2a927921f1fdf1e2f08f4492777906a4ff68639a65e6
MD5 1f2815091cf645dbce97ceb133ba6883
BLAKE2b-256 9b3d59c31401b82525bfb144b107eaaa2410bac82e0e4bb17785b7fc10bd9cce

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