diff --git a/kiwi_simple_metrics/main.py b/kiwi_simple_metrics/main.py index 5f6445f..6301fea 100644 --- a/kiwi_simple_metrics/main.py +++ b/kiwi_simple_metrics/main.py @@ -1,9 +1,6 @@ #!/usr/bin/python3 import asyncio -import urllib.parse - -import requests from . import metrics from .settings import SETTINGS @@ -22,14 +19,8 @@ def handle_report() -> None: if SETTINGS.log.enabled: print(report) - # maybe push this to a webhook - if (url := SETTINGS.webhook.get_url(failed=report.failed)) is not None: - requests.get( - url=str(url).format( - urllib.parse.quote_plus(report.result) - ), - verify=not SETTINGS.webhook.insecure, - ) + # maybe push this to the configured webhook + report.push_webhook() async def run_metrics() -> None: diff --git a/kiwi_simple_metrics/metrics/_report.py b/kiwi_simple_metrics/metrics/_report.py index 0b799f4..e974ab1 100644 --- a/kiwi_simple_metrics/metrics/_report.py +++ b/kiwi_simple_metrics/metrics/_report.py @@ -1,6 +1,9 @@ +import urllib.parse from dataclasses import dataclass from typing import Any, Callable, Iterator, Self +import requests + from ..settings import SETTINGS, MetricSettings @@ -112,3 +115,15 @@ class Report: for report in reports ), ) + + def push_webhook(self) -> None: + if (url := SETTINGS.webhook.url if not self.failed + else SETTINGS.webhook.fail) is None: + return + + requests.get( + url=str(url).format( + urllib.parse.quote_plus(self.result) + ), + verify=not SETTINGS.webhook.insecure, + ) diff --git a/kiwi_simple_metrics/settings/misc.py b/kiwi_simple_metrics/settings/misc.py index c137f0c..053d04f 100644 --- a/kiwi_simple_metrics/settings/misc.py +++ b/kiwi_simple_metrics/settings/misc.py @@ -16,9 +16,3 @@ class WebhookSettings(BaseModel): # allow insecure/self-signed webhook targets insecure: bool = False - - def get_url(self, failed: bool) -> AnyUrl | None: - if failed: - return self.fail - - return self.url