Python SDK for the Ophelos API
Project description
Ophelos Python SDK
Python SDK for the Ophelos API - a comprehensive debt management and customer communication platform.
Installation
From PyPI (when published)
pip install ophelos-sdk
From Local Distribution
# Install from wheel (recommended)
pip install dist/ophelos_sdk-1.1.0-py3-none-any.whl
# Or install from source distribution
pip install dist/ophelos-sdk-1.1.0.tar.gz
# Or install in development mode
pip install -e .
Requirements Files
The project includes separate requirements files:
requirements.txt- Runtime dependencies only (for end users)requirements-dev.txt- Development dependencies onlypyproject.toml- Complete dependency specification (recommended)
# For end users (runtime only)
pip install -r requirements.txt
# For developers (includes testing, linting, formatting tools)
pip install -r requirements-dev.txt
# Or install everything via pyproject.toml (recommended)
pip install -e ".[dev]"
Quick Start
from ophelos_sdk import OphelosClient
from ophelos_sdk.models import Customer, Debt
# Initialize client with your credentials
client = OphelosClient(
client_id="your_client_id",
client_secret="your_client_secret",
audience="your_audience",
environment="staging", # or "production"
version="2025-04-01" # API version (default: "2025-04-01")
)
# Option 1: Create using dictionaries (traditional approach)
customer = client.customers.create({
"first_name": "John",
"last_name": "Doe",
"contact_details": [
{"type": "email", "value": "john.doe@example.com", "primary": True}
]
})
# Option 2: Create using model instances (new approach)
from ophelos_sdk.models import Customer, ContactDetail
customer_model = Customer(
id="temp_cust_123", # Temporary ID
first_name="Jane",
last_name="Smith",
contact_details=[
ContactDetail(
id="temp_cd_123",
type="email",
value="jane.smith@example.com",
primary=True
)
]
)
# Pass model directly to API - automatic conversion to API body
customer = client.customers.create(customer_model)
# Create a debt using model instance
debt_model = Debt(
id="temp_debt_123",
customer=customer.id, # Use real customer ID
organisation="org_123",
currency="GBP",
reference_code="DEBT-001",
kind="purchased"
)
debt = client.debts.create(debt_model)
# Prepare the debt for processing
client.debts.ready(debt.id)
📋 For comprehensive usage examples and advanced features, see USAGE.md
Key Features
- Complete API Coverage: All Ophelos API endpoints with comprehensive test coverage
- Type Safety: Full type hints and Pydantic models with automatic API body generation
- Model-First Approach: Create and pass Pydantic model instances directly to API calls
- Request/Response Transparency: Access complete HTTP request and response details from any model instance
- Smart Field Management: Automatic exclusion of server-generated fields and intelligent relationship handling
- Robust Error Handling: Graceful fallback for invalid API responses
- Authentication: Automatic OAuth2 token management with thread-safe token caching
- Multi-Tenant Support: Automatic tenant header injection
- Pagination: Built-in pagination support with generators for memory-efficient iteration
- Webhooks: Webhook event handling and validation with signature verification
- Concurrent Safe: Thread-safe for use with concurrent request patterns
Request/Response Transparency
Every model instance returned by the SDK includes complete HTTP request and response details:
# Get a customer
customer = client.customers.get('cust_123')
# Access request details
print(customer.request_info)
# Output: {
# 'method': 'GET',
# 'url': 'https://api.ophelos.com/customers/cust_123',
# 'headers': {'Authorization': 'Bearer ...', 'Ophelos-Version': '2025-04-01'},
# 'body': None
# }
# Access response details
print(customer.response_info)
# Output: {
# 'status_code': 200,
# 'headers': {'Content-Type': 'application/json', ...},
# 'url': 'https://api.ophelos.com/customers/cust_123'
# }
# Access raw response object for advanced use cases
response = customer.response_raw
print(f"Response took: {response.elapsed.total_seconds()} seconds")
print(f"Server: {response.headers.get('Server')}")
# Works with all operations - create, update, list, search
debts = client.debts.list(limit=10)
for debt in debts.data:
print(f"Debt {debt.id} response time: {debt.response_raw.elapsed}")
# Also works with paginated responses
print(f"List request: {debts.request_info}")
print(f"List response status: {debts.response_info['status_code']}")
This transparency enables:
- Request debugging: See exactly what was sent to the API
- Response monitoring: Track response times, status codes, headers
- Audit trails: Log complete request/response details for compliance
- Performance analysis: Monitor API response times and patterns
Model-First API Usage
The SDK supports both traditional dictionary-based API calls and a modern model-first approach:
from ophelos_sdk.models import Customer, Debt, ContactDetail
# Create models with type safety and validation
customer = Customer(
id="temp_123", # Temporary ID for creation
first_name="John",
last_name="Doe",
contact_details=[
ContactDetail(
id="temp_cd_1",
type="email",
value="john@example.com",
primary=True
)
]
)
# Pass model directly to API - automatic conversion
created_customer = client.customers.create(customer)
# Smart API body generation - automatically excludes server-generated fields
api_body = customer.to_api_body()
print(api_body)
# Output: {
# "first_name": "John",
# "last_name": "Doe",
# "contact_details": [
# {"type": "email", "value": "john@example.com", "primary": True}
# ]
# }
# Note: id, object, created_at, updated_at are automatically excluded
Authentication
Option 1: OAuth2 Client Credentials (Recommended)
# OAuth2 authentication (automatic token management)
client = OphelosClient(
client_id="your_client_id",
client_secret="your_client_secret",
audience="your_audience",
environment="production", # "development", "staging", or "production"
version="2025-04-01" # API version (default: "2025-04-01")
)
Option 2: Direct Access Token
# Direct access token authentication
client = OphelosClient(
access_token="your_access_token",
version="2025-04-01"
)
Multi-Tenant Support
# Initialize client with tenant ID
client = OphelosClient(
client_id="your_client_id",
client_secret="your_client_secret",
audience="your_audience",
environment="production",
tenant_id="tenant_123" # Automatically adds OPHELOS_TENANT_ID header
)
Contact Ophelos support to obtain credentials.
Examples
Working with Debts
from ophelos_sdk.models import Debt
# List debts with pagination
debts = client.debts.list(limit=10, expand=["customer"])
# Access request/response details
print(f"Request URL: {debts.request_info['url']}")
print(f"Response time: {debts.response_raw.elapsed.total_seconds()}s")
# Search debts
results = client.debts.search("status:paying AND updated_at>=2024-01-01")
# Get debt details
debt = client.debts.get("debt_123", expand=["customer", "payments"])
# Create using model instance
debt_model = Debt(
id="temp_debt",
customer="cust_123",
organisation="org_123",
currency="GBP",
reference_code="DEBT-001",
kind="purchased"
)
created_debt = client.debts.create(debt_model)
# Access creation request details
print(f"Created debt with request: {created_debt.request_info}")
Error Handling
from ophelos_sdk.exceptions import OphelosAPIError, AuthenticationError
try:
debt = client.debts.get("invalid_debt_id")
except OphelosAPIError as e:
print(f"API Error: {e.message} (Status: {e.status_code})")
if hasattr(e, 'response_data'):
print(f"Response: {e.response_data}")
except AuthenticationError as e:
print(f"Authentication failed: {e.message}")
Webhook Handling
from ophelos_sdk.webhooks import WebhookHandler
# Initialize webhook handler
webhook_handler = WebhookHandler("your_webhook_secret")
# Validate and parse webhook
try:
event = webhook_handler.verify_and_parse(
payload=request.body,
signature=request.headers.get("Ophelos-Signature")
)
if event.type == "debt.created":
print(f"New debt created: {event.data.id}")
except Exception as e:
print(f"Webhook validation failed: {e}")
API Resources
- Debts: Create, update, and manage debts with lifecycle operations
- Customers: Customer CRUD operations with contact detail management
- Payments: Payment processing and tracking
- Organisations: Organisation setup and configuration
- Invoices: Invoice creation and management
- Communications: Communication tracking and management
- Payment Plans: Payment plan management
- Webhooks: Webhook management and validation
Pagination
# List with automatic pagination
debts = client.debts.list(limit=50)
# Check pagination status
if debts.has_more:
print(f"Total count: {debts.total_count}")
# Navigate using cursors
next_page = client.debts.list(limit=50, after=debts.pagination['next']['after'])
# Memory-efficient iteration
for debt in client.debts.iterate(limit_per_page=100):
print(f"Processing debt: {debt.id}")
Development
# Clone and install
git clone https://github.com/ophelos/ophelos-python-sdk.git
cd ophelos-python-sdk
pip install -e ".[dev]"
# Run tests (250+ tests including 143 model tests)
pytest
# Run linting
flake8 ophelos_sdk/
mypy ophelos_sdk/
black ophelos_sdk/ --line-length 120
Support
- API Reference: https://api.ophelos.com
- Support Email: support@ophelos.com
- GitHub Issues: GitHub Issues
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ophelos_sdk-1.1.0.tar.gz.
File metadata
- Download URL: ophelos_sdk-1.1.0.tar.gz
- Upload date:
- Size: 72.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a1bb669972cc50a1cf2bea0fb7e4c6065c5f60bb30bbb67fe00fdbc217b9df3
|
|
| MD5 |
05810d64ccdb6800bf828bfd95f9fc92
|
|
| BLAKE2b-256 |
8fc649e464d2278a2bc023de68be1350260edbdca80541c66230eb55eed0eb57
|
File details
Details for the file ophelos_sdk-1.1.0-py3-none-any.whl.
File metadata
- Download URL: ophelos_sdk-1.1.0-py3-none-any.whl
- Upload date:
- Size: 39.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4aa23d38793e8a6c70a76128a78569b3fbe4eb004eeae63b1c52060f2c58f945
|
|
| MD5 |
55f83ba217aa16446feb70775c5dfeca
|
|
| BLAKE2b-256 |
8c9ea0420d05d986d46c75d59616b61fc00da992955ba54734cb963e90994fc3
|