-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook_server.py
More file actions
52 lines (40 loc) · 1.69 KB
/
Copy pathwebhook_server.py
File metadata and controls
52 lines (40 loc) · 1.69 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
"""Receive and verify SonoVault webhook deliveries.
1. Register the endpoint: sv.webhooks.create(url="https://your.host/webhooks/sonovault")
2. Store the returned secret (shown only once) in SONOVAULT_WEBHOOK_SECRET.
Run: SONOVAULT_WEBHOOK_SECRET=whsec_... python examples/webhook_server.py
Uses only the standard library. In a real app, use your web framework's
raw-body access and call verify_webhook_signature the same way.
"""
import json
import os
from http.server import BaseHTTPRequestHandler, HTTPServer
from sonovault import verify_webhook_signature
SECRET = os.environ["SONOVAULT_WEBHOOK_SECRET"]
class WebhookHandler(BaseHTTPRequestHandler):
def do_POST(self):
if self.path != "/webhooks/sonovault":
self.send_response(404)
self.end_headers()
return
# Read the RAW body. Verify before parsing JSON.
payload = self.rfile.read(int(self.headers.get("Content-Length", 0)))
ok = verify_webhook_signature(
secret=SECRET,
header=self.headers.get("SonoVault-Signature", ""),
payload=payload,
)
if not ok:
self.send_response(400)
self.end_headers()
return
event = json.loads(payload)
# Dedupe on the stable event id: deliveries are retried on failure.
track = event["data"].get("track") or {}
print(event["id"], event["type"], event["data"]["stream_id"], track.get("title", ""))
self.send_response(200)
self.end_headers()
def log_message(self, *args):
pass # keep stdout for events
if __name__ == "__main__":
print("Listening on :8787")
HTTPServer(("", 8787), WebhookHandler).serve_forever()