Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update files.upload v2 method in correspondence with server-side changes #1408

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 18 additions & 47 deletions integration_tests/web/test_files_upload_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def test_uploading_text_files(self):
title="Test code",
)
self.assertIsNotNone(upload)
self.assertIsNotNone(upload.get("files")[0].get("id"))
self.assertIsNotNone(upload.get("files")[0].get("title"))

def test_uploading_bytes_io(self):
client = self.sync_client
Expand All @@ -54,6 +56,8 @@ def test_uploading_bytes_io(self):
title="Test code",
)
self.assertIsNotNone(upload)
self.assertIsNotNone(upload.get("files")[0].get("id"))
self.assertIsNotNone(upload.get("files")[0].get("title"))

def test_uploading_multiple_files(self):
client = self.sync_client
Expand All @@ -74,6 +78,8 @@ def test_uploading_multiple_files(self):
initial_comment="Here are files :wave:",
)
self.assertIsNotNone(upload)
self.assertIsNotNone(upload.get("files")[0].get("id"))
self.assertIsNotNone(upload.get("files")[0].get("title"))

@async_test
async def test_uploading_text_files_async(self):
Expand All @@ -86,6 +92,8 @@ async def test_uploading_text_files_async(self):
file=file,
)
self.assertIsNotNone(upload)
self.assertIsNotNone(upload.get("files")[0].get("id"))
self.assertIsNotNone(upload.get("files")[0].get("title"))

deletion = await client.files_delete(file=upload["file"]["id"])
self.assertIsNotNone(deletion)
Expand Down Expand Up @@ -116,6 +124,8 @@ def test_uploading_binary_files(self):
file=file,
)
self.assertIsNotNone(upload)
self.assertIsNotNone(upload.get("files")[0].get("id"))
self.assertIsNotNone(upload.get("files")[0].get("title"))

deletion = client.files_delete(file=upload["file"]["id"])
self.assertIsNotNone(deletion)
Expand All @@ -133,6 +143,8 @@ def test_uploading_binary_files_as_content(self):
content=content,
)
self.assertIsNotNone(upload)
self.assertIsNotNone(upload.get("files")[0].get("id"))
self.assertIsNotNone(upload.get("files")[0].get("title"))

deletion = client.files_delete(file=upload["file"]["id"])
self.assertIsNotNone(deletion)
Expand All @@ -149,6 +161,8 @@ async def test_uploading_binary_files_async(self):
file=file,
)
self.assertIsNotNone(upload)
self.assertIsNotNone(upload.get("files")[0].get("id"))
self.assertIsNotNone(upload.get("files")[0].get("title"))

deletion = await client.files_delete(file=upload["file"]["id"])
self.assertIsNotNone(deletion)
Expand All @@ -165,6 +179,8 @@ def test_uploading_file_with_token_param(self):
file=file,
)
self.assertIsNotNone(upload)
self.assertIsNotNone(upload.get("files")[0].get("id"))
self.assertIsNotNone(upload.get("files")[0].get("title"))

deletion = client.files_delete(
token=self.bot_token,
Expand All @@ -185,56 +201,11 @@ async def test_uploading_file_with_token_param_async(self):
file=file,
)
self.assertIsNotNone(upload)
self.assertIsNotNone(upload.get("files")[0].get("id"))
self.assertIsNotNone(upload.get("files")[0].get("title"))

deletion = await client.files_delete(
token=self.bot_token,
file=upload["file"]["id"],
)
self.assertIsNotNone(deletion)

def test_request_file_info_false(self):
client = self.sync_client
upload = client.files_upload_v2(
channels=self.channel_id,
title="Foo",
filename="foo.txt",
content="foo",
)
self.assertIsNotNone(upload)
self.assertIsNotNone(upload.get("files")[0].get("id"))
self.assertIsNotNone(upload.get("files")[0].get("name"))

upload = client.files_upload_v2(
channels=self.channel_id,
title="Foo",
filename="foo.txt",
content="foo",
request_file_info=False,
)
self.assertIsNotNone(upload)
self.assertIsNotNone(upload.get("files")[0].get("id"))
self.assertIsNone(upload.get("files")[0].get("name"))

@async_test
async def test_request_file_info_false_async(self):
client = self.async_client
upload = await client.files_upload_v2(
channels=self.channel_id,
title="Foo",
filename="foo.txt",
content="foo",
)
self.assertIsNotNone(upload)
self.assertIsNotNone(upload.get("files")[0].get("id"))
self.assertIsNotNone(upload.get("files")[0].get("name"))

