Getting Started mit Python Client
Der ProcessCube® Python Client ermöglicht die Integration der Engine in Python-Anwendungen. Er bietet eine typisierte, synchrone REST-API sowie Pattern für External Tasks und User Tasks.
Installation
Installieren Sie den Python Client über pip:
pip install processcube-clientDer Python Client erfordert Python 3.7 oder höher.
Client erstellen
Ohne Authentifizierung
from processcube_client.core.api import Client
# Client für lokale Engine
client = Client('http://localhost:8000')
# Application Info abrufen
info = client.info()
print(info)Mit Authentifizierung
from processcube_client.core.api import Client
client = Client(
'http://localhost:8000',
client_id='my-service',
client_secret='secret-key',
authority_url='http://localhost:11235'
)
# Client ist jetzt authentifiziert
processes = client.get_process_models()Quick Start Beispiel
Prozess starten
from processcube_client.core.api import Client, ProcessStartRequest, StartCallbackType
# Client erstellen
client = Client('http://localhost:8000')
# Start-Request vorbereiten
request = ProcessStartRequest(
return_on=StartCallbackType.CallbackOnProcessInstanceCreated,
process_model_id='OrderProcess',
initial_token={
'customerName': 'Max Mustermann',
'orderAmount': 1500
}
)
# Prozess starten
response = client.process_model_start('OrderProcess', request)
print(f"Prozess-Instanz gestartet: {response.process_instance_id}")Prozess-Instanzen abfragen
# Alle Instanzen eines Prozesses
instances = client.process_instances_query({
'process_model_id': 'OrderProcess'
})
for instance in instances:
print(f"Instanz: {instance.process_instance_id}, Status: {instance.state}")Type Hints und Typisierung
Der Python Client bietet vollständige Type Hints für alle Funktionen:
from typing import Dict, Any
from processcube_client.core.api import Client, ProcessStartRequest, ProcessStartResponse
def start_order_process(
client: Client,
customer_name: str,
order_amount: float
) -> ProcessStartResponse:
"""Startet einen Order-Prozess."""
request = ProcessStartRequest(
return_on=StartCallbackType.CallbackOnProcessInstanceCreated,
process_model_id='OrderProcess',
initial_token={
'customerName': customer_name,
'orderAmount': order_amount
}
)
return client.process_model_start('OrderProcess', request)Async/Await Support
Der Python Client unterstützt async/await für External Tasks und User Tasks:
import asyncio
from processcube_client.user_task import UserTaskClient
async def process_user_tasks(engine_url: str):
async with UserTaskClient(engine_url) as client:
# User Tasks abrufen
user_tasks = await client.get_user_tasks()
for task in user_tasks:
print(f"User Task: {task['userTaskInstanceId']}")
# Task bearbeiten
result = await client.finish_user_task(
task['userTaskInstanceId'],
{'approved': True}
)
# Event Loop starten
asyncio.run(process_user_tasks('http://localhost:8000'))Logging
Der Python Client nutzt das Standard-Logging-Modul:
import logging
from processcube_client.core.api import Client
# Logging konfigurieren
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Client verwenden
client = Client('http://localhost:8000')
logger.info('Client erstellt')Best Practices
Environment Variables
import os
from processcube_client.core.api import Client
def create_client() -> Client:
"""Erstellt einen Client mit Environment Variables."""
return Client(
os.getenv('ENGINE_URL', 'http://localhost:8000'),
client_id=os.getenv('CLIENT_ID'),
client_secret=os.getenv('CLIENT_SECRET'),
authority_url=os.getenv('AUTHORITY_URL', 'http://localhost:11235')
)
client = create_client()Fehlerbehandlung
from processcube_client.core.api import Client
from processcube_client.exceptions import ProcessCubeException
client = Client('http://localhost:8000')
try:
response = client.process_model_start('OrderProcess', request)
print(f"Prozess gestartet: {response.process_instance_id}")
except ProcessCubeException as e:
print(f"Fehler beim Starten: {e}")Framework-Integration
FastAPI
from fastapi import FastAPI, Depends
from processcube_client.core.api import Client
app = FastAPI()
def get_processcube_client() -> Client:
"""Dependency für ProcessCube Client."""
return Client('http://localhost:8000')
@app.post("/orders")
async def create_order(
customer_name: str,
order_amount: float,
client: Client = Depends(get_processcube_client)
):
request = ProcessStartRequest(
return_on=StartCallbackType.CallbackOnProcessInstanceCreated,
process_model_id='OrderProcess',
initial_token={
'customerName': customer_name,
'orderAmount': order_amount
}
)
response = client.process_model_start('OrderProcess', request)
return {
"process_instance_id": response.process_instance_id,
"status": "started"
}Flask
from flask import Flask, request, jsonify
from processcube_client.core.api import Client
app = Flask(__name__)
client = Client('http://localhost:8000')
@app.route('/orders', methods=['POST'])
def create_order():
data = request.json
start_request = ProcessStartRequest(
return_on=StartCallbackType.CallbackOnProcessInstanceCreated,
process_model_id='OrderProcess',
initial_token=data
)
response = client.process_model_start('OrderProcess', start_request)
return jsonify({
'process_instance_id': response.process_instance_id,
'status': 'started'
})Nächste Schritte
- REST API - Vollständige API-Dokumentation
- External Tasks - Worker für externe Systeme
- User Tasks - Benutzer-Interaktionen
Tipp: Für Production-Umgebungen empfehlen wir die Verwendung von Environment Variables für Credentials und Engine-URLs.