- Created pyproject.toml for project metadata and dependencies. - Added requirements.txt for development and production dependencies. - Implemented basic test structure in the tests module. - Developed unit tests for ElytraClient, including Pydantic validation and error handling.
115 lines
3.8 KiB
Python
115 lines
3.8 KiB
Python
"""Example usage of the Elytra PIM Client with Pydantic validation"""
|
|
|
|
from dotenv import load_dotenv
|
|
from elytra_client import (
|
|
ElytraClient,
|
|
SingleProductResponse,
|
|
SingleNewProductRequestBody,
|
|
AttributeRequestBody,
|
|
)
|
|
from elytra_client.config import ElytraConfig
|
|
|
|
# Load environment variables from .env file
|
|
load_dotenv()
|
|
|
|
|
|
def fetch_products_example(client):
|
|
"""Example showing product fetching with Pydantic validation"""
|
|
print("\n--- Fetching Products with Pydantic Validation ---")
|
|
products_response = client.get_products(lang="en", page=1, limit=3)
|
|
|
|
print(f"Total products: {products_response.get('total')}")
|
|
print(f"Current page: {products_response.get('page')}")
|
|
|
|
# Items are Pydantic validated models with full type hints
|
|
if products_response.get("items"):
|
|
for product in products_response["items"]:
|
|
assert isinstance(product, SingleProductResponse)
|
|
print(f" - {product.productName} (ID: {product.id})")
|
|
|
|
|
|
def fetch_single_product_example(client):
|
|
"""Example showing single product fetch with direct Pydantic model"""
|
|
print("\n--- Fetching Single Product ---")
|
|
try:
|
|
product: SingleProductResponse = client.get_product(123, lang="en")
|
|
print(f"Product Name: {product.productName}")
|
|
print(f"Status: {product.objectStatus}")
|
|
print(f"Created: {product.created}")
|
|
|
|
if product.attributes:
|
|
print(f"Attributes: {len(product.attributes)}")
|
|
for attr in product.attributes:
|
|
print(f" - {attr.attributeName}: {attr.value}")
|
|
except Exception as e:
|
|
print(f"Product not found or error: {e}")
|
|
|
|
|
|
def create_product_example(client):
|
|
"""Example showing product creation with Pydantic validation"""
|
|
print("\n--- Creating Product with Pydantic Validation ---")
|
|
try:
|
|
# Create with Pydantic model - validation happens automatically
|
|
new_product = SingleNewProductRequestBody(
|
|
productName="EXAMPLE-PRODUCT-001",
|
|
parentId=1,
|
|
attributeGroupId=10,
|
|
attributes=[
|
|
AttributeRequestBody(
|
|
attributeId=1,
|
|
value="Sample Value",
|
|
languageCode="en"
|
|
)
|
|
]
|
|
)
|
|
|
|
# Create product - returns validated Pydantic model
|
|
created: SingleProductResponse = client.create_product(new_product)
|
|
print(f"✅ Product created successfully!")
|
|
print(f" ID: {created.id}")
|
|
print(f" Name: {created.productName}")
|
|
except Exception as e:
|
|
print(f"❌ Error: {e}")
|
|
|
|
|
|
def validate_data_example():
|
|
"""Example showing Pydantic validation"""
|
|
print("\n--- Pydantic Validation Example ---")
|
|
try:
|
|
# This will validate the data
|
|
product = SingleNewProductRequestBody(
|
|
productName="VALID-PRODUCT",
|
|
parentId=1,
|
|
attributeGroupId=10
|
|
)
|
|
print("✅ Valid product data")
|
|
print(f" Data: {product.model_dump()}")
|
|
except Exception as e:
|
|
print(f"❌ Validation failed: {e}")
|
|
|
|
|
|
def main():
|
|
"""Main example demonstrating Pydantic-driven Elytra client"""
|
|
try:
|
|
# Load configuration from environment
|
|
config = ElytraConfig.from_env()
|
|
|
|
# Create client with context manager
|
|
with ElytraClient(base_url=config.base_url, api_key=config.api_key) as client:
|
|
print("✅ Connected to Elytra PIM API")
|
|
print(" Using Pydantic v2 for data validation\n")
|
|
|
|
# Run examples
|
|
validate_data_example()
|
|
fetch_products_example(client)
|
|
fetch_single_product_example(client)
|
|
create_product_example(client)
|
|
|
|
print("\n✅ All examples completed successfully!")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error: {e}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|