use config as arg for function instead of values

This commit is contained in:
histausse 2021-10-06 17:03:02 +02:00
parent c7de4cfbc0
commit c2e149aa1d
Signed by: histausse
GPG key ID: 67486F107F62E9E9
3 changed files with 12 additions and 18 deletions

View file

@ -17,10 +17,7 @@ async def __main():
bot_corout = send_messages( bot_corout = send_messages(
message_queue, message_queue,
config.username, config
config.homeserver,
config.password,
config.alert_rooms
) )
format_corout = format_alerts( format_corout = format_alerts(
alert_queue, alert_queue,
@ -28,8 +25,7 @@ async def __main():
) )
webhook_corout = run_webhook( webhook_corout = run_webhook(
alert_queue, alert_queue,
config.host, config
config.port
) )

View file

@ -9,6 +9,7 @@ from typing import (
) )
from matrix_bot.client import Client from matrix_bot.client import Client
from matrix_bot.invite_policy import WhiteList from matrix_bot.invite_policy import WhiteList
from .config import Config
from .format import Message from .format import Message
async def __send_messsages( async def __send_messsages(
@ -31,27 +32,24 @@ async def __send_messsages(
async def send_messages( async def send_messages(
message_queue: asyncio.Queue[dict[str, Any]], # For now, type will change in the futur message_queue: asyncio.Queue[dict[str, Any]], # For now, type will change in the futur
username: str, config: Config
homeserver: str,
password: str,
alert_rooms: list[str]
): ):
""" """
Initialize the bot and send messages added to the queue to the alert_rooms. Initialize the bot and send messages added to the queue to the alert_rooms.
""" """
bot = await Client( bot = await Client(
username, config.username,
homeserver, config.homeserver,
password config.password
) )
invite_policy = await WhiteList(bot, alert_rooms) invite_policy = await WhiteList(bot, config.alert_rooms)
bot.set_invite_policy(invite_policy) bot.set_invite_policy(invite_policy)
await asyncio.gather( await asyncio.gather(
bot.run(), bot.run(),
__send_messsages( __send_messsages(
message_queue, message_queue,
bot, bot,
alert_rooms config.alert_rooms
) )
) )

View file

@ -10,13 +10,13 @@ from typing import (
Any, Any,
NoReturn NoReturn
) )
from .config import Config
ENDPOINT = "/webhook" ENDPOINT = "/webhook"
async def run_webhook( async def run_webhook(
alert_queue: asyncio.Queue[dict[str, Any]], alert_queue: asyncio.Queue[dict[str, Any]],
host: str, config: Config
port: int
)->NoReturn: )->NoReturn:
""" """
Run the webhook endpoint and put the alerts in the queue. Run the webhook endpoint and put the alerts in the queue.
@ -31,5 +31,5 @@ async def run_webhook(
app.add_routes([aiohttp.web.post(ENDPOINT, handler)]) app.add_routes([aiohttp.web.post(ENDPOINT, handler)])
runner = aiohttp.web.AppRunner(app) runner = aiohttp.web.AppRunner(app)
await runner.setup() await runner.setup()
site = aiohttp.web.TCPSite(runner, host, port) site = aiohttp.web.TCPSite(runner, config.host, config.port)
await site.start() await site.start()