From 627edab9e9f6df652613f0f56f79b0701b0e38e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Fri, 1 Sep 2023 22:56:20 +0000 Subject: [PATCH] Report.push_webhook() --- kiwi_simple_metrics/main.py | 13 ++----------- kiwi_simple_metrics/metrics/_report.py | 15 +++++++++++++++ kiwi_simple_metrics/settings/misc.py | 6 ------ 3 files changed, 17 insertions(+), 17 deletions(-) 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