Skip to content

Commit

Permalink
Fix pagination bug where latest is used for offset
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Geise committed Aug 7, 2023
1 parent c33db09 commit 0f36d14
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
11 changes: 11 additions & 0 deletions slack_discovery_sdk/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
from .internal_utils import _next_cursor_is_present # type:ignore


_LATEST_OFFSET_APIS = [
'discovery.conversations.history',
'discovery.conversations.edits',
'discovery.conversations.renames',
'discovery.conversations.reactions',
]


class DiscoveryResponse:
"""An iterable container of response data.
Attributes:
Expand Down Expand Up @@ -120,6 +128,9 @@ def __next__(self):
# offset for https://api.slack.com/enterprise/discovery/methods#users_list etc.
params.update({"offset": self.body.get("offset")})

if any([latest_offset_api in self.api_url for latest_offset_api in _LATEST_OFFSET_APIS]):
params.update({"latest": self.body.get("offset")})

response = self._client.fetch_next_page( # skipcq: PYL-W0212
http_method=self.http_method,
api_url=self.api_url,
Expand Down
17 changes: 16 additions & 1 deletion tests/test_conversations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2021, Slack Technologies, LLC. All rights reserved.
from typing import Any

import os, pytest, time
import os, pytest, time, json
from slack_sdk import WebClient
from slack_discovery_sdk import DiscoveryClient
from slack_discovery_sdk.internal_utils import (
Expand Down Expand Up @@ -91,6 +91,21 @@ def test_conversations_list_pagination(self):
break
assert len(conversations) > limit_size

def test_conversations_history_pagination(self):
conversations = []
limit_size = 1
page_num = 0
for page in self.client.discovery_conversations_history(channel=self.channel, team=self.team, limit=limit_size):
for message in page['messages']:
conversations.append(json.dumps(message))
page_num += 1
if page_num > 5:
break

assert len(conversations) > limit_size
# ensure we are getting different messages for each page
assert sorted(set(conversations)) == sorted(conversations)

def test_conversations_history_from_one_minute_ago(self):
test_text = "This first message will be used to test getting conversation history from the last minute"
first_msg = None
Expand Down

0 comments on commit 0f36d14

Please sign in to comment.