- Added `rest_api` module with client and authentication classes. - Implemented `LobsterRestApiClient` for interacting with the REST API. - Created models for job and protocol responses using Pydantic. - Developed examples demonstrating basic usage, API token authentication, job execution, and error handling. - Added unit tests for authentication and client methods, including error handling scenarios. |
||
|---|---|---|
| elytra_client | ||
| examples | ||
| tests | ||
| .agent-instructions.md | ||
| .copilot-instructions.md | ||
| .env.example | ||
| .gitignore | ||
| .pypirc.example | ||
| BUILD.md | ||
| build_requirements.txt | ||
| build_wheel.ps1 | ||
| build_wheel.py | ||
| build_wheel.sh | ||
| DEVELOPMENT.md | ||
| openapi.yaml | ||
| pyproject.toml | ||
| README.md | ||
| requirements.txt | ||
| setup.py | ||
| STYLE_GUIDE.md | ||
| UPDATE_VERSION.md | ||
| update_version.ps1 | ||
| update_version.py | ||
| update_version.sh | ||
| upload_wheel_to_forgejo_pypi.bat | ||
| upload_wheel_to_forgejo_pypi.ps1 | ||
| upload_wheel_to_forgejo_pypi.sh | ||
Elytra PIM Client
A fully Pythonic and Pydantic-driven client for the Elytra PIM (Product Information Management) API.
Features
- 🐍 Fully Pythonic with Pydantic v2 data validation
- 📦 Auto-generated Pydantic models from OpenAPI specification
- 🔐 Bearer token authentication
- ✅ Request/Response validation with Pydantic
- 🌍 Multi-language support
- 📄 Full type hints throughout the codebase
- 🧪 Comprehensive error handling
- 🔄 Context manager support
- 🔄 Automatic serialization/deserialization
Installation
From Source
Clone the repository:
git clone https://git.him-tools.de/HIM-public/elytra_client.git
cd elytra_client
pip install -e .
With Development Dependencies
pip install -e ".[dev]"
Quick Start
Basic Usage
from elytra_client import ElytraClient, SingleProductResponse
# Initialize the client
client = ElytraClient(
base_url="https://example.com/api/v1",
api_key="your-api-key"
)
# Get all products - returns dict with Pydantic validated items
products_response = client.get_products(lang="en", page=1, limit=10)
for product in products_response["items"]:
print(f"Product: {product.productName}, ID: {product.id}")
# Get a specific product - returns Pydantic model directly
product: SingleProductResponse = client.get_product(product_id=123, lang="en")
print(f"Name: {product.productName}")
print(f"Status: {product.objectStatus}")
# Close the client
client.close()
Creating Products with Validation
from elytra_client import (
ElytraClient,
SingleNewProductRequestBody,
AttributeRequestBody,
)
with ElytraClient(base_url="https://example.com/api/v1", api_key="your-api-key") as client:
# Create a new product with validated Pydantic model
new_product = SingleNewProductRequestBody(
productName="NEW-PRODUCT-001",
parentId=1,
attributeGroupId=10,
attributes=[
AttributeRequestBody(
attributeId=1,
value="Sample Value",
languageCode="en"
)
]
)
# Validation happens automatically
created_product = client.create_product(new_product)
print(f"Created product ID: {created_product.id}")
Environment Variable Configuration
Set your environment variables:
export ELYTRA_BASE_URL="https://example.com/api/v1"
export ELYTRA_API_KEY="your-api-key"
export ELYTRA_TIMEOUT="30"
export ELYTRA_VERIFY_SSL="true"
Then load from environment:
from elytra_client import ElytraClient
from elytra_client.config import ElytraConfig
config = ElytraConfig.from_env()
client = ElytraClient(base_url=config.base_url, api_key=config.api_key)
Legacy REST API (Lobster PIM)
The package also includes a subpackage for the older Lobster PIM REST API, which provides access to scheduled jobs and protocol logs that are not yet available in the newer API.
Quick Start
from elytra_client.rest_api import LobsterRestApiClient, RestApiAuth
# Create authentication
auth = RestApiAuth.from_username_password("username", "password")
# Create client for the REST API
client = LobsterRestApiClient("http://lobster-server:8080", auth=auth)
# Get all active jobs
jobs = client.get_all_active_jobs()
for job in jobs.jobInfoObjects:
print(f"Job: {job.name} - Status: {job.status}")
# Execute a job
result = client.execute_job(job_id=123)
print(f"Execution started with runtime ID: {result.runtimeId}")
# Get protocol/logs
protocols = client.get_protocols()
Features
- 📋 Job Management: Access, monitor, and execute scheduled jobs
- 📜 Protocol/Logs: Retrieve execution logs and protocol information
- 🔐 Flexible Authentication: Username/password or API token authentication
- ⏳ Job Control: Execute jobs with parameter overrides and queue management
- 🎯 Type Safety: Full Pydantic validation for all responses
Authentication Methods
# Username/Password
auth = RestApiAuth.from_username_password("admin", "password")
# API Token (domain-specific)
auth = RestApiAuth.from_api_token("admin", "token-id", domain="Jobs")
Documentation
See elytra_client/rest_api/README.md for comprehensive REST API documentation, including:
- Complete API reference
- Authentication details
- Job management examples
- Protocol/log access
- Error handling
- Common usage scenarios
API Methods
All methods return Pydantic models with full type validation and IDE autocompletion support.
Products
get_products(...) -> Dict- Get all products (items areSingleProductResponsePydantic models)get_product(id, lang) -> SingleProductResponse- Get single productcreate_product(data) -> SingleProductResponse- Create new product with validationupdate_product(data) -> SingleProductResponse- Update product with validationdelete_product(id) -> Dict- Delete product
Product Groups
get_product_groups(...) -> Dict- Get all product groups (items areSingleProductGroupResponsemodels)get_product_group(id, lang) -> SingleProductGroupResponse- Get single product groupcreate_product_group(data) -> SingleProductGroupResponse- Create new product groupupdate_product_group(data) -> SingleProductGroupResponse- Update product groupdelete_product_group(id) -> Dict- Delete product group
Attributes
get_attributes(...) -> Dict- Get all attributes (items areSingleAttributeResponsemodels)get_attribute(id, lang) -> SingleAttributeResponse- Get single attribute
Health Check
health_check() -> Dict- Check API health status
Error Handling
The client provides specific exception classes for different error types:
from elytra_client import ElytraClient
from elytra_client.exceptions import (
ElytraAuthenticationError,
ElytraNotFoundError,
ElytraValidationError,
ElytraAPIError,
)
from pydantic import ValidationError
try:
client = ElytraClient(base_url="https://example.com/api/v1", api_key="invalid-key")
product = client.get_product(123)
except ElytraAuthenticationError:
print("Authentication failed")
except ElytraNotFoundError:
print("Product not found")
except ElytraValidationError as e:
print(f"API response validation failed: {e}")
except ValidationError as e:
print(f"Request model validation failed: {e}")
except ElytraAPIError as e:
print(f"API error: {e}")
Validation
- Request validation: Pydantic models validate all input before sending to API
- Response validation: Pydantic models validate API responses for data integrity
- Automatic deserialization: Responses are automatically converted to Pydantic models
Pydantic Models
All request and response models are automatically generated from the OpenAPI specification using datamodel-code-generator.
Available Models
- Response Models:
SingleProductResponse,SingleProductGroupResponse,SingleAttributeResponse, etc. - Request Models:
SingleNewProductRequestBody,SingleUpdateProductRequestBody,SingleNewProductGroupRequestBody, etc. - Attribute Models:
ProductAttributeResponse,AttributeRequestBody
All models include:
- ✅ Full type hints and validation
- ✅ Documentation from OpenAPI spec
- ✅ IDE autocompletion support
- ✅ Automatic serialization/deserialization
Regenerating Models
To regenerate models from the OpenAPI spec:
python -m datamodel_code_generator --input openapi.yaml --input-file-type openapi --output elytra_client/models.py --target-python-version 3.10
Development
Running Tests
pytest
Code Quality
Format code with Black:
black elytra_client tests
Check with flake8:
flake8 elytra_client tests
Type checking with mypy:
mypy elytra_client
API Documentation
For complete API documentation, refer to the OpenAPI specification in openapi.yaml
see here: https://w4services.atlassian.net/wiki/spaces/pimdocext/pages/426803205/Web+API
or visit the Elytra website: https://www.elytra.ch/
Contact
For support on this client, please email: info@hoerl-im.de
License
MIT License - see LICENSE file for details