Coverage for functions/messages.py: 100%
32 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-08-30 11:51 -0700
« prev ^ index » next coverage.py v7.4.4, created at 2024-08-30 11:51 -0700
1"""
2MessageHandling class.
3"""
5# └── functions/messages.py
6# ├── [API] log()
7# ├── [BROKER] message()
8# ├── [BROKER] debug()
9# └── [BROKER] toast()
11from .broker import BrokerConnect
12from .api import ApiConnect
13from .information import Information
15MESSAGE_TYPES = ["assertion", "busy", "debug",
16 "error", "fun", "info", "success", "warn"]
17CHANNELS = ["ticker", "toast", "email", "espeak"]
20def validate_log_options(message_type, channel):
21 """Validate the message type and channel options."""
22 if message_type not in MESSAGE_TYPES:
23 raise ValueError(
24 f"Invalid message type: `{message_type}` not in {MESSAGE_TYPES}")
25 if channel not in CHANNELS:
26 raise ValueError(f"Invalid channel: {channel} not in {CHANNELS}")
29class MessageHandling():
30 """Message handling class."""
31 def __init__(self, state):
32 self.broker = BrokerConnect(state)
33 self.api = ApiConnect(state)
34 self.info = Information(state)
35 self.state = state
37 def log(self, message_str, message_type="info", channel="ticker"):
38 """Sends new log message via the API."""
39 self.state.print_status(description="Sending new log message to the API.")
41 validate_log_options(message_type, channel)
43 log_message = {
44 "message": message_str,
45 "type": message_type,
46 "channels": [channel],
47 }
49 self.info.api_post("logs", log_message)
51 def message(self, message_str, message_type="info", channel="ticker"):
52 """Sends new log message via the message broker."""
53 self.state.print_status(description="Sending new log message to the message broker.")
55 validate_log_options(message_type, channel)
57 message = {
58 "kind": "send_message",
59 "args": {
60 "message": message_str,
61 "message_type": message_type,
62 },
63 "body": [{
64 "kind": "channel",
65 "args": {
66 "channel_name": channel
67 }
68 }]
69 }
71 self.broker.publish(message)
73 def debug(self, message_str):
74 """Sends debug message used for developer information or troubleshooting."""
75 self.state.print_status(description="Sending debug message to the message broker.")
77 self.message(message_str, "debug", "ticker")
79 def toast(self, message_str):
80 """Sends a message that pops up on the user interface briefly."""
81 self.state.print_status(description="Sending toast message to the message broker.")
83 self.message(message_str, "info", "toast")