upload = await client.files_upload_v2(
channels=self.channel_id,
title="Foo",
filename="foo.txt",
content="foo",
request_file_info=False,
)
self.assertIsNotNone(upload)
self.assertIsNotNone(upload.get("files")[0].get("id"))
self.assertIsNone(upload.get("files")[0].get("name"))
12 changes: 3 additions & 9 deletions slack_sdk/web/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
_remove_none_values,
_to_v2_file_upload_item,
_upload_file_via_v2_url,
_attach_full_file_metadata_async,
_validate_for_legacy_client,
_print_files_upload_v2_suggestion,
)
Expand Down Expand Up @@ -3422,7 +3421,7 @@
channel: Optional[str] = None,
initial_comment: Optional[str] = None,
thread_ts: Optional[str] = None,
request_file_info: bool = True,
request_file_info: bool = True, # since v3.23, this flag is no longer necessary
**kwargs,
) -> AsyncSlackResponse:
"""This wrapper method provides an easy way to upload files using the following endpoints:
Expand Down Expand Up @@ -3519,15 +3518,10 @@
channel_id=channel_to_share,
initial_comment=initial_comment,
thread_ts=thread_ts,
token=kwargs.get("token"),
**kwargs,
)
if request_file_info is True:
await _attach_full_file_metadata_async(
client=self,
token_as_arg=kwargs.get("token"),
completion=completion,
)
if len(completion.get("files")) == 1: # type: ignore
completion.data["file"] = completion.get("files")[0] # type: ignore

Check warning on line 3524 in slack_sdk/web/async_client.py

View check run for this annotation

Codecov / codecov/patch

slack_sdk/web/async_client.py#L3523-L3524

Added lines #L3523 - L3524 were not covered by tests
return completion

async def files_getUploadURLExternal(
Expand Down
12 changes: 3 additions & 9 deletions slack_sdk/web/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
_remove_none_values,
_to_v2_file_upload_item,
_upload_file_via_v2_url,
_attach_full_file_metadata,
_validate_for_legacy_client,
_print_files_upload_v2_suggestion,
)
Expand Down Expand Up @@ -3413,7 +3412,7 @@
channel: Optional[str] = None,
initial_comment: Optional[str] = None,
thread_ts: Optional[str] = None,
request_file_info: bool = True,
request_file_info: bool = True, # since v3.23, this flag is no longer necessary
**kwargs,
) -> SlackResponse:
"""This wrapper method provides an easy way to upload files using the following endpoints:
Expand Down Expand Up @@ -3510,15 +3509,10 @@
channel_id=channel_to_share,
initial_comment=initial_comment,
thread_ts=thread_ts,
token=kwargs.get("token"),
**kwargs,
)
if request_file_info is True:
_attach_full_file_metadata(
client=self,
token_as_arg=kwargs.get("token"),
completion=completion,
)
if len(completion.get("files")) == 1: # type: ignore
completion.data["file"] = completion.get("files")[0] # type: ignore

Check warning on line 3515 in slack_sdk/web/client.py

View check run for this annotation

Codecov / codecov/patch

slack_sdk/web/client.py#L3514-L3515

Added lines #L3514 - L3515 were not covered by tests
return completion

def files_getUploadURLExternal(
Expand Down
34 changes: 0 additions & 34 deletions slack_sdk/web/internal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,40 +407,6 @@ def _validate_for_legacy_client(
raise SlackRequestError(message)


def _attach_full_file_metadata(
client, # type: ignore
token_as_arg: Optional[str],
completion: Union["SlackResponse", Future], # noqa: F821
) -> None:
_validate_for_legacy_client(completion)
_completion: Any = completion # just for satisfying pytype
# fetch all the file metadata for backward-compatibility
for f in _completion.get("files"):
full_info = client.files_info(
file=f.get("id"),
token=token_as_arg,
)
f.update(full_info["file"])
if len(_completion.get("files")) == 1:
_completion.data["file"] = _completion.get("files")[0]


async def _attach_full_file_metadata_async(
client, # type: ignore
token_as_arg: Optional[str],
completion: "SlackResponse", # noqa: F821
) -> None:
# fetch all the file metadata for backward-compatibility
for f in completion.get("files"):
full_info = await client.files_info(
file=f.get("id"),
token=token_as_arg,
)
f.update(full_info["file"])
if len(completion.get("files")) == 1:
completion.data["file"] = completion.get("files")[0]


def _print_files_upload_v2_suggestion():
message = (
"client.files_upload() may cause some issues like timeouts for relatively large files. "
Expand Down
12 changes: 3 additions & 9 deletions slack_sdk/web/legacy_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
_remove_none_values,
_to_v2_file_upload_item,
_upload_file_via_v2_url,
_attach_full_file_metadata,
_validate_for_legacy_client,
_print_files_upload_v2_suggestion,
)
Expand Down Expand Up @@ -3424,7 +3423,7 @@
channel: Optional[str] = None,
initial_comment: Optional[str] = None,
thread_ts: Optional[str] = None,
request_file_info: bool = True,
request_file_info: bool = True, # since v3.23, this flag is no longer necessary
**kwargs,
) -> Union[Future, SlackResponse]:
"""This wrapper method provides an easy way to upload files using the following endpoints:
Expand Down Expand Up @@ -3521,15 +3520,10 @@
channel_id=channel_to_share,
initial_comment=initial_comment,
thread_ts=thread_ts,
token=kwargs.get("token"),
**kwargs,
)
if request_file_info is True:
_attach_full_file_metadata(
client=self,
token_as_arg=kwargs.get("token"),
completion=completion,
)
if len(completion.get("files")) == 1: # type: ignore
completion.data["file"] = completion.get("files")[0] # type: ignore

Check warning on line 3526 in slack_sdk/web/legacy_client.py

View check run for this annotation

Codecov / codecov/patch

slack_sdk/web/legacy_client.py#L3525-L3526

Added lines #L3525 - L3526 were not covered by tests
return completion

def files_getUploadURLExternal(
Expand Down