Skip to content

Commit 57230f3

Browse files
committed
remove duplicate pagination #103
1 parent 7ea7437 commit 57230f3

6 files changed

Lines changed: 34 additions & 107 deletions

File tree

churchtools_api/calendar.py

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -95,34 +95,11 @@ def get_calendar_appointments(
9595

9696
if response.status_code == 200:
9797
response_content = json.loads(response.content)
98-
response_data = response_content['data'].copy()
99-
logging.debug(
100-
"First response of calendar appointments successful %s", response_content)
98+
response_data = self.combine_paginated_response_data(
99+
response_content, url=url, headers=headers
100+
)
101101

102-
if 'meta' not in response_content.keys(): # Shortcut without Pagination
103-
result = [response_data] if isinstance(
104-
response_data, dict) else response_data
105-
106-
elif 'pagination' not in response_content['meta'].keys():
107-
result = [response_data] if isinstance(
108-
response_data, dict) else response_data
109-
110-
# Long part extending results with pagination
111-
# TODO #1 copied from other method unsure if pagination works the
112-
# same as with groups
113-
else:
114-
while response_content['meta']['pagination']['current'] \
115-
< response_content['meta']['pagination']['lastPage']:
116-
logging.info("page %s of %s",
117-
response_content['meta']['pagination']['current'],
118-
response_content['meta']['pagination']['lastPage'])
119-
params = {
120-
'page': response_content['meta']['pagination']['current'] + 1}
121-
response = self.session.get(
122-
url=url, headers=headers, params=params)
123-
response_content = json.loads(response.content)
124-
response_data.extend(response_content['data'])
125-
result = response_data
102+
result = [response_data] if isinstance(response_data, dict) else response_data
126103

127104
if len(result) == 0:
128105
logging.info(

churchtools_api/events.py

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -79,33 +79,10 @@ def get_events(self, **kwargs) -> list[dict]:
7979

8080
if response.status_code == 200:
8181
response_content = json.loads(response.content)
82-
response_data = response_content['data'].copy()
83-
logging.debug(
84-
"First response of Events successful {}".format(response_content))
85-
86-
if 'meta' not in response_content.keys(): # Shortcut without Pagination
87-
return [response_data] if isinstance(
88-
response_data, dict) else response_data
89-
90-
if 'pagination' not in response_content['meta'].keys():
91-
return [response_data] if isinstance(
92-
response_data, dict) else response_data
93-
94-
# Long part extending results with pagination
95-
# TODO #1 copied from other method unsure if pagination works the
96-
# same as with groups
97-
while response_content['meta']['pagination']['current'] \
98-
< response_content['meta']['pagination']['lastPage']:
99-
logging.info("page {} of {}".format(response_content['meta']['pagination']['current'],
100-
response_content['meta']['pagination']['lastPage']))
101-
params = {
102-
'page': response_content['meta']['pagination']['current'] + 1}
103-
response = self.session.get(
104-
url=url, headers=headers, params=params)
105-
response_content = json.loads(response.content)
106-
response_data.extend(response_content['data'])
107-
108-
return response_data
82+
response_data = self.combine_paginated_response_data(
83+
response_content, url=url, headers=headers
84+
)
85+
return [response_data] if isinstance(response_data, dict) else response_data
10986
else:
11087
logging.warning(
11188
"Something went wrong fetching events: {}".format(

churchtools_api/persons.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,10 @@ def get_persons(self, **kwargs):
4444
logging.warning('Requesting ct_users {} returned an empty response - '
4545
'make sure the user has correct permissions'.format(params))
4646

47-
if 'meta' not in response_content.keys(): # Shortcut without Pagination
48-
return [response_data] if isinstance(
49-
response_data, dict) else response_data
50-
51-
# Long part extending results with pagination
52-
while response_content['meta']['pagination']['current'] \
53-
< response_content['meta']['pagination']['lastPage']:
54-
logging.info("page {} of {}".format(response_content['meta']['pagination']['current'],
55-
response_content['meta']['pagination']['lastPage']))
56-
params = {
57-
'page': response_content['meta']['pagination']['current'] + 1}
58-
response = self.session.get(
59-
url=url, headers=headers, params=params)
60-
response_content = json.loads(response.content)
61-
response_data.extend(response_content['data'])
47+
response_data = self.combine_paginated_response_data(
48+
response_content, url=url, headers=headers
49+
)
50+
response_data = [response_data] if isinstance(response_data, dict) else response_data
6251

6352
if 'returnAsDict' in kwargs and not 'serviceId' in kwargs:
6453
if kwargs['returnAsDict']:

churchtools_api/resources.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import json
22
import logging
3-
from datetime import datetime, timedelta
43

54
from churchtools_api.churchtools_api_abstract import ChurchToolsApiAbstract
65

churchtools_api/songs.py

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,28 @@ class ChurchToolsApiSongs(ChurchToolsApiAbstract):
1515
def __init__(self):
1616
super()
1717

18-
def get_songs(self, **kwargs):
19-
""" Gets list of all songs from the server
20-
:key kwargs song_id: int: optional filter by song id
21-
:return: list of songs
22-
:rtype: list[dict]
18+
def get_songs(self, **kwargs) -> list[dict]:
19+
"""Gets list of all songs from the server.
20+
21+
Kwargs:
22+
song_id: int: optional filter by song id
23+
24+
Returns: list of songs
2325
"""
2426

25-
url = self.domain + '/api/songs'
27+
url = self.domain + "/api/songs"
2628
if "song_id" in kwargs.keys():
27-
url = url + '/{}'.format(kwargs["song_id"])
28-
headers = {
29-
'accept': 'application/json'
30-
}
29+
url = url + "/{}".format(kwargs["song_id"])
30+
headers = {"accept": "application/json"}
3131
response = self.session.get(url=url, headers=headers)
3232

3333
if response.status_code == 200:
3434
response_content = json.loads(response.content)
35-
response_data = response_content['data'].copy()
36-
logging.debug(
37-
"First response of GET Songs successful {}".format(response_content))
38-
39-
if 'meta' not in response_content.keys(): # Shortcut without Pagination
40-
return [response_data] if isinstance(
41-
response_data, dict) else response_data
42-
43-
# Long part extending results with pagination
44-
while response_content['meta']['pagination']['current'] \
45-
< response_content['meta']['pagination']['lastPage']:
46-
logging.info("page {} of {}".format(response_content['meta']['pagination']['current'],
47-
response_content['meta']['pagination']['lastPage']))
48-
params = {
49-
'page': response_content['meta']['pagination']['current'] + 1}
50-
response = self.session.get(
51-
url=url, headers=headers, params=params)
52-
response_content = json.loads(response.content)
53-
response_data.extend(response_content['data'])
54-
55-
return response_data
35+
response_data = self.combine_paginated_response_data(
36+
response_content, url=url, headers=headers
37+
)
38+
return [response_data] if isinstance(response_data, dict) else response_data
39+
5640
else:
5741
if "song_id" in kwargs.keys():
5842
logging.info(

tests/test_churchtools_api.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,22 @@ def test_get_persons(self):
121121
result4 = self.api.get_persons(returnAsDict=False)
122122
self.assertIsInstance(result4, list)
123123

124-
def test_get_songs(self):
124+
def test_get_songs(self) -> None:
125125
"""
126126
1. Test requests all songs and checks that result has more than 10 elements (hence default pagination works)
127-
2. Test requests song 408 and checks that result matches Test song
127+
2. Test requests song 2034 and checks that result matches "sample"
128+
128129
IMPORTANT - This test method and the parameters used depend on the target system!
129-
:return:
130+
the hard coded sample exists on ELKW1610.KRZ.TOOLS
130131
"""
131-
test_song_id = 408
132+
test_song_id = 2034
132133

133134
songs = self.api.get_songs()
134135
self.assertGreater(len(songs), 10)
135136

136137
song = self.api.get_songs(song_id=test_song_id)[0]
137-
self.assertEqual(song['id'], 408)
138-
self.assertEqual(song['name'], 'Test')
138+
self.assertEqual(song["id"], 2034)
139+
self.assertEqual(song["name"], "sample")
139140

140141
def test_get_song_ajax(self):
141142
"""

0 commit comments

Comments
 (0)