Skip to content

Commit

Permalink
Fix slackapi#650 Deprecation warnings to channels/groups/mpim/im API …
Browse files Browse the repository at this point in the history
…method calls
  • Loading branch information
seratch committed Apr 28, 2020
1 parent 613f3f7 commit 40ae3b1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
23 changes: 23 additions & 0 deletions slack/web/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import platform
import sys
import warnings

import slack.version as ver

Expand All @@ -18,3 +19,25 @@ def get_user_agent():
system_info = "{0}/{1}".format(platform.system(), platform.release())
user_agent_string = " ".join([python_version, client, system_info])
return user_agent_string


# https://api.slack.com/changelog/2020-01-deprecating-antecedents-to-the-conversations-api
deprecated_method_prefixes_2020_01 = ["channels.", "groups.", "im.", "mpim."]


def show_2020_01_deprecation(method_name: str):
if not method_name:
return

matched_prefixes = [
prefix
for prefix in deprecated_method_prefixes_2020_01
if method_name.startswith(prefix)
]
if len(matched_prefixes) > 0:
message = (
f"{method_name} is deprecated. Please use the Conversations API instead. "
f"For more info, go to "
f"https://api.slack.com/changelog/2020-01-deprecating-antecedents-to-the-conversations-api"
)
warnings.warn(message)
5 changes: 4 additions & 1 deletion slack/web/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from aiohttp import FormData, BasicAuth

# Internal Imports
from slack.web import get_user_agent
from slack.web import get_user_agent, show_2020_01_deprecation
from slack.web.slack_response import SlackResponse
import slack.errors as err
from slack.web.urllib_client import UrllibWebClient
Expand Down Expand Up @@ -167,6 +167,9 @@ def api_call(
}

if self.run_async or self.use_sync_aiohttp:
# NOTE: For sync mode client, show_2020_01_deprecation(str) is called inside UrllibClient
show_2020_01_deprecation(api_method)

if self._event_loop is None:
self._event_loop = self._get_event_loop()

Expand Down
4 changes: 3 additions & 1 deletion slack/web/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,9 @@ def admin_users_invite(
channel_ids (list): A list of channel_ids for this user to join.
At least one channel is required. e.g. ['C1A2B3C4D', 'C26Z25Y24']
"""
kwargs.update({"team_id": team_id, "email": email, "channel_ids": ",".join(channel_ids)})
kwargs.update(
{"team_id": team_id, "email": email, "channel_ids": ",".join(channel_ids)}
)
return self.api_call("admin.users.invite", json=kwargs)

def admin_users_list(
Expand Down
12 changes: 11 additions & 1 deletion slack/web/urllib_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from urllib.parse import urlencode
from urllib.request import Request, urlopen

from slack.web import get_user_agent
from slack.web import get_user_agent, show_2020_01_deprecation
from slack.web.slack_response import SlackResponse


Expand Down Expand Up @@ -63,6 +63,9 @@ def api_call(
:param additional_headers: request headers to append
:return: API response
"""

show_2020_01_deprecation(self._to_api_method(url))

files_to_close: List[BinaryIO] = []
try:
if self.logger.level <= logging.DEBUG:
Expand Down Expand Up @@ -243,3 +246,10 @@ def _build_request_headers(
# will be set afterwards
headers.pop("Content-Type", None)
return headers

def _to_api_method(self, url: str):
if url:
elements = url.split("/")
if elements and len(elements) > 0:
return elements[len(elements) - 1].split("?", 1)[0]
return None

0 comments on commit 40ae3b1

Please sign in to comment.