aboutsummaryrefslogtreecommitdiffstats
path: root/slack
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2023-01-14 07:51:59 +0100
committerTrygve Aaberge <trygveaa@gmail.com>2024-02-18 11:32:53 +0100
commita7b617b886c3f56165a5aeb27631520a6e6e5d96 (patch)
tree06f0a3203991244f9c76eb1fdc4cb8d5f79df19c /slack
parent70eaa484831106cb5df6eeb3d7b7fe5ef9b65d0c (diff)
downloadwee-slack-a7b617b886c3f56165a5aeb27631520a6e6e5d96.tar.gz
Create functions for fetch requests
Diffstat (limited to 'slack')
-rw-r--r--slack/slack_api.py49
-rw-r--r--slack/slack_conversation.py13
-rw-r--r--slack/slack_user.py2
-rw-r--r--slack/slack_workspace.py13
4 files changed, 47 insertions, 30 deletions
diff --git a/slack/slack_api.py b/slack/slack_api.py
index 3761930..8408488 100644
--- a/slack/slack_api.py
+++ b/slack/slack_api.py
@@ -1,13 +1,17 @@
from __future__ import annotations
import json
-from typing import TYPE_CHECKING, Dict, Union
+from typing import TYPE_CHECKING, Any, Dict, Union
from urllib.parse import urlencode
from slack.http import http_request
from slack.shared import shared
if TYPE_CHECKING:
+ from slack_api import SlackConversationInfoResponse
+
+ from slack.slack_conversation import SlackConversation
+ from slack.slack_user import SlackUser
from slack.slack_workspace import SlackWorkspace
@@ -15,34 +19,63 @@ class SlackApi:
def __init__(self, workspace: SlackWorkspace):
self.workspace = workspace
- def get_request_options(self):
+ 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]] = {}):
+ 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._get_request_options(),
self.workspace.config.slack_timeout.value * 1000,
)
return json.loads(response)
- async def fetch_list(
+ 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
+ pages: int = -1, # negative or 0 means all pages
):
- response = await self.fetch(method, params)
+ 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)
+ next_pages = await self._fetch_list(method, list_key, params, pages - 1)
response[list_key].extend(next_pages[list_key])
return response
return response
+
+ async def fetch_conversations_history(self, conversation: SlackConversation) -> Any:
+ return await self._fetch("conversations.history", {"channel": conversation.id})
+
+ async def fetch_conversations_info(
+ self, conversation: SlackConversation
+ ) -> SlackConversationInfoResponse:
+ return await self._fetch("conversations.info", {"channel": conversation.id})
+
+ async def fetch_users_conversations(
+ self,
+ types: str,
+ exclude_archived: bool = True,
+ limit: int = 1000,
+ pages: int = -1,
+ ) -> Any:
+ return await self._fetch_list(
+ "users.conversations",
+ "channels",
+ {
+ "types": types,
+ "exclude_archived": exclude_archived,
+ "limit": limit,
+ },
+ pages,
+ )
+
+ async def fetch_users_info(self, user: SlackUser) -> Any:
+ return await self._fetch("users.info", {"user": user.id})
diff --git a/slack/slack_conversation.py b/slack/slack_conversation.py
index f616eb9..e92ddcf 100644
--- a/slack/slack_conversation.py
+++ b/slack/slack_conversation.py
@@ -12,8 +12,6 @@ from slack.task import gather
from slack.util import get_callback_name
if TYPE_CHECKING:
- from slack_api import SlackConversationInfoResponse
-
from slack.slack_api import SlackApi
from slack.slack_workspace import SlackWorkspace
@@ -60,7 +58,7 @@ class SlackConversation:
async def init(self):
with self.loading():
- info = await self.fetch_info()
+ info = await self.api.fetch_conversations_info(self)
if info["ok"] != True:
# TODO: Handle error
return
@@ -78,11 +76,6 @@ class SlackConversation:
)
weechat.buffer_set(self.buffer_pointer, "localvar_set_nick", "nick")
- async def fetch_info(self) -> SlackConversationInfoResponse:
- with self.loading():
- info = await self.api.fetch("conversations.info", {"channel": self.id})
- return info
-
async def fill_history(self):
if self.history_filled or self.history_pending:
return
@@ -90,9 +83,7 @@ class SlackConversation:
with self.loading():
self.history_pending = True
- history = await self.api.fetch(
- "conversations.history", {"channel": self.id}
- )
+ history = await self.api.fetch_conversations_history(self)
start = time.time()
messages = [SlackMessage(self, message) for message in history["messages"]]
diff --git a/slack/slack_user.py b/slack/slack_user.py
index 50540f8..18c897f 100644
--- a/slack/slack_user.py
+++ b/slack/slack_user.py
@@ -17,5 +17,5 @@ class SlackUser:
return self.workspace.api
async def init(self):
- info = await self.api.fetch("users.info", {"user": self.id})
+ info = await self.api.fetch_users_info(self)
self.name = info["user"]["name"]
diff --git a/slack/slack_workspace.py b/slack/slack_workspace.py
index 3662682..72ffc7d 100644
--- a/slack/slack_workspace.py
+++ b/slack/slack_workspace.py
@@ -38,16 +38,9 @@ class SlackWorkspace:
async def connect(self):
# rtm_connect = await self.api.fetch("rtm.connect")
- user_channels_response = await self.api.fetch_list(
- "users.conversations",
- "channels",
- {
- "exclude_archived": True,
- # "types": "public_channel,private_channel,im",
- "types": "public_channel",
- "limit": 1000,
- },
- -1,
+ # "types": "public_channel,private_channel,im",
+ user_channels_response = await self.api.fetch_users_conversations(
+ "public_channel"
)
user_channels = user_channels_response["channels"]