A comprehensive Python SDK for interacting with Coremail XT API
Project description
Coremail Python SDK
A comprehensive Python SDK for interacting with Coremail XT API.
Table of Contents
Features
- 🔐 Secure Authentication: Token-based authentication with automatic caching (1 hour TTL)
- 📦 Comprehensive API Coverage: Full support for user, domain, and system management
- 🧪 Well Tested: Extensively tested with 100% test coverage
- 📝 Type Hints: Full type annotations for better development experience
- 📊 Rate Limiting: Built-in token caching to optimize API usage
- 🔄 Error Handling: Unified error handling with proper exceptions
Installation
pip install coremail
Quick Start
from coremail import CoremailClient
# Initialize the client with environment variables
client = CoremailClient()
# Or with explicit parameters
client = CoremailClient(
base_url="http://your-host-of-coremail:9900/apiws/v3",
app_id="your_app_id@your-domain.com",
secret="your_secret_key"
)
# Example: Request a token
token = client.requestToken()
print(f"Token: {token}")
# Example: Get user attributes
user_attrs = client.getAttrs("test_user@your-domain.com")
print(f"User attributes: {user_attrs}")
# Example: Change user password
change_result = client.changeAttrs(
"test_user@your-domain.com",
{"password": "new_secure_password"}
)
print(f"Password changed: {change_result}")
# Example: Authenticate a user
auth_result = client.authenticate("test_user@your-domain.com", "password")
print(f"Authentication result: {auth_result}")
Configuration
Environment Variables
Create a .env file in your project root:
COREMAIL_BASE_URL=http://your-host-of-coremail:9900/apiws/v3
COREMAIL_APP_ID=your_app_id@your-domain.com
COREMAIL_SECRET=your_secret_key
Manual Configuration
from coremail import CoremailClient
client = CoremailClient(
base_url="http://your-host-of-coremail:9900/apiws/v3",
app_id="your_app_id@your-domain.com",
secret="your_secret_key"
)
API Reference
CoremailClient
The client provides direct access to all Coremail API endpoints:
requestToken()- Request a new authentication token (with 1-hour cache)authenticate(user_at_domain, password)- Authenticate a usergetAttrs(user_at_domain, attrs=None)- Get user attributeschangeAttrs(user_at_domain, attrs)- Change user attributescreate(user_at_domain, attrs)- Create a new userdelete(user_at_domain)- Delete a userlist_users(domain=None, attrs=None)- List users in the system or domainlistDomains(attrs=None)- List domains in the systemgetDomainAttrs(domain_name, attrs=None)- Get domain attributeschangeDomainAttrs(domain_name, attrs)- Change domain attributesuserExist(user_at_domain)- Check if a user existssearch(user_at_domain, search_params)- Search messages for a userget_logs(log_type, start_time=None, end_time=None, limit=None)- Get system logsmanage_group(operation, group_name, user_at_domain=None)- Manage groupsget_system_config(config_type=None)- Get system configurationadmin(operation, params=None)- Perform administrative operationsrefresh_token()- Force refresh the authentication token
Examples
Basic User Management
from coremail import CoremailClient
client = CoremailClient()
# Create a new user
new_user_result = client.create(
"newuser@your-domain.com",
{
"password": "initial_password",
"quota_mb": 1024,
"user_enabled": True,
"user_name": "New User"
}
)
# Update user attributes
update_result = client.changeAttrs(
"newuser@your-domain.com",
{
"password": "new_secure_password",
"quota_mb": 2048
}
)
# Get user info
user_info = client.getAttrs("newuser@your-domain.com")
# Delete user
delete_result = client.delete("newuser@your-domain.com")
Domain Management
# List all domains
domains = client.listDomains()
# Get domain information
domain_info = client.getDomainAttrs("your-domain.com")
# Update domain settings
client.changeDomainAttrs(
"your-domain.com",
{
"quota_mb": 1024000,
"max_users": 1000,
"enabled": True
}
)
Authentication and Validation
# Check if user exists by attempting to get attributes
result = client.getAttrs("test@your-domain.com")
exists = result.get('code') == 0
# Authenticate user directly
auth_response = client.authenticate("user@domain.com", "password")
is_authenticated = auth_response.get('code') == 0
Development
Setup
# Clone the repository
git clone https://github.com/liudonghua123/coremail.git
cd coremail
# Create virtual environment
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install in development mode
uv pip install -e ".[dev]"
Running Tests
# Run all tests
pytest
# Run tests with coverage
pytest --cov=coremail
# Run specific test file
pytest tests/test_client.py
Code Quality
# Run type checking
mypy coremail
# Format code
black coremail tests
# Check linting
flake8 coremail tests
Building
# Build the package
python -m build
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for new functionality
- Run the test suite (
pytest) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
If you encounter any issues, please file them in the Issues section of the repository.
For questions and support, you can:
- Open an issue on GitHub
- Check the documentation
- Look at the examples in the
examples/directory
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 coremail-0.1.1.tar.gz.
File metadata
- Download URL: coremail-0.1.1.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3801a095ce095646467ff377815168f32cfcadb3b7293680187aebb709034166
|
|
| MD5 |
01f1704d28bd1685808be895b56450a4
|
|
| BLAKE2b-256 |
a807397b234d3cf2e689b79c353a78934a2381a757dca8c7b25461a240e2d69f
|
File details
Details for the file coremail-0.1.1-py3-none-any.whl.
File metadata
- Download URL: coremail-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2bd444c7d5e58e85542def3d6fdff4d29b3073e30c853e24d573a3bd9ea65ed4
|
|
| MD5 |
b8d780bb8625d79011ec6762784770ba
|
|
| BLAKE2b-256 |
0a95b01bd87430261f1294502be6f29bfe8df32f0afe5ccb13385f96a1a5a02b
|