-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifier.py
More file actions
28 lines (23 loc) · 769 Bytes
/
notifier.py
File metadata and controls
28 lines (23 loc) · 769 Bytes
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
from __future__ import annotations
import json
import urllib.request
from typing import Any
class WebhookNotifier:
def __init__(self, enabled: bool, url: str) -> None:
self.enabled = enabled
self.url = url.strip()
def send(self, event: str, payload: dict[str, Any]) -> None:
if not self.enabled or not self.url:
return
body = json.dumps({"event": event, "payload": payload}).encode("utf-8")
request = urllib.request.Request(
self.url,
data=body,
headers={"Content-Type": "application/json"},
method="POST",
)
try:
with urllib.request.urlopen(request, timeout=10):
pass
except Exception:
pass