Skip to content

Commit c23312c

Browse files
authored
Fix #1100 by improving the get/__getitem__ method behavior when response body is empty (#1103)
1 parent 86faa33 commit c23312c

File tree

5 files changed

+76
-15
lines changed

5 files changed

+76
-15
lines changed

slack/web/async_slack_response.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ def __init__(
7474

7575
def __str__(self):
7676
"""Return the Response data if object is converted to a string."""
77+
if isinstance(self.data, bytes):
78+
raise ValueError(
79+
"As the response.data is binary data, this operation is unsupported"
80+
)
7781
return f"{self.data}"
7882

7983
def __getitem__(self, key):
@@ -87,6 +91,14 @@ def __getitem__(self, key):
8791
Returns:
8892
The value from data or None.
8993
"""
94+
if isinstance(self.data, bytes):
95+
raise ValueError(
96+
"As the response.data is binary data, this operation is unsupported"
97+
)
98+
if self.data is None:
99+
raise ValueError(
100+
"As the response.data is empty, this operation is unsupported"
101+
)
90102
return self.data.get(key, None)
91103

92104
def __aiter__(self):
@@ -156,6 +168,12 @@ def get(self, key, default=None):
156168
Returns:
157169
The value from data or the specified default.
158170
"""
171+
if isinstance(self.data, bytes):
172+
raise ValueError(
173+
"As the response.data is binary data, this operation is unsupported"
174+
)
175+
if self.data is None:
176+
return None
159177
return self.data.get(key, default)
160178

161179
def validate(self):
@@ -168,13 +186,6 @@ def validate(self):
168186
Raises:
169187
SlackApiError: The request to the Slack API failed.
170188
"""
171-
if self._logger.level <= logging.DEBUG:
172-
self._logger.debug(
173-
"Received the following response - "
174-
f"status: {self.status_code}, "
175-
f"headers: {dict(self.headers)}, "
176-
f"body: {self.data}"
177-
)
178189
if self.status_code == 200 and self.data and self.data.get("ok", False):
179190
return self
180191
msg = "The request to the Slack API failed."

slack_sdk/web/async_slack_response.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ def __getitem__(self, key):
9696
raise ValueError(
9797
"As the response.data is binary data, this operation is unsupported"
9898
)
99+
if self.data is None:
100+
raise ValueError(
101+
"As the response.data is empty, this operation is unsupported"
102+
)
99103
return self.data.get(key, None)
100104

101105
def __aiter__(self):
@@ -177,6 +181,8 @@ def get(self, key, default=None):
177181
raise ValueError(
178182
"As the response.data is binary data, this operation is unsupported"
179183
)
184+
if self.data is None:
185+
return None
180186
return self.data.get(key, default)
181187

182188
def validate(self):
@@ -189,14 +195,6 @@ def validate(self):
189195
Raises:
190196
SlackApiError: The request to the Slack API failed.
191197
"""
192-
if self._logger.level <= logging.DEBUG:
193-
body = self.data if isinstance(self.data, dict) else "(binary)"
194-
self._logger.debug(
195-
"Received the following response - "
196-
f"status: {self.status_code}, "
197-
f"headers: {dict(self.headers)}, "
198-
f"body: {body}"
199-
)
200198
if (
201199
self.status_code == 200
202200
and self.data

slack_sdk/web/slack_response.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ def __getitem__(self, key):
9696
raise ValueError(
9797
"As the response.data is binary data, this operation is unsupported"
9898
)
99+
if self.data is None:
100+
raise ValueError(
101+
"As the response.data is empty, this operation is unsupported"
102+
)
99103
return self.data.get(key, None)
100104

101105
def __iter__(self):
@@ -174,6 +178,8 @@ def get(self, key, default=None):
174178
raise ValueError(
175179
"As the response.data is binary data, this operation is unsupported"
176180
)
181+
if self.data is None:
182+
return None
177183
return self.data.get(key, default)
178184

179185
def validate(self):

tests/slack_sdk/web/test_slack_response.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,20 @@ def test_issue_559(self):
2626
self.assertTrue("ok" in response.data)
2727
self.assertTrue("args" in response.data)
2828
self.assertFalse("error" in response.data)
29+
30+
# https://github.com/slackapi/python-slack-sdk/issues/1100
31+
def test_issue_1100(self):
32+
response = SlackResponse(
33+
client=WebClient(token="xoxb-dummy"),
34+
http_verb="POST",
35+
api_url="http://localhost:3000/api.test",
36+
req_args={},
37+
data=None,
38+
headers={},
39+
status_code=200,
40+
)
41+
with self.assertRaises(ValueError):
42+
response["foo"]
43+
44+
foo = response.get("foo")
45+
self.assertIsNone(foo)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import unittest
2+
3+
from slack.web.async_slack_response import AsyncSlackResponse
4+
from slack_sdk.web.async_client import AsyncWebClient
5+
6+
7+
class TestAsyncSlackResponse(unittest.TestCase):
8+
def setUp(self):
9+
pass
10+
11+
def tearDown(self):
12+
pass
13+
14+
# https://github.com/slackapi/python-slack-sdk/issues/1100
15+
def test_issue_1100(self):
16+
response = AsyncSlackResponse(
17+
client=AsyncWebClient(token="xoxb-dummy"),
18+
http_verb="POST",
19+
api_url="http://localhost:3000/api.test",
20+
req_args={},
21+
data=None,
22+
headers={},
23+
status_code=200,
24+
)
25+
with self.assertRaises(ValueError):
26+
response["foo"]
27+
28+
foo = response.get("foo")
29+
self.assertIsNone(foo)

0 commit comments

Comments
 (0)