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 May 11, 2020
1 parent af57ecd commit 17c77c0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 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
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 17c77c0

Please sign in to comment.