Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not seem usable #6

Open
brablc opened this issue Nov 17, 2024 · 0 comments
Open

Does not seem usable #6

brablc opened this issue Nov 17, 2024 · 0 comments

Comments

@brablc
Copy link

brablc commented Nov 17, 2024

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 False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant