Skip to content

Commit

Permalink
Fix #1305 by fixing pagination with async for syntax (#1308)
Browse files Browse the repository at this point in the history
* Fix #1305 by fixing pagination with async for syntax

* Remove unnecessary imports
  • Loading branch information
seratch authored Nov 28, 2022
1 parent 937bf94 commit 14c2582
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
49 changes: 49 additions & 0 deletions integration_tests/web/test_issue_1305.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import asyncio
import os
import time
import unittest

from integration_tests.env_variable_names import SLACK_SDK_TEST_GRID_ORG_ADMIN_USER_TOKEN
from integration_tests.helpers import async_test
from slack_sdk.web import WebClient
from slack_sdk.web.async_client import AsyncWebClient


class TestWebClient(unittest.TestCase):
"""Runs integration tests with real Slack API
https://github.com/slackapi/python-slack-sdk/issues/1305
"""

def setUp(self):
self.org_admin_token = os.environ[SLACK_SDK_TEST_GRID_ORG_ADMIN_USER_TOKEN]
self.sync_client: WebClient = WebClient(token=self.org_admin_token)
self.async_client: AsyncWebClient = AsyncWebClient(token=self.org_admin_token)

def tearDown(self):
pass

def test_sync(self):
client = self.sync_client
count = 0

for page in client.admin_conversations_search(limit=1):
count += len(page["conversations"])
if count > 1:
break
time.sleep(1)

self.assertGreater(count, 0)

@async_test
async def test_async(self):
client = self.async_client
count = 0

async for page in await client.admin_conversations_search(limit=1):
count += len(page["conversations"])
if count > 1:
break
await asyncio.sleep(1)

self.assertGreater(count, 0)
3 changes: 2 additions & 1 deletion slack_sdk/web/async_slack_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ async def __anext__(self):
params = self.req_args.get("params", {})
if params is None:
params = {}
params.update({"cursor": self.data["response_metadata"]["next_cursor"]})
next_cursor = self.data.get("response_metadata", {}).get("next_cursor") or self.data.get("next_cursor")
params.update({"cursor": next_cursor})
self.req_args.update({"params": params})

response = await self._client._request( # skipcq: PYL-W0212
Expand Down

0 comments on commit 14c2582

Please sign in to comment.