Taragolis commented on code in PR #25852:
URL: https://github.com/apache/airflow/pull/25852#discussion_r951115925
##########
airflow/providers/slack/hooks/slack.py:
##########
@@ -45,28 +65,124 @@ class SlackHook(BaseHook):
# For more details check
https://slack.dev/python-slack-sdk/web/index.html#messaging
slack_hook.client.chat_postMessage(channel="#random", text="Hello
world!")
- :param token: Slack API token
:param slack_conn_id: :ref:`Slack connection id <howto/connection:slack>`
that has Slack API token in the password field.
- :param use_session: A boolean specifying if the client should take
advantage of
- connection pooling. Default is True.
- :param base_url: A string representing the Slack API base URL. Default is
- ``https://www.slack.com/api/``
- :param timeout: The maximum number of seconds the client will wait
- to connect and receive a response from Slack. Default is 30 seconds.
+ :param timeout: The maximum number of seconds the client will wait to
connect
+ and receive a response from Slack. If not set than default WebClient
value will use.
+ :param base_url: A string representing the Slack API base URL.
+ If not set than default WebClient BASE_URL will use
(``https://www.slack.com/api/``).
+ :param proxy: Proxy to make the Slack Incoming Webhook call.
+ :param retry_handlers: List of handlers to customize retry logic in
WebClient.
+ :param token: (deprecated) Slack API Token.
"""
+ conn_name_attr = 'slack_conn_id'
+ default_conn_name = 'slack_api_default'
+ conn_type = 'slack'
+ hook_name = 'Slack API'
+
def __init__(
self,
token: Optional[str] = None,
slack_conn_id: Optional[str] = None,
- **client_args: Any,
+ base_url: Optional[str] = None,
+ timeout: Optional[int] = None,
+ proxy: Optional[str] = None,
+ retry_handlers: Optional[List["RetryHandler"]] = None,
+ **extra_client_args: Any,
) -> None:
+ if not token and not slack_conn_id:
+ raise AirflowException("Either `slack_conn_id` or `token` should
be provided.")
+
super().__init__()
- self.token = self.__get_token(token, slack_conn_id)
- self.client = WebClient(self.token, **client_args)
+ self.slack_conn_id = slack_conn_id
+ if not self.slack_conn_id:
+ warnings.warn(
+ "You have not set parameter `slack_conn_id`. Currently `Slack
API` connection id optional "
+ "but in a future release it will mandatory.",
+ FutureWarning,
+ stacklevel=2,
+ )
+ self.base_url = base_url
+ self.timeout = timeout
+ self.proxy = proxy
+ self.retry_handlers = retry_handlers
+ self.extra_client_args = extra_client_args
+ if self.extra_client_args.pop("use_session", None) is not None:
+ warnings.warn("`use_session` has no affect in
slack_sdk.WebClient.", UserWarning, stacklevel=2)
+
+ self._token = token
+ if self._token:
+ warnings.warn(
+ "Provide token as hook argument deprecated by security reason
and will be removed "
+ "in a future releases. Please specify token in `Slack API`
connection.",
+ DeprecationWarning,
+ stacklevel=2,
+ )
+
+ @cached_property
+ def client(self) -> WebClient:
+ """Get the underlying slack_sdk.WebClient (cached)."""
+ return WebClient(**self._get_conn_params())
+
+ def get_conn(self) -> WebClient:
+ """Get the underlying slack_sdk.WebClient (cached)."""
+ return self.client
+
+ def _get_conn_params(self) -> Dict[str, Any]:
+ """Fetch connection params as a dict and merge it with hook
parameters."""
+ conn = self.get_connection(self.slack_conn_id) if self.slack_conn_id
else None
+ conn_params: Dict[str, Any] = {}
+
+ if self._token:
+ conn_params["token"] = self._token
+ elif conn:
+ if not conn.password:
+ raise AirflowNotFoundException(
+ f"Connection ID {self.slack_conn_id!r} does not contain
password (Slack API Token)."
+ )
+ conn_params["token"] = conn.password
+
+ extra_config = ConnectionExtraConfig(
+ conn_type=self.conn_type,
+ conn_id=conn.conn_id if conn else None,
+ extra=conn.extra_dejson if conn else {},
+ )
+
+ # Merge Hook parameters with Connection config
+ conn_params.update(
+ {
+ "timeout": self.timeout or extra_config.getint("timeout",
default=None),
+ "base_url": self.base_url or extra_config.get("base_url",
default=None),
+ "proxy": self.proxy or extra_config.get("proxy", default=None),
+ "retry_handlers": (
+ self.retry_handlers or
extra_config.getimports("retry_handlers", default=None)
+ ),
+ }
+ )
+
+ # Add additional client args
+ conn_params.update(self.extra_client_args)
+ if "logger" not in conn_params:
+ conn_params["logger"] = self.log
+
+ return {k: v for k, v in conn_params.items() if v is not None}
+
+ @cached_property
+ def token(self) -> str:
+ warnings.warn(
+ "`SlackHook.token` property deprecated and will be removed in a
future releases.",
+ DeprecationWarning,
+ stacklevel=2,
+ )
+ return self._get_conn_params()["token"]
def __get_token(self, token: Any, slack_conn_id: Any) -> str:
+ warnings.warn(
+ "`SlackHook.__get_token` method deprecated and will be removed in
a future releases.",
+ DeprecationWarning,
+ stacklevel=2,
+ )
Review Comment:
Also might be better just remove this method.
It not used after changes and it is hardly possible that someone override it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]