A Python client for the Carbone API
Project description
CarboneConnect Python Client
A Python client library for interacting with the Carbone API service. This library provides a simple interface to render documents using templates and data.
Installation
pip install py-carbone-connect
Features
- Template rendering with data
- Support for both file paths and file-like objects as templates
- Streaming response support
- Error handling with custom exceptions
- Automatic content type detection
Usage
Basic Usage
from carbone_connect import CarboneConnect
# Initialize the client
carbone = CarboneConnect('http://selfhosted.carbone-server.com') # it does not have Auth yet
# Render a template with data
template_path = 'path/to/template.docx'
data = {
'name': 'John Doe',
'company': 'ACME Corp'
}
# Generate document
result = carbone.render(template_path, data)
# Save the result
with open('output.docx', 'wb') as f:
f.write(result)
Streaming Response
# Get a streaming response
response = carbone.render_stream(template_path, data)
# Process the stream
with open('output.docx', 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
API Reference
CarboneConnect
__init__(api_url: str)
Initialize the Carbone client with the API URL.
render(template: Union[str, BinaryIO], data: Dict[str, Any], options: Optional[Dict[str, Any]] = None) -> bytes
Render a template with provided data and return the result as bytes.
template
: Path to template file or file-like objectdata
: Dictionary containing data to render in the templateoptions
: Optional rendering options
render_stream(template: Union[str, BinaryIO], data: Dict[str, Any], options: Optional[Dict[str, Any]] = None) -> requests.Response
Render a template and return a streaming response.
Error Handling
The library includes a custom CarboneError
exception class for handling API-related errors:
try:
result = carbone.render(template_path, data)
except CarboneError as e:
print(f'Error: {e}')
Test
Install development dependencies
pip install -e .[dev]
Run tests
pytest
Run tests with coverage report
pytest --cov=carbone_connect --cov-report=term-missing
Run specific test
pytest tests/test_client.py -k test_initialization
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
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
File details
Details for the file py_carbone_connect-1.1.2.tar.gz
.
File metadata
- Download URL: py_carbone_connect-1.1.2.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2de8e608a815d544137f8b569a4adc7c9f89305d7e7e0429bfcb8a76c6b4204 |
|
MD5 | 05d4f51419ab64c6a785c1674e1fd701 |
|
BLAKE2b-256 | ba52fe2f152988361470c9047706183d9d901b6fb42db4054188a0c1977ea6e4 |
File details
Details for the file py_carbone_connect-1.1.2-py3-none-any.whl
.
File metadata
- Download URL: py_carbone_connect-1.1.2-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4954d02b8b3fa9f941b5f6b4538f5d7e18545c183c944356640458d4d4317a99 |
|
MD5 | 6b2c1b08017c2f62d42187dcb04021be |
|
BLAKE2b-256 | da0a004ab6f3856be2b89fc419b384468ab73692e83c51483875fa54a5bab0d6 |