-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook_example.py
More file actions
277 lines (217 loc) · 8.5 KB
/
webhook_example.py
File metadata and controls
277 lines (217 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python3
"""
Webhook examples for Seedance SDK
"""
import asyncio
import os
from seedance import create_webhook_server, WebhookHandler, TaskData
def webhook_server_example():
"""Example of running a webhook server"""
print("=== Webhook Server Example ===")
# Create webhook server with default handlers
webhook_secret = os.getenv("SEEDANCE_WEBHOOK_SECRET", "your-webhook-secret")
server = create_webhook_server(webhook_secret)
# Add custom handlers
@server.handler.on_task_completed
def custom_completed_handler(task: TaskData):
print(f"🎉 Custom handler: Task {task.id} completed!")
print(f" Model: {task.model}")
print(f" Duration: {task.result.duration}s")
# You could:
# - Update your database
# - Send notifications
# - Process the video further
# - Update user credits
@server.handler.on_task_failed
def custom_failed_handler(task: TaskData):
print(f"💥 Custom handler: Task {task.id} failed!")
print(f" Error: {task.error}")
# You could:
# - Log the error
# - Notify users
# - Retry with different parameters
# - Issue refunds
print(f"Starting webhook server on http://localhost:8000")
print("Send webhooks to: http://localhost:8000/webhook")
print("Health check: http://localhost:8000/health")
print("\nPress Ctrl+C to stop the server")
# Run server (this blocks)
server.run(host="localhost", port=8000)
async def webhook_handler_example():
"""Example of using webhook handler in your own endpoint"""
print("\n=== Webhook Handler Example ===")
webhook_secret = os.getenv("SEEDANCE_WEBHOOK_SECRET", "your-webhook-secret")
handler = WebhookHandler(webhook_secret)
# Define handlers
completed_tasks = []
failed_tasks = []
@handler.on_task_completed
async def handle_completed(task: TaskData):
completed_tasks.append(task)
print(f"✅ Completed: {task.id}")
# Example: Save to database
# await save_video_result(task)
# Example: Send notification
# await send_user_notification(task.user_id, "Your video is ready!")
# Example: Queue additional processing
# await queue_video_processing(task.result.video_url)
@handler.on_task_failed
async def handle_failed(task: TaskData):
failed_tasks.append(task)
print(f"❌ Failed: {task.id} - {task.error}")
# Example: Log error
# await log_error(task.error, task.id)
# Example: Notify support
# await notify_support(f"Task {task.id} failed: {task.error}")
@handler.on_any_event
async def handle_any_event(webhook_payload):
print(f"📡 Event: {webhook_payload.event}")
print(f" Timestamp: {webhook_payload.timestamp}")
# Simulate webhook processing
print("Simulating webhook processing...")
# This would normally come from your HTTP endpoint
mock_payload = b'''
{
"event": "task.completed",
"timestamp": "2025-01-01T12:00:00Z",
"data": {
"id": "task_123456",
"status": "completed",
"model": "seedance-2.0",
"task_type": "text_to_video",
"created_at": "2025-01-01T11:58:00Z",
"updated_at": "2025-01-01T12:00:00Z",
"credits_consumed": 180,
"result": {
"video_url": "https://cdn.example.com/videos/task_123456.mp4",
"thumbnail_url": "https://cdn.example.com/thumbnails/task_123456.jpg",
"duration": 8,
"resolution": "1080p",
"has_audio": true,
"file_size": 15728640
}
}
}
'''
# Generate mock signature (in real scenario, this comes from Seedance)
import hashlib
import hmac
mock_signature = hmac.new(
webhook_secret.encode(),
mock_payload,
hashlib.sha256
).hexdigest()
# Process webhook
try:
webhook_payload = await handler.process_webhook(
mock_payload,
mock_signature,
raise_on_error=True
)
print(f"\n✅ Webhook processed successfully!")
print(f" Event: {webhook_payload.event}")
print(f" Task ID: {webhook_payload.data.id}")
print(f" Video URL: {webhook_payload.data.result.video_url}")
except Exception as e:
print(f"❌ Webhook processing failed: {e}")
def flask_webhook_example():
"""Example of integrating webhook handler with Flask"""
print("\n=== Flask Webhook Example ===")
webhook_secret = os.getenv("SEEDANCE_WEBHOOK_SECRET", "your-webhook-secret")
handler = WebhookHandler(webhook_secret)
try:
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route("/webhook", methods=["POST"])
def webhook_endpoint():
try:
payload = request.data
signature = request.headers.get("X-Seedance-Signature")
if not signature:
return jsonify({"error": "Missing signature"}), 400
# Process webhook asynchronously
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
webhook_payload = loop.run_until_complete(
handler.process_webhook(payload, signature, raise_on_error=True)
)
return jsonify({"status": "ok"})
finally:
loop.close()
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/health")
def health_check():
return jsonify({"status": "healthy"})
print("Flask webhook example code generated!")
print("To run:")
print("pip install flask")
print("python examples/webhook_example.py")
print("Then run: flask run")
return app
except ImportError:
print("Flask not installed. Install with: pip install flask")
return None
def django_webhook_example():
"""Example of integrating webhook handler with Django"""
print("\n=== Django Webhook Example ===")
webhook_code = '''
# Django views.py example
import json
import asyncio
from django.http import JsonResponse, HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods
from seedance import WebhookHandler
# Initialize webhook handler
webhook_secret = "your-webhook-secret"
handler = WebhookHandler(webhook_secret)
@handler.on_task_completed
async def handle_completed(task):
# Handle completed task
pass
@handler.on_task_failed
async def handle_failed(task):
# Handle failed task
pass
@csrf_exempt
@require_http_methods(["POST"])
def webhook_endpoint(request):
try:
payload = request.body
signature = request.headers.get("X-Seedance-Signature")
if not signature:
return JsonResponse({"error": "Missing signature"}, status=400)
# Process webhook
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
webhook_payload = loop.run_until_complete(
handler.process_webhook(payload, signature, raise_on_error=True)
)
return JsonResponse({"status": "ok"})
finally:
loop.close()
except Exception as e:
return JsonResponse({"error": str(e)}, status=500)
def health_check(request):
return JsonResponse({"status": "healthy"})
'''
print("Django webhook example code:")
print(webhook_code)
if __name__ == "__main__":
print("Seedance SDK Webhook Examples")
print("=" * 50)
# Uncomment to run webhook server
# webhook_server_example()
# Run other examples
asyncio.run(webhook_handler_example())
flask_webhook_example()
django_webhook_example()
print("\n" + "=" * 50)
print("Webhook examples completed!")
print("\nTo test with real webhooks:")
print("1. Set SEEDANCE_WEBHOOK_SECRET environment variable")
print("2. Configure your Seedance dashboard to send webhooks to your endpoint")
print("3. Run one of the webhook server examples")