Skip to content

Commit 855a258

Browse files
committed
chore: refactor notification functions
1 parent abd1934 commit 855a258

File tree

2 files changed

+69
-37
lines changed

2 files changed

+69
-37
lines changed

observer/notification.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from typing import Any
2+
3+
import requests
4+
5+
from configuration.types import (
6+
NotificationDiscord,
7+
NotificationGeneric,
8+
NotificationSlack,
9+
NotificationTelegram,
10+
)
11+
12+
from .message import Message
13+
14+
15+
def notify(
16+
url: str, method: str, headers: dict[str, str], json: dict[str, Any]
17+
) -> requests.Response | None:
18+
try:
19+
return requests.request(
20+
url=url,
21+
method=method,
22+
headers=headers,
23+
json=json,
24+
)
25+
except Exception:
26+
pass
27+
28+
29+
def notify_discord(
30+
config: NotificationDiscord, message: str
31+
) -> requests.Response | None:
32+
return notify(
33+
config.webhook_url,
34+
"POST",
35+
headers={"Content-Type": "application/json"},
36+
json={"content": message},
37+
)
38+
39+
40+
def notify_slack(config: NotificationSlack, message: str) -> requests.Response | None:
41+
return notify(
42+
config.webhook_url,
43+
"POST",
44+
headers={"Content-Type": "application/json"},
45+
json={"text": message},
46+
)
47+
48+
49+
def notify_telegram(
50+
config: NotificationTelegram, message: str
51+
) -> requests.Response | None:
52+
return notify(
53+
f"https://api.telegram.org/bot{config.bot_token}/sendMessage",
54+
"POST",
55+
headers={"Content-Type": "application/json"},
56+
json={"chat_id": config.chat_id, "text": message},
57+
)
58+
59+
60+
def notify_generic(
61+
config: NotificationGeneric, issue: "Message"
62+
) -> requests.Response | None:
63+
return notify(
64+
config.webhook_url,
65+
"POST",
66+
headers={"Content-Type": "application/json"},
67+
json={"level": issue.level.value, "message": issue.message},
68+
)

observer/observer.py

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import time
33
from typing import Self
44

5-
import requests
65
from eth_account._utils.signing import to_standard_v
76
from eth_keys.datatypes import Signature as EthSignature
87
from py_flare_common.fsp.epoch.epoch import RewardEpoch
@@ -22,10 +21,6 @@
2221

2322
from configuration.types import (
2423
Configuration,
25-
NotificationDiscord,
26-
NotificationGeneric,
27-
NotificationSlack,
28-
NotificationTelegram,
2924
)
3025
from observer.reward_epoch_manager import (
3126
Entity,
@@ -45,6 +40,7 @@
4540
)
4641

4742
from .message import Message, MessageLevel
43+
from .notification import notify_discord, notify_generic, notify_slack, notify_telegram
4844

4945
LOGGER = logging.getLogger(__name__)
5046
logging.basicConfig(
@@ -65,38 +61,6 @@ def from_vrs(cls, s: SSignature) -> Self:
6561
)
6662

6763

68-
def notify_discord(config: NotificationDiscord, message: str) -> None:
69-
requests.post(
70-
config.webhook_url,
71-
headers={"Content-Type": "application/json"},
72-
json={"content": message},
73-
)
74-
75-
76-
def notify_slack(config: NotificationSlack, message: str) -> None:
77-
requests.post(
78-
config.webhook_url,
79-
headers={"Content-Type": "application/json"},
80-
json={"text": message},
81-
)
82-
83-
84-
def notify_telegram(config: NotificationTelegram, message: str) -> None:
85-
requests.post(
86-
f"https://api.telegram.org/bot{config.bot_token}/sendMessage",
87-
headers={"Content-Type": "application/json"},
88-
json={"chat_id": config.chat_id, "text": message},
89-
)
90-
91-
92-
def notify_generic(config: NotificationGeneric, issue: "Message") -> None:
93-
requests.post(
94-
config.webhook_url,
95-
headers={"Content-Type": "application/json"},
96-
json={"level": issue.level.value, "message": issue.message},
97-
)
98-
99-
10064
async def find_voter_registration_blocks(
10165
w: AsyncWeb3,
10266
current_block_id: int,

0 commit comments

Comments
 (0)