-
Notifications
You must be signed in to change notification settings - Fork 9
Open
Description
I wasted some hours trying to get this API work on my fork. The requirements are frozen, so it needs a fix right away. I wanted to use Events API, but it does nor produce usable return type. It is all kind of awkward.
For my purpose I wrote my code from scrap:
import os
import requests
from enum import Enum
from typing import Optional, Dict, Any
import logging
class ZendutyAlertType(Enum):
CRITICAL = "critical"
ERROR = "error"
RESOLVED = "resolved"
class ZendutyClient:
"""
A class to handle sending alerts to Zenduty service.
"""
def __init__(self, api_key: Optional[str] = None):
"""
Initialize the ZendutyAlerts client.
Args:
api_key: Zenduty API key. If not provided, will look for ZENDUTY_API_KEY env variable
"""
self.api_key = api_key or os.getenv("ZENDUTY_API_KEY")
if not self.api_key:
raise ValueError(
"ZENDUTY_API_KEY must be provided or set as environment variable"
)
self.logger = logging.getLogger(__name__)
self.base_url = "https://www.zenduty.com/api/events"
def send_alert(
self,
*,
alert_type: ZendutyAlertType,
message: str,
entity_id: str,
summary: str,
payload: Optional[Dict[str, Any]] = None,
urls: Optional[Dict[str, str]] = None,
):
try:
request_data: Dict[str, Any] = {
"alert_type": alert_type.value,
"entity_id": entity_id,
"message": message,
"summary": summary,
}
if payload is not None:
request_data["payload"] = payload
if urls is not None:
request_data["urls"] = [
{"link_text": k, "link_url": v} for k, v in urls.items()
]
url = f"{self.base_url}/{self.api_key}/"
response = requests.post(
url, json=request_data, headers={"Content-Type": "application/json"}
)
if self.logger.isEnabledFor(logging.DEBUG):
self.logger.debug(f"Request data: {request_data}")
self.logger.debug(f"Response data: {response.json()}")
return response
except requests.exceptions.RequestException as e:
self.logger.error(f"Failed to send alert to Zenduty: {str(e)}")
return False
except Exception as e:
self.logger.error(f"Error sending alert: {str(e)}")
return FalseReactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels