Skip to content

Commit

Permalink
Merge pull request #26 from abirukov/pagination-fixes
Browse files Browse the repository at this point in the history
fix pagination
  • Loading branch information
akopdev authored Jul 19, 2024
2 parents 6435afc + 405338d commit 6ed802f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
17 changes: 13 additions & 4 deletions bitrix24/bitrix24.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async def request(self, method: str, params: str = None) -> Dict[str, Any]:
return response

async def _call(
self, method: str, params: Dict[str, Any] = None, start: int = 0
self, method: str, params: Dict[str, Any] = None, start: int = 0
) -> Dict[str, Any]:
"""Async call a REST method with specified parameters.
Expand All @@ -133,12 +133,21 @@ async def _call(
res = await self.request(method, payload)

if "next" in res and not start and self._fetch_all_pages:
if res["total"] % 50 == 0:
count_tasks = res["total"] // 50 - 1
else:
count_tasks = res["total"] // 50

tasks = [
self._call(method, params, (s + 1) * 50) for s in range(res["total"] // 50 - 1)
self._call(method, params, (s + 1) * 50) for s in range(count_tasks)
]
items = await asyncio.gather(*tasks)
result = list(itertools.chain(*items))
return res["result"] + result
if type(res["result"]) is not dict:
return res["result"] + list(itertools.chain(*items))
if items:
key = list(res["result"].keys())[0]
for item in items:
res["result"][key] += item[key]
return res["result"]

def callMethod(self, method: str, params: Dict[str, Any] = None, **kwargs) -> Dict[str, Any]:
Expand Down
23 changes: 21 additions & 2 deletions tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,39 @@ async def test_concurrent_requests(b24):
with aioresponses() as m:
m.get(
"https://example.bitrix24.com/rest/1/123456789/crm.deal.list.json?start=0",
payload={"result": [{"ID": 1}], "next": 50, "total": 100},
payload={"result": [{"ID": 1}], "next": 50, "total": 82},
status=200,
repeat=True,
)
m.get(
"https://example.bitrix24.com/rest/1/123456789/crm.deal.list.json?start=50",
payload={"result": [{"ID": 2}], "total": 100},
payload={"result": [{"ID": 2}], "total": 82},
status=200,
repeat=True,
)
res = await b24.callMethod("crm.deal.list")
assert res == [{"ID": 1}, {"ID": 2}]


@pytest.mark.asyncio
async def test_concurrent_requests_nesting_level(b24):
with aioresponses() as m:
m.get(
"https://example.bitrix24.com/rest/1/123456789/tasks.task.list.json?start=0",
payload={"result": {"tasks": [{"ID": 1}]}, "next": 50, "total": 100},
status=200,
repeat=True,
)
m.get(
"https://example.bitrix24.com/rest/1/123456789/tasks.task.list.json?start=50",
payload={"result": {"tasks": [{"ID": 2}]}, "total": 100},
status=200,
repeat=True,
)
res = await b24.callMethod("tasks.task.list")
assert res == {"tasks": [{"ID": 1}, {"ID": 2}]}


@pytest.mark.asyncio
async def test_request_with_disabled_pagination():
b24 = Bitrix24("https://example.bitrix24.com/rest/1/123456789", fetch_all_pages=False)
Expand Down

0 comments on commit 6ed802f

Please sign in to comment.