Skip to content

Commit

Permalink
Merge pull request #730 from seratch/issue-728-file-upload-bytes
Browse files Browse the repository at this point in the history
Fix #728 by adding bytearray support in files_upload (sync mode)
  • Loading branch information
seratch committed Jun 24, 2020
2 parents 77a125a + bff133c commit 258806a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
40 changes: 40 additions & 0 deletions integration_tests/web/test_issue_728.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
import unittest

from integration_tests.env_variable_names import \
SLACK_SDK_TEST_BOT_TOKEN, \
SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID
from integration_tests.helpers import async_test
from slack import WebClient


class TestWebClient(unittest.TestCase):
"""Runs integration tests with real Slack API
https://github.com/slackapi/python-slackclient/issues/728
"""

def setUp(self):
if not hasattr(self, "logger"):
self.bot_token = os.environ[SLACK_SDK_TEST_BOT_TOKEN]
self.channel_ids = ",".join([os.environ[SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID]])

def tearDown(self):
pass

def test_bytes_for_file_param(self):
client: WebClient = WebClient(token=self.bot_token, run_async=False)
bytes = bytearray("This is a test", "utf-8")
upload = client.files_upload(file=bytes, filename="test.txt", channels=self.channel_ids)
self.assertIsNotNone(upload)
deletion = client.files_delete(file=upload["file"]["id"])
self.assertIsNotNone(deletion)

@async_test
async def test_bytes_for_file_param_async(self):
client: WebClient = WebClient(token=self.bot_token, run_async=True)
bytes = bytearray("This is a test", "utf-8")
upload = await client.files_upload(file=bytes, filename="test.txt", channels=self.channel_ids)
self.assertIsNotNone(upload)
deletion = await client.files_delete(file=upload["file"]["id"])
self.assertIsNotNone(deletion)
2 changes: 2 additions & 0 deletions slack/web/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ def convert_params(values: dict) -> dict:
f: BinaryIO = open(v.encode("utf-8", "ignore"), "rb")
files_to_close.append(f)
request_data.update({k: f})
elif isinstance(v, bytearray):
request_data.update({k: io.BytesIO(v)})
else:
request_data.update({k: v})

Expand Down

0 comments on commit 258806a

Please sign in to comment.