Skip to content

Commit aadb452

Browse files
committed
add get survey data layouts function
1 parent b427bf3 commit aadb452

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

CmixAPIClient/api.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ def add_extra_url_params(self, url, params):
163163

164164
return url
165165

166+
def get_survey_data_layouts(self, survey_id):
167+
self.check_auth_headers()
168+
data_layouts_url = '{}/surveys/{}/data-layouts'.format(CMIX_SERVICES['survey'][self.url_type], survey_id)
169+
data_layouts_response = requests.get(data_layouts_url, headers=self._authentication_headers)
170+
if data_layouts_response.status_code != 200:
171+
raise CmixError(
172+
'CMIX returned a non-200 response code while getting data_layouts: {} and error {}'.format(
173+
data_layouts_response.status_code,
174+
data_layouts_response.text
175+
)
176+
)
177+
return data_layouts_response.json()
178+
166179
def get_survey_definition(self, survey_id):
167180
self.check_auth_headers()
168181
definition_url = '{}/surveys/{}/definition'.format(CMIX_SERVICES['survey'][self.url_type], survey_id)
@@ -242,25 +255,14 @@ def create_export_archive(self, survey_id, export_type):
242255
)
243256
archive_json = archive_response.json()
244257

245-
layout_url = '{}/surveys/{}/data-layouts/'.format(CMIX_SERVICES['survey']["BASE_URL"], survey_id)
246-
layout_response = requests.get(layout_url, headers=self._authentication_headers)
247-
if layout_response.status_code != 200:
248-
raise CmixError(
249-
'CMIX returned a non-200 response code: {} and error {}'.format(
250-
layout_response.status_code,
251-
layout_response.text
252-
)
253-
)
258+
layout_json = self.get_survey_data_layouts(survey_id)
254259
layout_id = None
255-
for layout in layout_response.json():
260+
for layout in layout_json:
256261
if layout.get('name') == 'Default':
257262
layout_id = layout.get('id')
258263
if layout_id is None:
259264
raise CmixError(
260-
'Layouts response did not contain a "Default" layout. Response Code: {}, Body {}'.format(
261-
layout_response.status_code,
262-
layout_response.content
263-
)
265+
'Layouts response did not contain a "Default" layout.'
264266
)
265267

266268
archive_json['dataLayoutId'] = layout_id

tests/test_api.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,32 @@ def test_create_export_archive_errors_handled(self):
116116
with self.assertRaises(CmixError):
117117
self.cmix_api.create_export_archive(self.survey_id, 'XLSX_READABLE')
118118

119+
def test_get_survey_data_layouts(self):
120+
self.cmix_api._authentication_headers = {'Authentication': 'Bearer test'}
121+
122+
# success case
123+
with mock.patch('CmixAPIClient.api.requests') as mock_request:
124+
mock_get = mock.Mock()
125+
mock_get.status_code = 200
126+
mock_get.json.return_value = {}
127+
mock_request.get.return_value = mock_get
128+
129+
self.cmix_api.get_survey_data_layouts(self.survey_id)
130+
131+
base_url = CMIX_SERVICES['survey']['BASE_URL']
132+
surveys_url = '{}/surveys/{}/data-layouts'.format(base_url, self.survey_id)
133+
mock_request.get.assert_any_call(surveys_url, headers=self.cmix_api._authentication_headers)
134+
135+
# error case (survey not found)
136+
with mock.patch('CmixAPIClient.api.requests') as mock_request:
137+
mock_get = mock.Mock()
138+
mock_get.status_code = 404
139+
mock_get.json.return_value = {}
140+
mock_request.get.return_value = mock_get
141+
142+
with self.assertRaises(CmixError):
143+
self.cmix_api.get_survey_data_layouts(self.survey_id)
144+
119145
def test_get_survey_status(self):
120146
self.cmix_api._authentication_headers = {'Authentication': 'Bearer test'}
121147

0 commit comments

Comments
 (0)