diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2023-01-14 07:27:57 +0100 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:53 +0100 |
commit | 70eaa484831106cb5df6eeb3d7b7fe5ef9b65d0c (patch) | |
tree | 4d8dea543597536249f10f69d9f1ec8d69d30251 | |
parent | ada362c5ff8d844c06c79bc98338d5aab7de9c8b (diff) | |
download | wee-slack-70eaa484831106cb5df6eeb3d7b7fe5ef9b65d0c.tar.gz |
Move SlackApi to a separate file
-rw-r--r-- | slack/slack_api.py | 48 | ||||
-rw-r--r-- | slack/slack_conversation.py | 3 | ||||
-rw-r--r-- | slack/slack_workspace.py | 43 |
3 files changed, 52 insertions, 42 deletions
diff --git a/slack/slack_api.py b/slack/slack_api.py new file mode 100644 index 0000000..3761930 --- /dev/null +++ b/slack/slack_api.py @@ -0,0 +1,48 @@ +from __future__ import annotations + +import json +from typing import TYPE_CHECKING, Dict, Union +from urllib.parse import urlencode + +from slack.http import http_request +from slack.shared import shared + +if TYPE_CHECKING: + from slack.slack_workspace import SlackWorkspace + + +class SlackApi: + def __init__(self, workspace: SlackWorkspace): + self.workspace = workspace + + def get_request_options(self): + return { + "useragent": f"wee_slack {shared.SCRIPT_VERSION}", + "httpheader": f"Authorization: Bearer {self.workspace.config.api_token.value}", + "cookie": self.workspace.config.api_cookies.value, + } + + async def fetch(self, method: str, params: Dict[str, Union[str, int]] = {}): + url = f"https://api.slack.com/api/{method}?{urlencode(params)}" + response = await http_request( + url, + self.get_request_options(), + self.workspace.config.slack_timeout.value * 1000, + ) + return json.loads(response) + + async def fetch_list( + self, + method: str, + list_key: str, + params: Dict[str, Union[str, int]] = {}, + pages: int = 1, # negative or 0 means all pages + ): + response = await self.fetch(method, params) + next_cursor = response.get("response_metadata", {}).get("next_cursor") + if pages != 1 and next_cursor and response["ok"]: + params["cursor"] = next_cursor + next_pages = await self.fetch_list(method, list_key, params, pages - 1) + response[list_key].extend(next_pages[list_key]) + return response + return response diff --git a/slack/slack_conversation.py b/slack/slack_conversation.py index 16dc8ec..f616eb9 100644 --- a/slack/slack_conversation.py +++ b/slack/slack_conversation.py @@ -14,7 +14,8 @@ from slack.util import get_callback_name if TYPE_CHECKING: from slack_api import SlackConversationInfoResponse - from slack.slack_workspace import SlackApi, SlackWorkspace + from slack.slack_api import SlackApi + from slack.slack_workspace import SlackWorkspace def get_conversation_from_buffer_pointer( diff --git a/slack/slack_workspace.py b/slack/slack_workspace.py index 5651f4a..3662682 100644 --- a/slack/slack_workspace.py +++ b/slack/slack_workspace.py @@ -1,55 +1,16 @@ from __future__ import annotations -import json -from typing import Dict, Union -from urllib.parse import urlencode +from typing import Dict import weechat -from slack.http import http_request from slack.shared import shared +from slack.slack_api import SlackApi from slack.slack_conversation import SlackConversation from slack.slack_user import SlackUser from slack.task import Future, create_task -class SlackApi: - def __init__(self, workspace: SlackWorkspace): - self.workspace = workspace - - def get_request_options(self): - return { - "useragent": f"wee_slack {shared.SCRIPT_VERSION}", - "httpheader": f"Authorization: Bearer {self.workspace.config.api_token.value}", - "cookie": self.workspace.config.api_cookies.value, - } - - async def fetch(self, method: str, params: Dict[str, Union[str, int]] = {}): - url = f"https://api.slack.com/api/{method}?{urlencode(params)}" - response = await http_request( - url, - self.get_request_options(), - self.workspace.config.slack_timeout.value * 1000, - ) - return json.loads(response) - - async def fetch_list( - self, - method: str, - list_key: str, - params: Dict[str, Union[str, int]] = {}, - pages: int = 1, # negative or 0 means all pages - ): - response = await self.fetch(method, params) - next_cursor = response.get("response_metadata", {}).get("next_cursor") - if pages != 1 and next_cursor and response["ok"]: - params["cursor"] = next_cursor - next_pages = await self.fetch_list(method, list_key, params, pages - 1) - response[list_key].extend(next_pages[list_key]) - return response - return response - - class SlackUsers(Dict[str, Future[SlackUser]]): def __init__(self, workspace: SlackWorkspace): super().__init__() |