Skip to content

Commit 2a17559

Browse files
authored
[Confluence] Add _get_paged function for internal handling of paged APIs. (#937)
* Add `_get_paged` function for internal handling of paged APIs. * Pin black to old version because 2.7 support is removed in LATEST version. An update will result in a lot of changes in the code.
1 parent d35f45a commit 2a17559

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

atlassian/confluence.py

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,46 @@ def _create_body(body, representation):
4545

4646
return {representation: {"value": body, "representation": representation}}
4747

48+
def _get_paged(self, url, params=None, data=None, flags=None, trailing=None, absolute=False):
49+
"""
50+
Used to get the paged data
51+
52+
:param url: string: The url to retrieve
53+
:param params: dict (default is None): The parameters
54+
:param data: dict (default is None): The data
55+
:param flags: string[] (default is None): The flags
56+
:param trailing: bool (default is None): If True, a trailing slash is added to the url
57+
:param absolute: bool (default is False): If True, the url is used absolute and not relative to the root
58+
59+
:return: A generator object for the data elements
60+
"""
61+
62+
if params is None:
63+
params = {}
64+
65+
while True:
66+
response = self.get(url, trailing=trailing, params=params, data=data, flags=flags, absolute=absolute)
67+
if "results" not in response:
68+
return
69+
70+
for value in response.get("results", []):
71+
yield value
72+
73+
# According to Cloud and Server documentation the links are returned the same way:
74+
# https://developer.atlassian.com/cloud/confluence/rest/api-group-content/#api-wiki-rest-api-content-get
75+
# https://developer.atlassian.com/server/confluence/pagination-in-the-rest-api/
76+
url = response.get("_links", {}).get("next")
77+
if url is None:
78+
break
79+
# From now on we have absolute URLs with parameters
80+
absolute = True
81+
# Params are now provided by the url
82+
params = {}
83+
# Trailing should not be added as it is already part of the url
84+
trailing = False
85+
86+
return
87+
4888
def page_exists(self, space, title):
4989
try:
5090
if self.get_page_by_title(space, title):
@@ -79,7 +119,13 @@ def get_page_child_by_type(self, page_id, type="page", start=None, limit=None, e
79119
log.info(url)
80120

81121
try:
82-
response = self.get(url, params=params)
122+
if not self.advanced_mode and start is None and limit is None:
123+
return self._get_paged(url, params=params)
124+
else:
125+
response = self.get(url, params=params)
126+
if self.advanced_mode:
127+
return response
128+
return response.get("results")
83129
except HTTPError as e:
84130
if e.response.status_code == 404:
85131
# Raise ApiError as the documented reason is ambiguous
@@ -91,10 +137,6 @@ def get_page_child_by_type(self, page_id, type="page", start=None, limit=None, e
91137

92138
raise
93139

94-
if self.advanced_mode:
95-
return response
96-
return response.get("results")
97-
98140
def get_child_title_list(self, page_id, type="page", start=None, limit=None):
99141
"""
100142
Find a list of Child title

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pytest
44
pytest-cov
55
flake8
66
flake8-no-fstring
7-
black
7+
black==21.12b0
88
tox
99
coverage
1010
codecov

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ commands = pylint {[base]linting_targets}
3434
[testenv:black]
3535
basepython = python3
3636
skip_install = true
37-
deps = black
37+
deps = black==21.12b0
3838
commands = black --check --diff {[base]linting_targets}
3939

4040
[testenv:mypy]

0 commit comments

Comments
 (0)