Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions slack/web/async_slack_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ def __init__(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For backward compatibility, we have two AsynSlackResponse under slack and slack_sdk packages. We have to apply the same changes to the two this time.

def __str__(self):
"""Return the Response data if object is converted to a string."""
if isinstance(self.data, bytes):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While SlackResponse has this validation, the same thing has been missing in slack.web.AsyncSlackResponse (legacy one).

raise ValueError(
"As the response.data is binary data, this operation is unsupported"
)
return f"{self.data}"

def __getitem__(self, key):
Expand All @@ -87,6 +91,14 @@ def __getitem__(self, key):
Returns:
The value from data or None.
"""
if isinstance(self.data, bytes):
raise ValueError(
"As the response.data is binary data, this operation is unsupported"
)
if self.data is None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is added for resolving the issue #1100

raise ValueError(
"As the response.data is empty, this operation is unsupported"
)
return self.data.get(key, None)

def __aiter__(self):
Expand Down Expand Up @@ -156,6 +168,12 @@ def get(self, key, default=None):
Returns:
The value from data or the specified default.
"""
if isinstance(self.data, bytes):
raise ValueError(
"As the response.data is binary data, this operation is unsupported"
)
if self.data is None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get method should return None even if the data is None in the same manner with dictionary objects.

return None
return self.data.get(key, default)

def validate(self):
Expand All @@ -168,13 +186,6 @@ def validate(self):
Raises:
SlackApiError: The request to the Slack API failed.
"""
if self._logger.level <= logging.DEBUG:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have been removed in #1094

self._logger.debug(
"Received the following response - "
f"status: {self.status_code}, "
f"headers: {dict(self.headers)}, "
f"body: {self.data}"
)
if self.status_code == 200 and self.data and self.data.get("ok", False):
return self
msg = "The request to the Slack API failed."
Expand Down
14 changes: 6 additions & 8 deletions slack_sdk/web/async_slack_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ def __getitem__(self, key):
raise ValueError(
"As the response.data is binary data, this operation is unsupported"
)
if self.data is None:
raise ValueError(
"As the response.data is empty, this operation is unsupported"
)
return self.data.get(key, None)

def __aiter__(self):
Expand Down Expand Up @@ -177,6 +181,8 @@ def get(self, key, default=None):
raise ValueError(
"As the response.data is binary data, this operation is unsupported"
)
if self.data is None:
return None
return self.data.get(key, default)

def validate(self):
Expand All @@ -189,14 +195,6 @@ def validate(self):
Raises:
SlackApiError: The request to the Slack API failed.
"""
if self._logger.level <= logging.DEBUG:
body = self.data if isinstance(self.data, dict) else "(binary)"
self._logger.debug(
"Received the following response - "
f"status: {self.status_code}, "
f"headers: {dict(self.headers)}, "
f"body: {body}"
)
if (
self.status_code == 200
and self.data
Expand Down
6 changes: 6 additions & 0 deletions slack_sdk/web/slack_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ def __getitem__(self, key):
raise ValueError(
"As the response.data is binary data, this operation is unsupported"
)
if self.data is None:
raise ValueError(
"As the response.data is empty, this operation is unsupported"
)
return self.data.get(key, None)

def __iter__(self):
Expand Down Expand Up @@ -174,6 +178,8 @@ def get(self, key, default=None):
raise ValueError(
"As the response.data is binary data, this operation is unsupported"
)
if self.data is None:
return None
return self.data.get(key, default)

def validate(self):
Expand Down
17 changes: 17 additions & 0 deletions tests/slack_sdk/web/test_slack_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,20 @@ def test_issue_559(self):
self.assertTrue("ok" in response.data)
self.assertTrue("args" in response.data)
self.assertFalse("error" in response.data)

# https://github.com/slackapi/python-slack-sdk/issues/1100
def test_issue_1100(self):
response = SlackResponse(
client=WebClient(token="xoxb-dummy"),
http_verb="POST",
api_url="http://localhost:3000/api.test",
req_args={},
data=None,
headers={},
status_code=200,
)
with self.assertRaises(ValueError):
response["foo"]

foo = response.get("foo")
self.assertIsNone(foo)
29 changes: 29 additions & 0 deletions tests/slack_sdk_async/web/test_async_slack_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import unittest

from slack.web.async_slack_response import AsyncSlackResponse
from slack_sdk.web.async_client import AsyncWebClient


class TestAsyncSlackResponse(unittest.TestCase):
def setUp(self):
pass

def tearDown(self):
pass

# https://github.com/slackapi/python-slack-sdk/issues/1100
def test_issue_1100(self):
response = AsyncSlackResponse(
client=AsyncWebClient(token="xoxb-dummy"),
http_verb="POST",
api_url="http://localhost:3000/api.test",
req_args={},
data=None,
headers={},
status_code=200,
)
with self.assertRaises(ValueError):
response["foo"]

foo = response.get("foo")
self.assertIsNone(foo)