Skip to content

Commit 258806a

Browse files
authored
Merge pull request #730 from seratch/issue-728-file-upload-bytes
Fix #728 by adding bytearray support in files_upload (sync mode)
2 parents 77a125a + bff133c commit 258806a

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import os
2+
import unittest
3+
4+
from integration_tests.env_variable_names import \
5+
SLACK_SDK_TEST_BOT_TOKEN, \
6+
SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID
7+
from integration_tests.helpers import async_test
8+
from slack import WebClient
9+
10+
11+
class TestWebClient(unittest.TestCase):
12+
"""Runs integration tests with real Slack API
13+
14+
https://github.com/slackapi/python-slackclient/issues/728
15+
"""
16+
17+
def setUp(self):
18+
if not hasattr(self, "logger"):
19+
self.bot_token = os.environ[SLACK_SDK_TEST_BOT_TOKEN]
20+
self.channel_ids = ",".join([os.environ[SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID]])
21+
22+
def tearDown(self):
23+
pass
24+
25+
def test_bytes_for_file_param(self):
26+
client: WebClient = WebClient(token=self.bot_token, run_async=False)
27+
bytes = bytearray("This is a test", "utf-8")
28+
upload = client.files_upload(file=bytes, filename="test.txt", channels=self.channel_ids)
29+
self.assertIsNotNone(upload)
30+
deletion = client.files_delete(file=upload["file"]["id"])
31+
self.assertIsNotNone(deletion)
32+
33+
@async_test
34+
async def test_bytes_for_file_param_async(self):
35+
client: WebClient = WebClient(token=self.bot_token, run_async=True)
36+
bytes = bytearray("This is a test", "utf-8")
37+
upload = await client.files_upload(file=bytes, filename="test.txt", channels=self.channel_ids)
38+
self.assertIsNotNone(upload)
39+
deletion = await client.files_delete(file=upload["file"]["id"])
40+
self.assertIsNotNone(deletion)

slack/web/base_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ def convert_params(values: dict) -> dict:
427427
f: BinaryIO = open(v.encode("utf-8", "ignore"), "rb")
428428
files_to_close.append(f)
429429
request_data.update({k: f})
430+
elif isinstance(v, bytearray):
431+
request_data.update({k: io.BytesIO(v)})
430432
else:
431433
request_data.update({k: v})
432434

0 commit comments

Comments
 (0)