Skip to content

Commit

Permalink
Make Amazon Chime connection lazy loaded and consistent with docs (#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
Taragolis committed Sep 1, 2023
1 parent 9144308 commit c4967b0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
19 changes: 12 additions & 7 deletions airflow/providers/amazon/aws/hooks/chime.py
Expand Up @@ -21,26 +21,27 @@

import json
import re
from functools import cached_property
from typing import Any

from airflow.exceptions import AirflowException
from airflow.providers.http.hooks.http import HttpHook


class ChimeWebhookHook(HttpHook):
"""Interact with Chime Web Hooks to create notifications.
"""Interact with Amazon Chime Webhooks to create notifications.
.. warning:: This hook is only designed to work with web hooks and not chat bots.
:param chime_conn_id: Chime connection ID with Endpoint as "https://hooks.chime.aws" and
the webhook token in the form of ```{webhook.id}?token{webhook.token}```
:param chime_conn_id: :ref:`Amazon Chime Connection ID <howto/connection:chime>`
with Endpoint as `https://hooks.chime.aws` and the webhook token
in the form of ``{webhook.id}?token{webhook.token}``
"""

conn_name_attr = "chime_conn_id"
default_conn_name = "chime_default"
conn_type = "chime"
hook_name = "Chime Web Hook"
hook_name = "Amazon Chime Webhook"

def __init__(
self,
Expand All @@ -49,7 +50,11 @@ def __init__(
**kwargs: Any,
) -> None:
super().__init__(*args, **kwargs)
self.webhook_endpoint = self._get_webhook_endpoint(chime_conn_id)
self._chime_conn_id = chime_conn_id

@cached_property
def webhook_endpoint(self):
return self._get_webhook_endpoint(self._chime_conn_id)

def _get_webhook_endpoint(self, conn_id: str) -> str:
"""
Expand Down Expand Up @@ -104,7 +109,7 @@ def get_ui_field_behaviour(cls) -> dict[str, Any]:
"hidden_fields": ["login", "port", "extra"],
"relabeling": {
"host": "Chime Webhook Endpoint",
"password": "Webhook Token",
"password": "Chime Webhook token",
},
"placeholders": {
"schema": "https",
Expand Down
3 changes: 2 additions & 1 deletion tests/providers/amazon/aws/hooks/test_chime.py
Expand Up @@ -66,8 +66,9 @@ def test_get_webhook_endpoint_invalid_url(self):

# When/Then
expected_message = r"Expected Chime webhook token in the form"
hook = ChimeWebhookHook(chime_conn_id="chime-bad-url")
with pytest.raises(AirflowException, match=expected_message):
ChimeWebhookHook(chime_conn_id="chime-bad-url")
assert not hook.webhook_endpoint

def test_get_webhook_endpoint_conn_id(self):
# Given
Expand Down

0 comments on commit c4967b0

Please sign in to comment.