Skip to content
/ airflow Public
forked from apache/airflow

Commit 0ac3b8c

Browse files
authored
Return slack api call response in slack_hook (apache#21107)
1 parent cb73053 commit 0ac3b8c

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

airflow/providers/slack/hooks/slack.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from typing import Any, Optional
2020

2121
from slack_sdk import WebClient
22+
from slack_sdk.web.slack_response import SlackResponse
2223

2324
from airflow.exceptions import AirflowException
2425
from airflow.hooks.base import BaseHook
@@ -78,7 +79,7 @@ def __get_token(self, token: Any, slack_conn_id: Any) -> str:
7879

7980
raise AirflowException('Cannot get token: No valid Slack token nor slack_conn_id supplied.')
8081

81-
def call(self, api_method: str, **kwargs) -> None:
82+
def call(self, api_method: str, **kwargs) -> SlackResponse:
8283
"""
8384
Calls Slack WebClient `WebClient.api_call` with given arguments.
8485
@@ -89,5 +90,8 @@ def call(self, api_method: str, **kwargs) -> None:
8990
form-encoding will take place. Optional.
9091
:param params: The URL parameters to append to the URL. Optional.
9192
:param json: JSON for the body to attach to the request. Optional.
93+
:return: The server's response to an HTTP request. Data from the response can be
94+
accessed like a dict. If the response included 'next_cursor' it can be
95+
iterated on to execute subsequent requests.
9296
"""
93-
self.client.api_call(api_method, **kwargs)
97+
return self.client.api_call(api_method, **kwargs)

docs/spelling_wordlist.txt

+1
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ Seedlist
333333
Sendgrid
334334
SerializedDAG
335335
SlackHook
336+
SlackResponse
336337
SnowflakeHook
337338
Spark
338339
SparkPi

tests/providers/slack/hooks/test_slack.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,14 @@ def test_call_with_failure(self, slack_client_class_mock):
122122
with pytest.raises(SlackApiError):
123123
slack_hook.call(test_method, data=test_api_params)
124124

125-
@mock.patch('airflow.providers.slack.hooks.slack.WebClient.api_call', autospec=True)
126125
@mock.patch('airflow.providers.slack.hooks.slack.WebClient')
127-
def test_api_call(self, mock_slack_client, mock_slack_api_call):
126+
def test_api_call(self, slack_client_class_mock):
127+
slack_client_mock = mock.Mock()
128+
slack_client_class_mock.return_value = slack_client_mock
129+
slack_client_mock.api_call.return_value = {'ok': True}
130+
128131
slack_hook = SlackHook(token='test_token')
129132
test_api_json = {'channel': 'test_channel'}
130133

131134
slack_hook.call("chat.postMessage", json=test_api_json)
132-
mock_slack_api_call.assert_called_once_with(mock_slack_client, "chat.postMessage", json=test_api_json)
135+
slack_client_mock.api_call.assert_called_with("chat.postMessage", json=test_api_json)

0 commit comments

Comments
 (0)