Refactor codebase to by DRY
This commit is contained in:
parent
c925079e8b
commit
83a526c533
13 changed files with 320 additions and 131 deletions
|
|
@ -1,18 +1,21 @@
|
|||
import json
|
||||
import logging
|
||||
from typing import Any, Tuple
|
||||
from typing import Any, List, Tuple
|
||||
|
||||
from fastapi import Request
|
||||
|
||||
from ops_bot.common import COLOR_ALARM, COLOR_OK, COLOR_UNKNOWN
|
||||
from ops_bot.config import RoutingKey
|
||||
|
||||
|
||||
def handle_subscribe_confirm(payload: Any) -> Tuple[str, str]:
|
||||
def handle_subscribe_confirm(payload: Any) -> List[Tuple[str, str]]:
|
||||
message = payload.get("Message")
|
||||
url = payload.get("SubscribeURL")
|
||||
plain = f"{message}\n\n{url}"
|
||||
return plain, plain
|
||||
return [(plain, plain)]
|
||||
|
||||
|
||||
def handle_notification(payload: Any) -> Tuple[str, str]:
|
||||
def handle_notification(payload: Any) -> List[Tuple[str, str]]:
|
||||
message = payload.get("Message")
|
||||
subject = payload.get("Subject")
|
||||
|
||||
|
|
@ -20,15 +23,15 @@ def handle_notification(payload: Any) -> Tuple[str, str]:
|
|||
formatted = (
|
||||
f"<strong><font color={COLOR_ALARM}>{subject}</font></strong>\n<p>{message}</p>"
|
||||
)
|
||||
return plain, formatted
|
||||
return [(plain, formatted)]
|
||||
|
||||
|
||||
def handle_json_notification(payload: Any, body: Any) -> Tuple[str, str]:
|
||||
def handle_json_notification(payload: Any, body: Any) -> List[Tuple[str, str]]:
|
||||
if "AlarmName" not in body:
|
||||
msg = "Received unknown json payload type over AWS SNS"
|
||||
logging.info(msg)
|
||||
logging.info(payload.get("Message"))
|
||||
return msg, msg
|
||||
return [(msg, msg)]
|
||||
|
||||
description = body.get("AlarmDescription")
|
||||
subject = payload.get("Subject")
|
||||
|
|
@ -50,10 +53,14 @@ def handle_json_notification(payload: Any, body: Any) -> Tuple[str, str]:
|
|||
else:
|
||||
plain += "\n{description}"
|
||||
formatted += f"\n<p>{description}</p>"
|
||||
return plain, formatted
|
||||
return [(plain, formatted)]
|
||||
|
||||
|
||||
def parse_sns_event(payload: Any) -> Tuple[str, str]:
|
||||
async def parse_sns_event(
|
||||
route: RoutingKey,
|
||||
payload: Any,
|
||||
request: Request,
|
||||
) -> List[Tuple[str, str]]:
|
||||
if payload.get("Type") == "SubscriptionConfirmation":
|
||||
return handle_subscribe_confirm(payload)
|
||||
elif payload.get("Type") == "UnsubscribeConfirmation":
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue