Skip to content

Commit 6ed802f

Browse files
authored
Merge pull request #26 from abirukov/pagination-fixes
fix pagination
2 parents 6435afc + 405338d commit 6ed802f

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

bitrix24/bitrix24.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ async def request(self, method: str, params: str = None) -> Dict[str, Any]:
115115
return response
116116

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

135135
if "next" in res and not start and self._fetch_all_pages:
136+
if res["total"] % 50 == 0:
137+
count_tasks = res["total"] // 50 - 1
138+
else:
139+
count_tasks = res["total"] // 50
140+
136141
tasks = [
137-
self._call(method, params, (s + 1) * 50) for s in range(res["total"] // 50 - 1)
142+
self._call(method, params, (s + 1) * 50) for s in range(count_tasks)
138143
]
139144
items = await asyncio.gather(*tasks)
140-
result = list(itertools.chain(*items))
141-
return res["result"] + result
145+
if type(res["result"]) is not dict:
146+
return res["result"] + list(itertools.chain(*items))
147+
if items:
148+
key = list(res["result"].keys())[0]
149+
for item in items:
150+
res["result"][key] += item[key]
142151
return res["result"]
143152

144153
def callMethod(self, method: str, params: Dict[str, Any] = None, **kwargs) -> Dict[str, Any]:

tests/test_pagination.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,39 @@ async def test_concurrent_requests(b24):
88
with aioresponses() as m:
99
m.get(
1010
"https://example.bitrix24.com/rest/1/123456789/crm.deal.list.json?start=0",
11-
payload={"result": [{"ID": 1}], "next": 50, "total": 100},
11+
payload={"result": [{"ID": 1}], "next": 50, "total": 82},
1212
status=200,
1313
repeat=True,
1414
)
1515
m.get(
1616
"https://example.bitrix24.com/rest/1/123456789/crm.deal.list.json?start=50",
17-
payload={"result": [{"ID": 2}], "total": 100},
17+
payload={"result": [{"ID": 2}], "total": 82},
1818
status=200,
1919
repeat=True,
2020
)
2121
res = await b24.callMethod("crm.deal.list")
2222
assert res == [{"ID": 1}, {"ID": 2}]
2323

2424

25+
@pytest.mark.asyncio
26+
async def test_concurrent_requests_nesting_level(b24):
27+
with aioresponses() as m:
28+
m.get(
29+
"https://example.bitrix24.com/rest/1/123456789/tasks.task.list.json?start=0",
30+
payload={"result": {"tasks": [{"ID": 1}]}, "next": 50, "total": 100},
31+
status=200,
32+
repeat=True,
33+
)
34+
m.get(
35+
"https://example.bitrix24.com/rest/1/123456789/tasks.task.list.json?start=50",
36+
payload={"result": {"tasks": [{"ID": 2}]}, "total": 100},
37+
status=200,
38+
repeat=True,
39+
)
40+
res = await b24.callMethod("tasks.task.list")
41+
assert res == {"tasks": [{"ID": 1}, {"ID": 2}]}
42+
43+
2544
@pytest.mark.asyncio
2645
async def test_request_with_disabled_pagination():
2746
b24 = Bitrix24("https://example.bitrix24.com/rest/1/123456789", fetch_all_pages=False)

0 commit comments

Comments
 (0)