webhook
webhook
¶
Generic webhook notification implementation using httpx.
Provides configurable HTTP webhook notifications for Marianne job events. Supports custom headers, retries, and flexible JSON payloads.
Phase 5 of Marianne implementation: Missing README features.
Classes¶
WebhookNotifier
¶
WebhookNotifier(url=None, url_env=None, headers=None, events=None, timeout=30.0, max_retries=2, retry_delay=1.0, include_metadata=True)
Generic HTTP webhook notification implementation.
Posts JSON notifications to configurable HTTP endpoints. Supports custom headers (for auth tokens), retries, and timeouts.
Example usage
notifier = WebhookNotifier( url="https://example.com/webhooks/marianne", headers={"Authorization": "Bearer token123"}, events={NotificationEvent.JOB_COMPLETE, NotificationEvent.JOB_FAILED}, ) await notifier.send(context)
Configuration from YAML
notifications: - type: webhook on_events: [job_complete, job_failed] config: url: https://example.com/webhook url_env: MZT_WEBHOOK_URL # Alternative to url headers: Authorization: "Bearer ${WEBHOOK_TOKEN}" X-Custom-Header: "value" timeout: 30 max_retries: 2 retry_delay: 1.0
Initialize the webhook notifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str | None
|
Direct webhook URL. |
None
|
url_env
|
str | None
|
Environment variable containing webhook URL. |
None
|
headers
|
dict[str, str] | None
|
HTTP headers to include in requests. |
None
|
events
|
set[NotificationEvent] | None
|
Set of events to subscribe to. Defaults to job-level events. |
None
|
timeout
|
float
|
HTTP request timeout in seconds. |
30.0
|
max_retries
|
int
|
Maximum retry attempts on failure (0 = no retries). |
2
|
retry_delay
|
float
|
Delay between retries in seconds. |
1.0
|
include_metadata
|
bool
|
Include Marianne metadata in payload (version, source). |
True
|
Source code in src/marianne/notifications/webhook.py
Attributes¶
subscribed_events
property
¶
Events this notifier is registered to receive.
Returns:
| Type | Description |
|---|---|
set[NotificationEvent]
|
Set of subscribed NotificationEvent types. |
Functions¶
from_config
classmethod
¶
Create WebhookNotifier from YAML configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
on_events
|
list[str]
|
List of event name strings from config. |
required |
config
|
dict[str, Any] | None
|
Optional dict with webhook-specific settings: - url: Direct webhook URL - url_env: Env var for webhook URL - headers: Dict of HTTP headers - timeout: Request timeout in seconds - max_retries: Retry attempts on failure - retry_delay: Delay between retries - include_metadata: Include Marianne metadata |
None
|
Returns:
| Type | Description |
|---|---|
WebhookNotifier
|
Configured WebhookNotifier instance. |
Example
notifier = WebhookNotifier.from_config( on_events=["job_complete", "job_failed"], config={ "url": "https://example.com/webhook", "headers": {"X-API-Key": "secret"}, }, )
Source code in src/marianne/notifications/webhook.py
send
async
¶
Send a webhook notification.
Posts JSON payload to the configured URL. Implements retry logic for transient failures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
NotificationContext
|
Notification context with event details. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if notification was sent, False if unavailable or failed. |
Source code in src/marianne/notifications/webhook.py
close
async
¶
Clean up HTTP client resources.
Called when the NotificationManager is shutting down.
Source code in src/marianne/notifications/webhook.py
MockWebhookNotifier
¶
Mock webhook notifier for testing.
Records all notifications sent without making HTTP calls. Useful for testing notification integration.
Initialize mock notifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
set[NotificationEvent] | None
|
Set of events to subscribe to. |
None
|
Source code in src/marianne/notifications/webhook.py
Attributes¶
Functions¶
set_fail_next
¶
Configure the next send() call to fail.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
should_fail
|
bool
|
If True, next send() returns False. |
True
|
simulate_status_code
¶
Simulate a specific HTTP status code response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
int
|
HTTP status code to simulate. |
required |
send
async
¶
Record notification without making HTTP call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
NotificationContext
|
Notification context. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True unless set_fail_next was called or simulated error status. |
Source code in src/marianne/notifications/webhook.py
close
async
¶
get_notification_count
¶
get_notifications_for_event
¶
Get all notifications for a specific event type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
NotificationEvent
|
Event type to filter by. |
required |
Returns:
| Type | Description |
|---|---|
list[NotificationContext]
|
List of matching notification contexts. |