From 44ee42c0ba78be9b45df2311b2a05ba73366f70e Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Wed, 4 Oct 2023 14:05:51 +0900 Subject: [PATCH 1/2] Update files.upload v2 method in correspondence with server-side changes --- integration_tests/web/test_files_upload_v2.py | 65 +++++-------------- slack_sdk/web/async_client.py | 12 +--- slack_sdk/web/client.py | 12 +--- slack_sdk/web/internal_utils.py | 34 ---------- slack_sdk/web/legacy_client.py | 12 +--- 5 files changed, 27 insertions(+), 108 deletions(-) diff --git a/integration_tests/web/test_files_upload_v2.py b/integration_tests/web/test_files_upload_v2.py index 0d287ee49..f660f2097 100644 --- a/integration_tests/web/test_files_upload_v2.py +++ b/integration_tests/web/test_files_upload_v2.py @@ -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 @@ -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 @@ -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): @@ -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) @@ -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) @@ -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) @@ -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) @@ -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, @@ -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")) diff --git a/slack_sdk/web/async_client.py b/slack_sdk/web/async_client.py index b0be7ffb0..5bfaa47c5 100644 --- a/slack_sdk/web/async_client.py +++ b/slack_sdk/web/async_client.py @@ -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, ) @@ -3422,7 +3421,7 @@ async def files_upload_v2( 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: @@ -3519,15 +3518,10 @@ async def files_upload_v2( 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: + completion.data["file"] = completion.get("files")[0] return completion async def files_getUploadURLExternal( diff --git a/slack_sdk/web/client.py b/slack_sdk/web/client.py index 968058262..423472ca8 100644 --- a/slack_sdk/web/client.py +++ b/slack_sdk/web/client.py @@ -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, ) @@ -3413,7 +3412,7 @@ def files_upload_v2( 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: @@ -3510,15 +3509,10 @@ def files_upload_v2( 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: + completion.data["file"] = completion.get("files")[0] return completion def files_getUploadURLExternal( diff --git a/slack_sdk/web/internal_utils.py b/slack_sdk/web/internal_utils.py index 466fa0215..2c34e585b 100644 --- a/slack_sdk/web/internal_utils.py +++ b/slack_sdk/web/internal_utils.py @@ -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. " diff --git a/slack_sdk/web/legacy_client.py b/slack_sdk/web/legacy_client.py index 2a1759666..b8dd34a0c 100644 --- a/slack_sdk/web/legacy_client.py +++ b/slack_sdk/web/legacy_client.py @@ -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, ) @@ -3424,7 +3423,7 @@ def files_upload_v2( 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: @@ -3521,15 +3520,10 @@ def files_upload_v2( 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: + completion.data["file"] = completion.get("files")[0] return completion def files_getUploadURLExternal( From f63e25e95b53309544cd2a6050ae8a9a363240b2 Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Wed, 4 Oct 2023 14:33:54 +0900 Subject: [PATCH 2/2] Add pytype skip comments --- slack_sdk/web/async_client.py | 4 ++-- slack_sdk/web/client.py | 4 ++-- slack_sdk/web/legacy_client.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/slack_sdk/web/async_client.py b/slack_sdk/web/async_client.py index 5bfaa47c5..5528c87aa 100644 --- a/slack_sdk/web/async_client.py +++ b/slack_sdk/web/async_client.py @@ -3520,8 +3520,8 @@ async def files_upload_v2( thread_ts=thread_ts, **kwargs, ) - if len(completion.get("files")) == 1: - completion.data["file"] = completion.get("files")[0] + if len(completion.get("files")) == 1: # type: ignore + completion.data["file"] = completion.get("files")[0] # type: ignore return completion async def files_getUploadURLExternal( diff --git a/slack_sdk/web/client.py b/slack_sdk/web/client.py index 423472ca8..2603c5aee 100644 --- a/slack_sdk/web/client.py +++ b/slack_sdk/web/client.py @@ -3511,8 +3511,8 @@ def files_upload_v2( thread_ts=thread_ts, **kwargs, ) - if len(completion.get("files")) == 1: - completion.data["file"] = completion.get("files")[0] + if len(completion.get("files")) == 1: # type: ignore + completion.data["file"] = completion.get("files")[0] # type: ignore return completion def files_getUploadURLExternal( diff --git a/slack_sdk/web/legacy_client.py b/slack_sdk/web/legacy_client.py index b8dd34a0c..40403f39a 100644 --- a/slack_sdk/web/legacy_client.py +++ b/slack_sdk/web/legacy_client.py @@ -3522,8 +3522,8 @@ def files_upload_v2( thread_ts=thread_ts, **kwargs, ) - if len(completion.get("files")) == 1: - completion.data["file"] = completion.get("files")[0] + if len(completion.get("files")) == 1: # type: ignore + completion.data["file"] = completion.get("files")[0] # type: ignore return completion def files_getUploadURLExternal(