Skip to content

Commit d679144

Browse files
authored
Fix #1102 Add __contains__ method in slack_sdk.web.SlackResponse / AsyncSlackResponse (#1104)
1 parent c23312c commit d679144

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

slack/web/async_slack_response.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ def __str__(self):
8080
)
8181
return f"{self.data}"
8282

83+
def __contains__(self, key: str) -> bool:
84+
return self.get(key) is not None
85+
8386
def __getitem__(self, key):
8487
"""Retrieves any key from the data store.
8588

slack_sdk/web/async_slack_response.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ def __str__(self):
8181
)
8282
return f"{self.data}"
8383

84+
def __contains__(self, key: str) -> bool:
85+
return self.get(key) is not None
86+
8487
def __getitem__(self, key):
8588
"""Retrieves any key from the data store.
8689

slack_sdk/web/slack_response.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ def __str__(self):
8181
)
8282
return f"{self.data}"
8383

84+
def __contains__(self, key: str) -> bool:
85+
return self.get(key) is not None
86+
8487
def __getitem__(self, key):
8588
"""Retrieves any key from the data store.
8689

tests/slack_sdk/web/test_slack_response.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,17 @@ def test_issue_1100(self):
4343

4444
foo = response.get("foo")
4545
self.assertIsNone(foo)
46+
47+
# https://github.com/slackapi/python-slack-sdk/issues/1102
48+
def test_issue_1102(self):
49+
response = SlackResponse(
50+
client=WebClient(token="xoxb-dummy"),
51+
http_verb="POST",
52+
api_url="http://localhost:3000/api.test",
53+
req_args={},
54+
data={"ok": True, "args": {"hello": "world"}},
55+
headers={},
56+
status_code=200,
57+
)
58+
self.assertTrue("ok" in response)
59+
self.assertTrue("foo" not in response)

tests/slack_sdk_async/web/test_async_slack_response.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from slack.web.async_slack_response import AsyncSlackResponse
3+
from slack_sdk.web.async_slack_response import AsyncSlackResponse
44
from slack_sdk.web.async_client import AsyncWebClient
55

66

@@ -27,3 +27,17 @@ def test_issue_1100(self):
2727

2828
foo = response.get("foo")
2929
self.assertIsNone(foo)
30+
31+
# https://github.com/slackapi/python-slack-sdk/issues/1102
32+
def test_issue_1102(self):
33+
response = AsyncSlackResponse(
34+
client=AsyncWebClient(token="xoxb-dummy"),
35+
http_verb="POST",
36+
api_url="http://localhost:3000/api.test",
37+
req_args={},
38+
data={"ok": True, "args": {"hello": "world"}},
39+
headers={},
40+
status_code=200,
41+
)
42+
self.assertTrue("ok" in response)
43+
self.assertTrue("foo" not in response)

0 commit comments

Comments
 (0)