diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2023-12-11 21:15:51 +0100 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:54 +0100 |
commit | 3b98fbeff044299104f7d00a3c9b56d9a53b0df0 (patch) | |
tree | 6f38b4bcb90f125047eca166f8889b4fcd77e17a /slack/slack_api.py | |
parent | 363950616419e0894196df08a47e5b084dcf8db4 (diff) | |
download | wee-slack-3b98fbeff044299104f7d00a3c9b56d9a53b0df0.tar.gz |
Support API restricted workspaces
Some enterprise workspaces are restricted in which API methods they can
use, so we have to use some of the APIs the official web client uses
(which can't be used by OAuth tokens) instead (mainly to initialize the
workspace with client.userBoot and client.counts, and to connect to the
websocket).
This also has the benefit of being more performant, as the API methods
the web client uses are more suited for creating a client than the
official API methods.
I think which API methods are restricted may be configured by the
workspace admins, so it may not be the same for different workspaces,
but for me it seems to be at least rtm.connect, users.conversations,
conversations.list and conversations.members, so these are the ones I've
changed to be conditional on the token type.
Diffstat (limited to 'slack/slack_api.py')
-rw-r--r-- | slack/slack_api.py | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/slack/slack_api.py b/slack/slack_api.py index fb6952d..7ce919f 100644 --- a/slack/slack_api.py +++ b/slack/slack_api.py @@ -14,6 +14,8 @@ from slack.util import chunked if TYPE_CHECKING: from slack_api.slack_bots_info import SlackBotInfoResponse, SlackBotsInfoResponse + from slack_api.slack_client_counts import SlackClientCountsResponse + from slack_api.slack_client_userboot import SlackClientUserbootResponse from slack_api.slack_common import SlackGenericResponse from slack_api.slack_conversations_history import SlackConversationsHistoryResponse from slack_api.slack_conversations_info import SlackConversationsInfoResponse @@ -56,10 +58,7 @@ class SlackEdgeApi(SlackApiCommon): return self.workspace.token_type == "session" async def _fetch_edgeapi(self, method: str, params: EdgeParams = {}): - enterprise_id_part = ( - f"{self.workspace.enterprise_id}/" if self.workspace.enterprise_id else "" - ) - url = f"https://edgeapi.slack.com/cache/{enterprise_id_part}{self.workspace.id}/{method}" + url = f"https://edgeapi.slack.com/cache/{self.workspace.id}/{method}" options = self._get_request_options() options["postfields"] = json.dumps(params) options["httpheader"] += "\nContent-Type: application/json" @@ -284,6 +283,20 @@ class SlackApi(SlackApiCommon): raise SlackApiError(self.workspace, method, response) return response + async def fetch_client_userboot(self): + method = "client.userBoot" + response: SlackClientUserbootResponse = await self._fetch(method) + if response["ok"] is False: + raise SlackApiError(self.workspace, method, response) + return response + + async def fetch_client_counts(self): + method = "client.counts" + response: SlackClientCountsResponse = await self._fetch(method) + if response["ok"] is False: + raise SlackApiError(self.workspace, method, response) + return response + async def conversations_close(self, conversation: SlackConversation): method = "conversations.close" params: Params = {"channel": conversation.id} |