Skip to content

Commit

Permalink
Fix #1100 by improving the get/__getitem__ method behavior when respo…
Browse files Browse the repository at this point in the history
…nse body is empty (#1103)
  • Loading branch information
seratch committed Aug 20, 2021
1 parent 86faa33 commit c23312c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 15 deletions.
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__(

def __str__(self):
"""Return the Response data if object is converted to a string."""
if isinstance(self.data, bytes):
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:
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:
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:
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)

0 comments on commit c23312c

Please sign in to comment.