Docstrings and typing

This commit is contained in:
Pablo Martin 2022-12-30 11:35:31 +01:00
parent 028f181ba8
commit 5e13cac9d7

View file

@ -1,17 +1,35 @@
import json
from prefect.core import Task
from prefect.triggers import any_failed
import requests
class SendSlackMessageTask(Task):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def run(self, webhook_url:, text_to_send) -> None:
def run(self, webhook_url: str, text_to_send: str) -> None:
"""
Pass the details from the task to the actual function.
:param webhook_url: the URL of the Slack webhook that should receive the
message.
:param text_to_send: the text to send to the Slack channel.
:return: None
"""
send_message_to_slack_channel(webhook_url, text_to_send)
def send_message_to_slack_channel(webhook_url, text_to_send):
def send_message_to_slack_channel(webhook_url: str, text_to_send: str) -> None:
"""
Send an HTTP POST to a Slack webhook to deliver a message in a channel. Raise
an error if Slack does not reply with a 200 OK status.
:param webhook_url: the URL of the Slack webhook that should receive the
message.
:param text_to_send: the text to send to the Slack channel.
:return: None
"""
slack_data = {"text": text_to_send}
response = requests.post(
@ -24,7 +42,3 @@ def send_message_to_slack_channel(webhook_url, text_to_send):
"Request to slack returned an error %s, the response is:\n%s"
% (response.status_code, response.text)
)
send_message_to_slack_channel_on_upstream_all_successful = SendSlackMessageTask()
send_error_warning_to_slack = SendSlackMessageTask(trigger=any_failed)