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
| import httpx
import hashlib
import hmac
import time
import json
from typing import Optional
class WebhookSender:
def __init__(self, secret: str, max_retries: int = 5):
self.secret = secret
self.max_retries = max_retries
self.retry_delays = [1, 5, 30, 120, 600] # seconds
def sign_payload(self, payload: str, timestamp: int) -> str:
"""Create HMAC signature for payload."""
message = f"{timestamp}.{payload}"
signature = hmac.new(
self.secret.encode(),
message.encode(),
hashlib.sha256
).hexdigest()
return f"t={timestamp},v1={signature}"
async def send(
self,
url: str,
event_type: str,
data: dict,
webhook_id: Optional[str] = None
) -> dict:
"""Send webhook with automatic retries."""
payload = json.dumps({
"id": webhook_id or str(uuid.uuid4()),
"type": event_type,
"created_at": datetime.utcnow().isoformat(),
"data": data
})
timestamp = int(time.time())
signature = self.sign_payload(payload, timestamp)
headers = {
"Content-Type": "application/json",
"X-Webhook-Signature": signature,
"X-Webhook-Timestamp": str(timestamp),
"X-Webhook-ID": webhook_id,
}
last_error = None
for attempt in range(self.max_retries):
try:
async with httpx.AsyncClient() as client:
response = await client.post(
url,
content=payload,
headers=headers,
timeout=30.0
)
if response.status_code == 200:
return {"status": "delivered", "attempt": attempt + 1}
if response.status_code >= 500:
# Server error, retry
raise Exception(f"Server error: {response.status_code}")
# Client error (4xx), don't retry
return {
"status": "failed",
"error": f"Client error: {response.status_code}",
"attempt": attempt + 1
}
except Exception as e:
last_error = str(e)
if attempt < self.max_retries - 1:
delay = self.retry_delays[min(attempt, len(self.retry_delays) - 1)]
await asyncio.sleep(delay)
return {
"status": "failed",
"error": last_error,
"attempts": self.max_retries
}
|