Fix #1018 Add optional arguments in WebClient methods#1099
Fix #1018 Add optional arguments in WebClient methods#1099seratch merged 4 commits intoslackapi:mainfrom
Conversation
| ): | ||
| return self | ||
| msg = "The request to the Slack API failed." | ||
| msg = f"The request to the Slack API failed. (url: {self.api_url})" |
There was a problem hiding this comment.
While running tests, I found that this error message does not have sufficient information.
| @@ -77,12 +81,7 @@ def admin_analytics_getFile( | |||
| **kwargs, | |||
| ) -> SlackResponse: | |||
| """Retrieve analytics data for a given date, presented as a compressed JSON file | |||
|
|
|||
| Args: | |||
There was a problem hiding this comment.
Now that we have all arguments, maintaining Args comment for 240+ API methods becomes more challenging. Instead, we can have a link to the API method document.
There was a problem hiding this comment.
I think this will also reduce risk of innacuracy or drift from updates made in one source vs. in the sdk.
| "team_id": team_id, | ||
| } | ||
| ) | ||
| return self.api_call("admin.apps.approve", params=kwargs) |
There was a problem hiding this comment.
As some endpoints do not support application/json request body, it's safer to use the form body for most APIs.
There was a problem hiding this comment.
Interesting, but all endpoints support form POSTs?
There was a problem hiding this comment.
@filmaj Yes, they do but interestingly some endpoints do not support application/json.
| @@ -119,32 +114,114 @@ def admin_apps_approve( | |||
| "The app_id or request_id argument must be specified." | |||
| ) | |||
|
|
|||
| return self.api_call("admin.apps.approve", json=kwargs) | |||
| kwargs.update( | |||
There was a problem hiding this comment.
when we use params (not json), None values will be removed in the api_call method (both async and sync)
slack_sdk/web/client.py
Outdated
| kwargs.update({"channel": channel}) | ||
| kwargs = _filter_none_values(kwargs) |
There was a problem hiding this comment.
For json=kwargs API calls, having None value in dict can result in null value in JSON data. The Slack API server-side often does not work with the data. Thus, we remove None values here. For params / data, we don't need to worry about it.
Codecov Report
@@ Coverage Diff @@
## main #1099 +/- ##
==========================================
+ Coverage 86.20% 86.27% +0.06%
==========================================
Files 110 110
Lines 9955 10413 +458
==========================================
+ Hits 8582 8984 +402
- Misses 1373 1429 +56
Continue to review full report at Codecov.
|
01ca6f7 to
9664ec1
Compare
|
Wow, nice work, |
srajiang
left a comment
There was a problem hiding this comment.
These look really great! Since there are quite a few changes, it will be good to have a few more eyes pass over this for little things missed.
| @@ -77,12 +81,7 @@ def admin_analytics_getFile( | |||
| **kwargs, | |||
| ) -> SlackResponse: | |||
| """Retrieve analytics data for a given date, presented as a compressed JSON file | |||
|
|
|||
| Args: | |||
There was a problem hiding this comment.
I think this will also reduce risk of innacuracy or drift from updates made in one source vs. in the sdk.
| @@ -158,7 +250,9 @@ def admin_apps_uninstall( | |||
| team_ids: Optional[Union[str, Sequence[str]]] = None, | |||
| **kwargs, | |||
| ) -> SlackResponse: | |||
| """Uninstall an app from one or many workspaces, or an entire enterprise organization.""" | |||
| """Uninstall an app from one or many workspaces, or an entire enterprise organization. | |||
There was a problem hiding this comment.
We could note here as we do above for admin.apps.restrict requirements, that with an org-level token, enterprise_id or team_ids is required.
slack_sdk/web/client.py
Outdated
| org_channel: Optional[bool] = None, | ||
| target_team_ids: Optional[Union[str, Sequence[str]]] = None, | ||
| team_id: Optional[str] = None, | ||
| **kwargs, | ||
| ) -> SlackResponse: | ||
| """Set the workspaces in an Enterprise grid org that connect to a channel. |
There was a problem hiding this comment.
Can modify to "that connect to a public or private channel"
There was a problem hiding this comment.
Good catch. Perhaps, the description was updated afterwards.
slack_sdk/web/client.py
Outdated
| self, | ||
| *, | ||
| team_id: str, | ||
| discoverability: str, # open, invite_only, closed, or unlisted |
There was a problem hiding this comment.
Perhaps it would be better to remove this additional comment, in case in future the API supports other discoverability options and there is a difference between the SDK and the public docs.
| """ | ||
| kwargs.update({"team_id": team_id, "name": name}) | ||
| return self.api_call("admin.teams.settings.setName", json=kwargs) | ||
| return self.api_call("admin.teams.settings.setName", params=kwargs) | ||
|
|
||
| def admin_usergroups_addChannels( |
There was a problem hiding this comment.
For some reason the git UI is preventing me from inserting a comment right at line 1153.
Should we make this an Optional field?
team_id: Optional[str] = None,There was a problem hiding this comment.
you're right. Updated.
| https://api.slack.com/methods/files.remote.remove | ||
| """ | ||
| kwargs.update({"external_id": external_id, "file": file}) | ||
| return self.api_call("files.remote.remove", http_verb="POST", params=kwargs) | ||
|
|
||
| def files_remote_share( |
There was a problem hiding this comment.
Could check for either file or external_id field here or raise a SlackRequestError
| def groups_archive(self, *, channel: str, **kwargs) -> SlackResponse: | ||
| """Archives a private channel. | ||
| # -------------------------- | ||
| # Deprecated: groups.* |
There was a problem hiding this comment.
Same comment as with channels.* comment on deprecation. Could point users to conversations.* mapped API methods.
| Args: | ||
| channel (str): Direct message channel to close. e.g. 'D1234567890' | ||
| """ | ||
| def im_close( |
There was a problem hiding this comment.
This method is no longer publicly listed on our API docs, even as a deprecated method. Should we remove support for this?
There was a problem hiding this comment.
We can remove it in the near future but I didn't do so this time
| Args: | ||
| channel (str): Multiparty Direct message channel to close. e.g. 'G1234567890' | ||
| """ | ||
| def mpim_close( |
|
|
||
| def reminders_add(self, *, text: str, time: str, **kwargs) -> SlackResponse: | ||
| def reminders_add( |
There was a problem hiding this comment.
There is an unusual options field for recurrence which is not as well documented. Though from investigating it looks like it would accept a string. Did you omit on purpose for now?
There was a problem hiding this comment.
Thanks. I just missed the one. Added with optional str type
filmaj
left a comment
There was a problem hiding this comment.
LGTM! I appreciate the links to the methods in the function header.
| "team_id": team_id, | ||
| } | ||
| ) | ||
| return self.api_call("admin.apps.approve", params=kwargs) |
There was a problem hiding this comment.
@filmaj Yes, they do but interestingly some endpoints do not support application/json.
| @@ -158,7 +250,9 @@ def admin_apps_uninstall( | |||
| team_ids: Optional[Union[str, Sequence[str]]] = None, | |||
| **kwargs, | |||
| ) -> SlackResponse: | |||
| """Uninstall an app from one or many workspaces, or an entire enterprise organization.""" | |||
| """Uninstall an app from one or many workspaces, or an entire enterprise organization. | |||
slack_sdk/web/client.py
Outdated
| org_channel: Optional[bool] = None, | ||
| target_team_ids: Optional[Union[str, Sequence[str]]] = None, | ||
| team_id: Optional[str] = None, | ||
| **kwargs, | ||
| ) -> SlackResponse: | ||
| """Set the workspaces in an Enterprise grid org that connect to a channel. |
There was a problem hiding this comment.
Good catch. Perhaps, the description was updated afterwards.
slack_sdk/web/client.py
Outdated
| self, | ||
| *, | ||
| team_id: str, | ||
| discoverability: str, # open, invite_only, closed, or unlisted |
| """ | ||
| kwargs.update({"team_id": team_id, "name": name}) | ||
| return self.api_call("admin.teams.settings.setName", json=kwargs) | ||
| return self.api_call("admin.teams.settings.setName", params=kwargs) | ||
|
|
||
| def admin_usergroups_addChannels( |
There was a problem hiding this comment.
you're right. Updated.
| ts: str, | ||
| cursor: Optional[str] = None, | ||
| inclusive: Optional[bool] = None, | ||
| latest: Optional[str] = None, |
There was a problem hiding this comment.
@srajiang we should not recommend using float values for ts, in general. See slackapi/node-slack-sdk#780 (comment)
slack_sdk/web/client.py
Outdated
| external_url: str, | ||
| title: str, | ||
| filetype: Optional[str] = None, | ||
| indexable_file_contents: Optional[str] = None, |
There was a problem hiding this comment.
good catch - fixed!
| https://api.slack.com/methods/files.remote.remove | ||
| """ | ||
| kwargs.update({"external_id": external_id, "file": file}) | ||
| return self.api_call("files.remote.remove", http_verb="POST", params=kwargs) | ||
|
|
||
| def files_remote_share( |
| Args: | ||
| channel (str): Direct message channel to close. e.g. 'D1234567890' | ||
| """ | ||
| def im_close( |
There was a problem hiding this comment.
We can remove it in the near future but I didn't do so this time
|
|
||
| def reminders_add(self, *, text: str, time: str, **kwargs) -> SlackResponse: | ||
| def reminders_add( |
There was a problem hiding this comment.
Thanks. I just missed the one. Added with optional str type
Summary
This pull request fixes #1018 by adding all the optional arguments in
WebClient,AsyncWebClient, andLegacyWebClient.Category (place an
xin each of the[ ])/docs-src(Documents, have you run./docs.sh?)/docs-src-v2(Documents, have you run./docs-v2.sh?)/tutorial(PythOnBoardingBot tutorial)tests/integration_tests(Automated tests for this library)Requirements (place an
xin each[ ])python3 -m venv .venv && source .venv/bin/activate && ./scripts/run_validation.shafter making the changes.