-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from __future__ import annotations | ||
|
||
|
||
import functools | ||
import logging | ||
|
||
import httpx | ||
|
||
from observer.azclients import get_keyvault_client | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@functools.lru_cache | ||
def _get_notify_info_url() -> str: | ||
client = get_keyvault_client() | ||
url = client.get_secret("seareport-info-webhook-url").value | ||
assert url is not None | ||
return url | ||
|
||
|
||
@functools.lru_cache | ||
def _get_notify_error_url() -> str: | ||
client = get_keyvault_client() | ||
url = client.get_secret("seareport-error-webhook-url").value | ||
assert url is not None | ||
return url | ||
|
||
|
||
def notify_info(*, message: str, title: str = "") -> None: | ||
url = _get_notify_info_url() | ||
if not title: | ||
title = "INFO" | ||
data = {"text": f"**{title}**\n\n {message}"} | ||
try: | ||
response = httpx.post(url, json=data, timeout=10) | ||
response.raise_for_status() | ||
except httpx.HTTPError: | ||
logger.exception("Failed to send INFO notification:\n%s", data) | ||
|
||
|
||
def notify_error(*, message: str, title: str = "") -> None: | ||
url = _get_notify_error_url() | ||
if not title: | ||
title = "ERROR" | ||
data = {"text": f"**{title}**\n\n {message}"} | ||
try: | ||
response = httpx.post(url, json=data, timeout=10) | ||
response.raise_for_status() | ||
except httpx.HTTPError: | ||
logger.exception("Failed to send ERROR notification:\n%s", data) |