diff --git a/matrix_alertbot/alertmanager.py b/matrix_alertbot/alertmanager.py index bab8a0c..1bdc7c4 100644 --- a/matrix_alertbot/alertmanager.py +++ b/matrix_alertbot/alertmanager.py @@ -1,7 +1,7 @@ import datetime from typing import Dict, List -import diskcache +from diskcache import Cache import pytimeparse import requests from requests import RequestException @@ -15,10 +15,9 @@ from matrix_alertbot.errors import ( class AlertmanagerClient: - def __init__(self, config: Config) -> None: - url = config.alertmanager_url + def __init__(self, url: str, cache: Cache) -> None: self.api_url = f"{url}/api/v2" - self.cache = diskcache.Cache(config.cache_dir) + self.cache = cache def get_alerts(self) -> List[Dict]: try: diff --git a/matrix_alertbot/callbacks.py b/matrix_alertbot/callbacks.py index 8f82d52..0833726 100644 --- a/matrix_alertbot/callbacks.py +++ b/matrix_alertbot/callbacks.py @@ -25,6 +25,7 @@ class Callbacks: self, client: AsyncClient, alertmanager: AlertmanagerClient, + cache: Cache, config: Config, ): """ @@ -38,7 +39,7 @@ class Callbacks: config: Bot configuration parameters. """ self.client = client - self.cache = Cache(config.cache_dir) + self.cache = cache self.alertmanager = alertmanager self.config = config self.command_prefix = config.command_prefix diff --git a/matrix_alertbot/main.py b/matrix_alertbot/main.py index 920fc97..8092b7d 100644 --- a/matrix_alertbot/main.py +++ b/matrix_alertbot/main.py @@ -88,8 +88,11 @@ def main() -> None: # Read the parsed config file and create a Config object config = Config(config_path) + # Configure the cache + cache = diskcache.Cache(config.cache_dir) + # Configure Alertmanager client - alertmanager = AlertmanagerClient(config) + alertmanager = AlertmanagerClient(config.alertmanager_url, cache) # Configuration options for the AsyncClient client_config = AsyncClientConfig( @@ -113,7 +116,7 @@ def main() -> None: client.user_id = config.user_id # Set up event callbacks - callbacks = Callbacks(client, alertmanager, config) + callbacks = Callbacks(client, alertmanager, cache, config) client.add_event_callback(callbacks.message, (RoomMessageText,)) client.add_event_callback( callbacks.invite_event_filtered_callback, (InviteMemberEvent,) @@ -121,7 +124,7 @@ def main() -> None: client.add_event_callback(callbacks.decryption_failure, (MegolmEvent,)) client.add_event_callback(callbacks.unknown, (UnknownEvent,)) - webhook_server = Webhook(client, config) + webhook_server = Webhook(client, cache, config) loop = asyncio.get_event_loop() loop.create_task(webhook_server.start()) diff --git a/matrix_alertbot/webhook.py b/matrix_alertbot/webhook.py index fd81788..80b45c3 100644 --- a/matrix_alertbot/webhook.py +++ b/matrix_alertbot/webhook.py @@ -48,9 +48,7 @@ async def create_alert(request: web_request.Request) -> web.Response: class Webhook: - def __init__(self, client: AsyncClient, config: Config) -> None: - cache = Cache(config.cache_dir) - + def __init__(self, client: AsyncClient, cache: Cache, config: Config) -> None: self.app = web.Application(logger=logger) self.app["client"] = client self.app["config"] = config diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 41e6733..4c1fa11 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -2,6 +2,7 @@ import unittest from unittest.mock import Mock import nio +from diskcache import Cache from matrix_alertbot.alertmanager import AlertmanagerClient from matrix_alertbot.callbacks import Callbacks @@ -15,13 +16,14 @@ class CallbacksTestCase(unittest.TestCase): self.fake_client = Mock(spec=nio.AsyncClient) self.fake_client.user = "@fake_user:example.com" + self.fake_cache = Mock(spec=Cache) self.fake_alertmanager = Mock(spec=AlertmanagerClient) # We don't spec config, as it doesn't currently have well defined attributes self.fake_config = Mock() self.callbacks = Callbacks( - self.fake_client, self.fake_alertmanager, self.fake_config + self.fake_client, self.fake_cache, self.fake_alertmanager, self.fake_config ) def test_invite(self) -> None: