-
Notifications
You must be signed in to change notification settings - Fork 16.4k
HTTP Notifier implementation #56160
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
Merged
Merged
HTTP Notifier implementation #56160
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7361797
HTTP Notifier implementation
seanghaeli d9d9417
Re-run CI tests
seanghaeli 8a0566f
Register http notifier in yaml file
seanghaeli 3642091
remove unnecessary import
seanghaeli 67b5e33
re-add import
seanghaeli 3ee19e8
imports
seanghaeli 990f1d6
update imports
seanghaeli 77ca858
Declare imports in init file
seanghaeli ccd5ec2
precommit updates
seanghaeli a3b5a20
re-run CI
seanghaeli a4c36a7
pre-commit fixes
seanghaeli 2273245
re-run CI tests after 56733 has been merged
seanghaeli cab9d33
re-base precommit changes
seanghaeli d6b8606
rebase
seanghaeli d1b826b
import tests update
seanghaeli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
providers/http/src/airflow/providers/http/notifications/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| from airflow.providers.http.notifications.http import HttpNotifier | ||
|
|
||
| __all__ = ["HttpNotifier"] |
105 changes: 105 additions & 0 deletions
105
providers/http/src/airflow/providers/http/notifications/http.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from functools import cached_property | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| import aiohttp | ||
|
|
||
| from airflow.providers.common.compat.notifier import BaseNotifier | ||
| from airflow.providers.http.hooks.http import HttpAsyncHook, HttpHook | ||
|
|
||
| if TYPE_CHECKING: | ||
| from airflow.sdk.definitions.context import Context | ||
|
|
||
|
|
||
| class HttpNotifier(BaseNotifier): | ||
| """ | ||
| HTTP Notifier. | ||
|
|
||
| Sends HTTP requests to notify external systems. | ||
|
|
||
| :param http_conn_id: HTTP connection id that has the base URL and optional authentication credentials. | ||
| :param endpoint: The endpoint to be called i.e. resource/v1/query? | ||
| :param method: The HTTP method to use. Defaults to POST. | ||
| :param data: Payload to be uploaded or request parameters | ||
| :param json: JSON payload to be uploaded | ||
| :param headers: Additional headers to be passed through as a dictionary | ||
| :param extra_options: Additional options to be used when executing the request | ||
| """ | ||
|
|
||
| template_fields = ("http_conn_id", "endpoint", "data", "json", "headers", "extra_options") | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| http_conn_id: str = HttpHook.default_conn_name, | ||
| endpoint: str | None = None, | ||
| method: str = "POST", | ||
| data: dict[str, Any] | str | None = None, | ||
| json: dict[str, Any] | str | None = None, | ||
| headers: dict[str, Any] | None = None, | ||
| extra_options: dict[str, Any] | None = None, | ||
| **kwargs, | ||
| ): | ||
| super().__init__(**kwargs) | ||
| self.http_conn_id = http_conn_id | ||
| self.endpoint = endpoint | ||
| self.method = method | ||
| self.data = data | ||
| self.json = json | ||
| self.headers = headers | ||
| self.extra_options = extra_options or {} | ||
|
|
||
| @cached_property | ||
| def hook(self) -> HttpHook: | ||
| """HTTP Hook.""" | ||
| return HttpHook(method=self.method, http_conn_id=self.http_conn_id) | ||
|
|
||
| @cached_property | ||
| def async_hook(self) -> HttpAsyncHook: | ||
| """HTTP Async Hook.""" | ||
| return HttpAsyncHook(method=self.method, http_conn_id=self.http_conn_id) | ||
|
|
||
| def notify(self, context: Context) -> None: | ||
| """Send HTTP notification (sync).""" | ||
| resp = self.hook.run( | ||
| endpoint=self.endpoint, | ||
| data=self.data, | ||
| headers=self.headers, | ||
| extra_options=self.extra_options, | ||
| json=self.json, | ||
| ) | ||
| self.log.debug("HTTP notification sent: %s %s", resp.status_code, resp.url) | ||
|
|
||
| async def async_notify(self, context: Context) -> None: | ||
| """Send HTTP notification (async).""" | ||
| async with aiohttp.ClientSession() as session: | ||
ferruzzi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| resp = await self.async_hook.run( | ||
| session=session, | ||
| endpoint=self.endpoint, | ||
| data=self.data, | ||
| json=self.json, | ||
| headers=self.headers, | ||
| extra_options=self.extra_options, | ||
| ) | ||
| self.log.debug("HTTP notification sent (async): %s %s", resp.status, resp.url) | ||
|
|
||
|
|
||
| send_http_notification = HttpNotifier | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
| from airflow.providers.http.notifications.http import HttpNotifier, send_http_notification | ||
|
|
||
|
|
||
| class TestHttpNotifier: | ||
| def test_class_and_notifier_are_same(self): | ||
| assert send_http_notification is HttpNotifier | ||
|
|
||
| @mock.patch("airflow.providers.http.notifications.http.HttpHook") | ||
| def test_http_notifier(self, mock_http_hook): | ||
| notifier = HttpNotifier( | ||
| http_conn_id="test_conn_id", | ||
| endpoint="/testing", | ||
| method="POST", | ||
| json={"message": "testing"}, | ||
| headers={"Content-Type": "application/json"}, | ||
| ) | ||
| notifier.notify({}) | ||
|
|
||
| mock_http_hook.return_value.run.assert_called_once_with( | ||
| endpoint="/testing", | ||
| data=None, | ||
| headers={"Content-Type": "application/json"}, | ||
| extra_options={}, | ||
| json={"message": "testing"}, | ||
| ) | ||
| mock_http_hook.assert_called_once_with(method="POST", http_conn_id="test_conn_id") | ||
|
|
||
| @pytest.mark.asyncio | ||
| @mock.patch("airflow.providers.http.notifications.http.HttpAsyncHook") | ||
| @mock.patch("aiohttp.ClientSession") | ||
| async def test_async_http_notifier(self, mock_session, mock_http_async_hook): | ||
| mock_hook = mock_http_async_hook.return_value | ||
| mock_hook.run = mock.AsyncMock() | ||
|
|
||
| notifier = HttpNotifier( | ||
| http_conn_id="test_conn_id", | ||
| endpoint="/test", | ||
| method="POST", | ||
| json={"message": "test"}, | ||
| ) | ||
|
|
||
| await notifier.async_notify({}) | ||
|
|
||
| mock_hook.run.assert_called_once_with( | ||
| session=mock_session.return_value.__aenter__.return_value, | ||
| endpoint="/test", | ||
| data=None, | ||
| json={"message": "test"}, | ||
| headers=None, | ||
| extra_options={}, | ||
| ) | ||
|
|
||
| @mock.patch("airflow.providers.http.notifications.http.HttpHook") | ||
| def test_http_notifier_templated(self, mock_http_hook, create_dag_without_db): | ||
| notifier = HttpNotifier( | ||
| endpoint="/{{ dag.dag_id }}", | ||
| json={"dag_id": "{{ dag.dag_id }}", "user": "{{ username }}"}, | ||
| ) | ||
| notifier( | ||
| { | ||
| "dag": create_dag_without_db("test_http_notification_templated"), | ||
| "username": "test-user", | ||
| } | ||
| ) | ||
|
|
||
| mock_http_hook.return_value.run.assert_called_once_with( | ||
| endpoint="/test_http_notification_templated", | ||
| data=None, | ||
| headers=None, | ||
| extra_options={}, | ||
| json={"dag_id": "test_http_notification_templated", "user": "test-user"}, | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.