Skip to main content

Carthooks Python SDK with Watcher for real-time data monitoring

Project description

Carthooks Python SDK

Carthooks Python SDK provides comprehensive tools for interacting with the Carthooks platform, including basic HTTP API client functionality and advanced real-time data monitoring capabilities.

Version 0.2.0 - Integrated Watcher

Major Changes

  • Integrated Watcher: Combined the former cybersailor package into carthooks as the watcher module
  • Unified SDK: Single package for both basic API operations and real-time data monitoring
  • Improved Architecture: Clean separation between HTTP client (Client) and data monitoring (Watcher)

Features

  • HTTP API Client: Full-featured client for Carthooks REST API

    • HTTP/2 support with connection pooling
    • DNS caching for improved performance
    • IPv6 support (configurable)
    • Comprehensive CRUD operations
  • Real-time Data Watcher: Advanced monitoring capabilities

    • Real-time data processing via Amazon SQS
    • Automatic monitoring task renewal
    • Single-threaded architecture for simplified execution
    • Flexible message acknowledgment modes

Installation

pip install carthooks

Basic API Usage

from carthooks import Client

# Initialize client
client = Client()
client.setAccessToken("your_token")

# Get items from a collection
result = client.getItems(app_id=123456, collection_id=789012)
if result.success:
    print(f"Found {len(result.data)} items")

# Create a new item
result = client.createItem(
    app_id=123456,
    collection_id=789012,
    data={"title": "New Item", "description": "Item description"}
)

Real-time Data Monitoring Usage

from carthooks import Watcher

def handler(ctx, message):
    print(f"Received: {message.item_id}")
    print(f"Event: {message.event_type}")
    print(f"Data: {message.fields}")
    return True

# Initialize watcher
watcher = Watcher(token="your_token")

# Subscribe to data changes
watcher.subscribe(
    handler=handler,
    app_id=123456,
    collection_id=789012,
    sqs_queue_url="https://sqs.region.amazonaws.com/account/queue-name",
    auto_ack=True  # Automatically acknowledge processed messages
)

# Start monitoring
watcher.run()

Manual Message Acknowledgment

def manual_handler(message):
    try:
        # Process message
        print(f"Processing: {message.item_id}")

        # Manually acknowledge on success
        message.ack()
    except Exception as e:
        # Reject message on failure (will be retried)
        message.nack(delay_seconds=30)

watcher.subscribe(
    handler=manual_handler,
    app_id=123456,
    collection_id=789012,
    sqs_queue_url="your_sqs_url",
    auto_ack=False  # Manual acknowledgment mode
)

Advanced Configuration

# Configure HTTP client
client = Client(
    timeout=60.0,
    max_connections=200,
    dns_cache_ttl=600,
    enable_ipv6=True
)

# Configure watcher with filters
watcher.subscribe(
    handler=handler,
    app_id=123456,
    collection_id=789012,
    sqs_queue_url="your_sqs_url",
    filter={"f_1009": {"$eq": 1}},  # Only monitor items where field f_1009 equals 1
    watch_name="custom-watch-name",
    age=86400,  # Monitor for 24 hours
    renewal_interval=1800,  # Renew every 30 minutes
    max_messages=5,  # Process up to 5 messages per batch
    visibility_timeout=300  # 5-minute visibility timeout
)

Environment Variables

# API Configuration
CARTHOOKS_API_URL=https://api.carthooks.com
CARTHOOKS_TIMEOUT=30.0
CARTHOOKS_MAX_CONNECTIONS=100

# HTTP/2 and DNS Configuration
CARTHOOKS_HTTP2_DISABLED=false
CARTHOOKS_DNS_CACHE_DISABLE=false
CARTHOOKS_DNS_CACHE_TTL=300
CARTHOOKS_ENABLE_IPV6=false

# AWS Configuration (for Watcher)
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
AWS_REGION=ap-southeast-1

Migration from cybersailor

If you were using the cybersailor package, migration is straightforward:

# Old cybersailor usage
from cybersailor import Sailor
sailor = Sailor(token="your_token")

# New carthooks usage
from carthooks import Watcher
watcher = Watcher(token="your_token")

The API is identical except for the class name change from Sailor to Watcher.

License

MIT License

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

carthooks-0.2.2.tar.gz (23.7 kB view details)

Uploaded Source

Built Distribution

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

carthooks-0.2.2-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file carthooks-0.2.2.tar.gz.

File metadata

  • Download URL: carthooks-0.2.2.tar.gz
  • Upload date:
  • Size: 23.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for carthooks-0.2.2.tar.gz
Algorithm Hash digest
SHA256 6011981513149f1dcfd385d3fc188b7c3b583ec6b8daa50c19ebf76bff00f447
MD5 010d6b65eeba9dfda603a88bec2be0aa
BLAKE2b-256 156cb58b65dc0c764d17e6ddf7e5721d34e78965bc334157632d76d119a88b11

See more details on using hashes here.

File details

Details for the file carthooks-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: carthooks-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for carthooks-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e5866430494f7ad6d179fbe3254fc68582b287891da05cb50d7eae6c2280c170
MD5 b564fc15554946647033807246a72d0a
BLAKE2b-256 1c458050481728327fc39a44c241358b36c8ed6368b1c65057153f5548a905fc

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