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 /slack/slack_workspace.py | |
parent | ada362c5ff8d844c06c79bc98338d5aab7de9c8b (diff) | |
download | wee-slack-70eaa484831106cb5df6eeb3d7b7fe5ef9b65d0c.tar.gz |
Move SlackApi to a separate file
Diffstat (limited to 'slack/slack_workspace.py')
-rw-r--r-- | slack/slack_workspace.py | 43 |
1 files changed, 2 insertions, 41 deletions
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__() |