Skip to main content

Official Python library for Trinity IoT Connect API interactions

Project description

Connect Client for Python

Python 3.12+ License: MIT

Official Python library for interacting with the Trinity IoT Connect API. This library provides a clean interface for accessing the Connect API endpoints with comprehensive error handling, type hints, and good test coverage.

Table of Contents

Installation

Using uv (recommended)

uv add trinity-connect-client

Using pip

pip install trinity-connect-client

From source

uv add git+https://github.com/trinity-telecomms/connect-py-client@v0.2.0

Quick Start

from trinity_connect_client import ConnectClient
from trinity_connect_client.exceptions import ResourceNotFoundError, UnauthorisedError

# Initialize the client
client = ConnectClient(
  api_version="v4",
  base_url="https://capi.trintel.co.za",
  token="your-service-account-token"
)

# Get a device by ID (returns dict)
try:
    device = client.devices.get(device_id=123)
    print(f"Device name: {device['name']}")
except ResourceNotFoundError:
    print("Device not found")
except UnauthorisedError:
    print("Access denied")

Using Response Models

The library provides type-safe dataclass models for API responses:

from trinity_connect_client import ConnectClient, Device, Company, Folder

client = ConnectClient(
    api_version="v4",
    base_url="https://capi.trintel.co.za",
    token="your-service-account-token"
)

# Get device as dict, then convert to model for type safety
device_dict = client.devices.get(device_id=123)
device = Device.from_dict(device_dict)

# Now you have full type hints and IDE autocomplete
print(f"Device: {device.name}")
print(f"UID: {device.uid}")
print(f"Status: {device.status}")

# Works with all response types
company_dict = client.orgs.get(company_id=1)
company = Company.from_dict(company_dict)

folders_list = client.orgs.get_folders(company_id=1)
folders = [Folder.from_dict(f) for f in folders_list]

Available Models:

  • Device - Device information
  • Company - Company/organization information
  • Folder - Folder information
  • DeviceData - Device telemetry data
  • DeviceEvent - Device events
  • DeviceCommand - Device commands

Configuration

Environment Variables

You can set your service account token via environment variables:

export CONNECT_API_TOKEN="your-service-account-token"
export CONNECT_API_BASE_URL="https://capi.trintel.co.za"
import os
from trinity_connect_client import ConnectClient

client = ConnectClient(
    api_version="v4",
    base_url=os.getenv("CONNECT_API_BASE_URL"),
    token=os.getenv("CONNECT_API_TOKEN")
)

Migration from v0.1.x to v0.2.0

Version 0.2.0 introduces breaking changes to authentication:

What Changed

  • Credentials-based authentication (email/password) has been removed
  • Service account token authentication is now required
  • Token caching logic has been removed (tokens are long-lived)
  • The auth module and login endpoint are no longer available

Upgrading

Before (v0.1.x):

client = ConnectClient(
    base_url="https://capi.trintel.co.za",
    credentials={
        "email": "user@example.com",
        "password": "password"
    },
    cache=cache_instance  # Optional
)

After (v0.2.0):

client = ConnectClient(
    base_url="https://capi.trintel.co.za",
    token="your-service-account-token"
)

How to get a service account token: Contact your Trinity IoT administrator to generate a service account token for your application.

Error Handling

The library raises specific exceptions for different error conditions:

from trinity_connect_client.exceptions import (
    ResourceNotFoundError,
    UnauthorisedError,
    ConnectAPIError
)

try:
    device = client.devices.get(device_id=123)
except ResourceNotFoundError:
    print("Device not found (404)")
except UnauthorisedError:
    print("Authentication failed (401)")
except PermissionError:
    print("Access forbidden (403)")
except ConnectAPIError as e:
    print(f"API error: {e}")
except ValueError as e:
    print(f"Invalid input: {e}")

Development

Setting up Development Environment

# Clone the repository
git clone https://github.com/trinity-telecomms/connect-py-client.git
cd connect-py-client

# Install dependencies with uv
uv sync

# Run tests
uv run pytest

# Run linting
uv run ruff check .

Running Tests

# Run all tests
uv run pytest

# Run with coverage
uv run pytest --cov=connect_client

# Run specific test file
uv run pytest tests/modules/devices/test_devices_api.py

Building the Package

This project uses the uv_build backend for building distributions:

# Build both wheel and source distribution
uv build

# Build only wheel
uv build --wheel

# Build only source distribution
uv build --sdist

# Build to a specific directory
uv build --out-dir dist/

The built distributions will be available in the dist/ directory.

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Add tests for new functionality
  5. Run the test suite (uv run pytest)
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

License

This project is licensed under the MIT Licence - see the LICENCE file for details.

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

trinity_connect_client-0.2.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

trinity_connect_client-0.2.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file trinity_connect_client-0.2.0.tar.gz.

File metadata

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

File hashes

Hashes for trinity_connect_client-0.2.0.tar.gz
Algorithm Hash digest
SHA256 48db53e8f16587844ba5269a0f098749d63ffd1ec1db723ce614e32f69f8213a
MD5 0f292886dab16ac0bcf1857ffb6a5033
BLAKE2b-256 92b2dd2d86cb467f3905dfb7a3f196dd39b96e02cc977ff5e6931f56645ba56f

See more details on using hashes here.

Provenance

The following attestation bundles were made for trinity_connect_client-0.2.0.tar.gz:

Publisher: release.yml on trinity-telecomms/connect-py-client

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

File details

Details for the file trinity_connect_client-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for trinity_connect_client-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b5e51009a79ab9fa3f8483f8fa7e0e9a94ab4f3b206ff3672e3c2f40b97d995c
MD5 79ca90a416c7e7a08049784415ffd0dd
BLAKE2b-256 93bdf6e11244b37300da4399802db89a34fe348d4693d0c3a968f6bb69c2c532

See more details on using hashes here.

Provenance

The following attestation bundles were made for trinity_connect_client-0.2.0-py3-none-any.whl:

Publisher: release.yml on trinity-telecomms/connect-py-client

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