Skip to content

Commit dd0b15c

Browse files
authored
Merge branch 'main' into remove-unnecessary-kwargs-for-some-files-methods
2 parents e9a769e + c2b5c29 commit dd0b15c

9 files changed

Lines changed: 378 additions & 7 deletions

File tree

docs-src/web/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ This library covers all the public endpoints as the methods in ``WebClient``. Th
607607
client = WebClient(token=os.environ['SLACK_BOT_TOKEN'])
608608
response = client.api_call(
609609
api_method='chat.postMessage',
610-
json={'channel': '#random','text': "Hello world!"}
610+
params={'channel': '#random','text': "Hello world!"}
611611
)
612612
assert response["message"]["text"] == "Hello world!"
613613

docs/web/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ <h3>Calling any API methods<a class="headerlink" href="#calling-any-api-methods"
695695
<span class="n">client</span> <span class="o">=</span> <span class="n">WebClient</span><span class="p">(</span><span class="n">token</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;SLACK_BOT_TOKEN&#39;</span><span class="p">])</span>
696696
<span class="n">response</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">api_call</span><span class="p">(</span>
697697
<span class="n">api_method</span><span class="o">=</span><span class="s1">&#39;chat.postMessage&#39;</span><span class="p">,</span>
698-
<span class="n">json</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;channel&#39;</span><span class="p">:</span> <span class="s1">&#39;#random&#39;</span><span class="p">,</span><span class="s1">&#39;text&#39;</span><span class="p">:</span> <span class="s2">&quot;Hello world!&quot;</span><span class="p">}</span>
698+
<span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;channel&#39;</span><span class="p">:</span> <span class="s1">&#39;#random&#39;</span><span class="p">,</span><span class="s1">&#39;text&#39;</span><span class="p">:</span> <span class="s2">&quot;Hello world!&quot;</span><span class="p">}</span>
699699
<span class="p">)</span>
700700
<span class="k">assert</span> <span class="n">response</span><span class="p">[</span><span class="s2">&quot;message&quot;</span><span class="p">][</span><span class="s2">&quot;text&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;Hello world!&quot;</span>
701701
</pre></div>

setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
"black==21.7b0",
2929
"psutil>=5,<6",
3030
"databases>=0.3",
31+
# used only under slack_sdk/*_store
32+
"boto3<=2",
33+
# TODO: Upgrade to v2
34+
"moto<2", # For AWS tests
3135
]
3236
codegen_dependencies = [
3337
"black==21.7b0",

slack_sdk/errors/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,7 @@ class SlackObjectFormationError(SlackClientError):
5252

5353

5454
class SlackClientConfigurationError(SlackClientError):
55-
"""Error raised when attempting to send messages over the websocket when the
56-
connection is closed."""
55+
"""Error raised because of invalid configuration on the client side:
56+
* when attempting to send messages over the websocket when the connection is closed.
57+
* when external system (e.g., Amazon S3) configuration / credentials are not correct
58+
"""

slack_sdk/oauth/installation_store/amazon_s3/__init__.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from botocore.client import BaseClient
77

8+
from slack_sdk.errors import SlackClientConfigurationError
89
from slack_sdk.oauth.installation_store.async_installation_store import (
910
AsyncInstallationStore,
1011
)
@@ -252,7 +253,7 @@ def delete_bot(
252253
)
253254
except Exception as e: # skipcq: PYL-W0703
254255
message = f"Failed to find bot installation data for enterprise: {e_id}, team: {t_id}: {e}"
255-
self.logger.warning(message)
256+
raise SlackClientConfigurationError(message)
256257

257258
async def async_delete_installation(
258259
self,
@@ -282,15 +283,52 @@ def delete_installation(
282283
Bucket=self.bucket_name,
283284
Prefix=f"{workspace_path}/installer-{user_id or ''}",
284285
)
286+
deleted_keys = []
285287
for content in objects.get("Contents", []):
286288
key = content.get("Key")
287289
if key is not None:
288290
self.logger.info(f"Going to delete installation ({key})")
289291
try:
290292
self.s3_client.delete_object(
291293
Bucket=self.bucket_name,
292-
Key=content.get("Key"),
294+
Key=key,
293295
)
296+
deleted_keys.append(key)
297+
except Exception as e: # skipcq: PYL-W0703
298+
message = f"Failed to find bot installation data for enterprise: {e_id}, team: {t_id}: {e}"
299+
raise SlackClientConfigurationError(message)
300+
301+
try:
302+
no_user_id_key = key.replace(f"-{user_id}", "")
303+
if not no_user_id_key.endswith("installer-latest"):
304+
self.s3_client.delete_object(
305+
Bucket=self.bucket_name,
306+
Key=no_user_id_key,
307+
)
308+
deleted_keys.append(no_user_id_key)
294309
except Exception as e: # skipcq: PYL-W0703
295310
message = f"Failed to find bot installation data for enterprise: {e_id}, team: {t_id}: {e}"
296-
self.logger.warning(message)
311+
raise SlackClientConfigurationError(message)
312+
313+
# Check the remaining installation data
314+
objects = self.s3_client.list_objects(
315+
Bucket=self.bucket_name,
316+
Prefix=f"{workspace_path}/installer-",
317+
MaxKeys=10, # the small number would be enough for this purpose
318+
)
319+
keys = [
320+
c.get("Key")
321+
for c in objects.get("Contents", [])
322+
if c.get("Key") not in deleted_keys
323+
]
324+
# If only installer-latest remains, we should delete the one as well
325+
if len(keys) == 1 and keys[0].endswith("installer-latest"):
326+
content = objects.get("Contents", [])[0]
327+
try:
328+
self.s3_client.delete_object(
329+
Bucket=self.bucket_name,
330+
Key=content.get("Key"),
331+
)
332+
except Exception as e: # skipcq: PYL-W0703
333+
message = f"Failed to find bot installation data for enterprise: {e_id}, team: {t_id}: {e}"
334+
raise SlackClientConfigurationError(message)

tests/slack_sdk/audit_logs/mock_web_api_server.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ def do_GET(self):
4141
return
4242

4343
try:
44+
if self.path == "/error":
45+
self.send_response(500)
46+
self.set_common_headers()
47+
self.wfile.write("unexpected response body".encode("utf-8"))
48+
return
49+
4450
if self.path == "/timeout":
4551
time.sleep(2)
4652

tests/slack_sdk/audit_logs/test_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
from urllib.error import URLError
23

34
from slack_sdk.audit_logs import AuditLogsClient, AuditLogsResponse
45
from tests.slack_sdk.audit_logs.mock_web_api_server import (
@@ -31,3 +32,14 @@ def test_schemas(self):
3132
resp: AuditLogsResponse = self.client.schemas()
3233
self.assertEqual(200, resp.status_code)
3334
self.assertIsNotNone(resp.body.get("schemas"))
35+
36+
def test_url_error(self):
37+
invalid_url = "http://localhost:9999/"
38+
client = AuditLogsClient(token="xoxp-", base_url=invalid_url)
39+
with self.assertRaises(URLError):
40+
client.logs(limit=1, action="user_login")
41+
42+
def test_http_error(self):
43+
resp: AuditLogsResponse = self.client.api_call(path="error")
44+
self.assertEqual(500, resp.status_code)
45+
self.assertEqual("unexpected response body", resp.raw_body)

0 commit comments

Comments
 (0)