2021-04-15 23:39:16 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2021-07-16 22:11:10 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2022-11-17 09:30:48 +01:00
|
|
|
from zerver.lib.exceptions import UnsupportedWebhookEventTypeError
|
2021-04-15 23:39:16 +02:00
|
|
|
from zerver.lib.response import json_success
|
2023-09-27 19:01:31 +02:00
|
|
|
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
|
2023-08-12 09:34:31 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_int, check_string
|
2021-04-15 23:39:16 +02:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message, get_setup_webhook_message
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
FRESHPING_TOPIC_TEMPLATE_TEST = "Freshping"
|
|
|
|
FRESHPING_TOPIC_TEMPLATE = "{check_name}"
|
|
|
|
|
|
|
|
FRESHPING_MESSAGE_TEMPLATE_UNREACHABLE = """
|
|
|
|
{request_url} has just become unreachable.
|
2021-05-10 07:02:14 +02:00
|
|
|
Error code: {http_status_code}.
|
2021-04-15 23:39:16 +02:00
|
|
|
""".strip()
|
|
|
|
FRESHPING_MESSAGE_TEMPLATE_UP = "{request_url} is back up and no longer unreachable."
|
2022-08-16 21:26:56 +02:00
|
|
|
CHECK_STATE_NAME_TO_EVENT_TYPE = {"Reporting Error": "reporting_error", "Available": "available"}
|
|
|
|
ALL_EVENT_TYPES = list(CHECK_STATE_NAME_TO_EVENT_TYPE.values())
|
2021-04-15 23:39:16 +02:00
|
|
|
|
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
@webhook_view("Freshping", all_event_types=ALL_EVENT_TYPES)
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2021-04-15 23:39:16 +02:00
|
|
|
def api_freshping_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:34:31 +02:00
|
|
|
*,
|
2023-09-27 19:01:31 +02:00
|
|
|
payload: JsonBodyPayload[WildValue],
|
2021-04-15 23:39:16 +02:00
|
|
|
) -> HttpResponse:
|
2022-08-16 21:26:56 +02:00
|
|
|
check_state_name = payload["webhook_event_data"]["check_state_name"].tame(check_string)
|
|
|
|
if check_state_name not in CHECK_STATE_NAME_TO_EVENT_TYPE:
|
2022-11-17 09:30:48 +01:00
|
|
|
raise UnsupportedWebhookEventTypeError(check_state_name)
|
2023-10-11 19:59:37 +02:00
|
|
|
body = get_body_for_http_request(payload)
|
|
|
|
topic = get_topic_for_http_request(payload)
|
2021-04-15 23:39:16 +02:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
check_send_webhook_message(
|
2022-04-02 10:34:26 +02:00
|
|
|
request,
|
|
|
|
user_profile,
|
2023-07-12 13:37:08 +02:00
|
|
|
topic,
|
2022-04-02 10:34:26 +02:00
|
|
|
body,
|
2022-08-16 21:26:56 +02:00
|
|
|
CHECK_STATE_NAME_TO_EVENT_TYPE[check_state_name],
|
2021-07-16 11:40:46 +02:00
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2021-04-15 23:39:16 +02:00
|
|
|
|
|
|
|
|
2023-07-12 13:37:08 +02:00
|
|
|
def get_topic_for_http_request(payload: WildValue) -> str:
|
2021-04-15 23:39:16 +02:00
|
|
|
webhook_event_data = payload["webhook_event_data"]
|
2022-04-02 10:34:26 +02:00
|
|
|
if webhook_event_data["application_name"].tame(check_string) == "Webhook test":
|
2023-07-12 13:37:08 +02:00
|
|
|
topic = FRESHPING_TOPIC_TEMPLATE_TEST
|
2021-04-15 23:39:16 +02:00
|
|
|
else:
|
2023-07-12 13:37:08 +02:00
|
|
|
topic = FRESHPING_TOPIC_TEMPLATE.format(
|
2022-04-02 10:34:26 +02:00
|
|
|
check_name=webhook_event_data["check_name"].tame(check_string)
|
|
|
|
)
|
2023-07-12 13:37:08 +02:00
|
|
|
return topic
|
2021-04-15 23:39:16 +02:00
|
|
|
|
|
|
|
|
2022-04-02 10:34:26 +02:00
|
|
|
def get_body_for_http_request(payload: WildValue) -> str:
|
2021-04-15 23:39:16 +02:00
|
|
|
webhook_event_data = payload["webhook_event_data"]
|
2022-04-02 10:34:26 +02:00
|
|
|
if webhook_event_data["check_state_name"].tame(check_string) == "Reporting Error":
|
|
|
|
body = FRESHPING_MESSAGE_TEMPLATE_UNREACHABLE.format(
|
|
|
|
request_url=webhook_event_data["request_url"].tame(check_string),
|
|
|
|
http_status_code=webhook_event_data["http_status_code"].tame(check_int),
|
|
|
|
)
|
|
|
|
elif webhook_event_data["check_state_name"].tame(check_string) == "Available":
|
|
|
|
if webhook_event_data["application_name"].tame(check_string) == "Webhook test":
|
2021-04-15 23:39:16 +02:00
|
|
|
body = get_setup_webhook_message("Freshping")
|
|
|
|
else:
|
2022-04-02 10:34:26 +02:00
|
|
|
body = FRESHPING_MESSAGE_TEMPLATE_UP.format(
|
|
|
|
request_url=webhook_event_data["request_url"].tame(check_string)
|
|
|
|
)
|
2021-04-15 23:39:16 +02:00
|
|
|
|
|
|
|
return body
|