diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2023-09-16 21:24:16 +0200 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:54 +0100 |
commit | 150fb51720fd0e8a8511c6c3468b8e182dedea3e (patch) | |
tree | f155c86fad3c2e27a189bfa47ce83d1acf7cfd74 /slack/slack_api.py | |
parent | 0b46f31f14e9a69568535b2c73bc00fc396e2d71 (diff) | |
download | wee-slack-150fb51720fd0e8a8511c6c3468b8e182dedea3e.tar.gz |
Only take in limit, not pages in api methods
Instead of having to supply both a limit and the number of pages, change
it to only take in a limit, and determine the number of pages
automatically.
Diffstat (limited to 'slack/slack_api.py')
-rw-r--r-- | slack/slack_api.py | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/slack/slack_api.py b/slack/slack_api.py index 9638e58..9627594 100644 --- a/slack/slack_api.py +++ b/slack/slack_api.py @@ -1,7 +1,7 @@ from __future__ import annotations import json -from typing import TYPE_CHECKING, Iterable, Mapping, Sequence, Union +from typing import TYPE_CHECKING, Iterable, Mapping, Optional, Sequence, Union from urllib.parse import urlencode from slack.error import SlackApiError @@ -108,13 +108,15 @@ class SlackApi(SlackApiCommon): method: str, list_key: str, params: Params = {}, - pages: int = -1, # negative or 0 means all pages + limit: Optional[int] = None, ): - response = await self._fetch(method, params) + cur_limit = 1000 if limit is None or limit > 1000 else limit + response = await self._fetch(method, {**params, "limit": cur_limit}) + remaining = limit - cur_limit if limit is not None else None next_cursor = response.get("response_metadata", {}).get("next_cursor") - if pages != 1 and next_cursor and response["ok"]: + if (remaining is None or remaining > 0) and next_cursor and response["ok"]: new_params = {**params, "cursor": next_cursor} - next_pages = await self._fetch_list(method, list_key, new_params, pages - 1) + next_pages = await self._fetch_list(method, list_key, new_params, remaining) response[list_key].extend(next_pages[list_key]) return response return response @@ -145,13 +147,12 @@ class SlackApi(SlackApiCommon): async def fetch_conversations_members( self, conversation: SlackConversation, - limit: int = 1000, - pages: int = -1, + limit: Optional[int] = None, ): method = "conversations.members" - params: Params = {"channel": conversation.id, "limit": limit} + params: Params = {"channel": conversation.id} response: SlackConversationsMembersResponse = await self._fetch_list( - method, "members", params, pages + method, "members", params, limit ) if response["ok"] is False: raise SlackApiError(self.workspace, method, response, params) @@ -161,20 +162,18 @@ class SlackApi(SlackApiCommon): self, types: str, exclude_archived: bool = True, - limit: int = 1000, - pages: int = -1, + limit: Optional[int] = None, ): method = "users.conversations" params: Params = { "types": types, "exclude_archived": exclude_archived, - "limit": limit, } response: SlackUsersConversationsResponse = await self._fetch_list( method, "channels", params, - pages, + limit, ) if response["ok"] is False: raise SlackApiError(self.workspace, method, response, params) |