Skip to content

Commit 93814b1

Browse files
committed
Merge branch 'dev' into 8-get-survey-sources
2 parents 541055c + e4014d2 commit 93814b1

File tree

3 files changed

+83
-14
lines changed

3 files changed

+83
-14
lines changed

CmixAPIClient/api.py

Lines changed: 29 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)
@@ -255,25 +268,14 @@ def create_export_archive(self, survey_id, export_type):
255268
)
256269
archive_json = archive_response.json()
257270

258-
layout_url = '{}/surveys/{}/data-layouts/'.format(CMIX_SERVICES['survey']["BASE_URL"], survey_id)
259-
layout_response = requests.get(layout_url, headers=self._authentication_headers)
260-
if layout_response.status_code != 200:
261-
raise CmixError(
262-
'CMIX returned a non-200 response code: {} and error {}'.format(
263-
layout_response.status_code,
264-
layout_response.text
265-
)
266-
)
271+
layout_json = self.get_survey_data_layouts(survey_id)
267272
layout_id = None
268-
for layout in layout_response.json():
273+
for layout in layout_json:
269274
if layout.get('name') == 'Default':
270275
layout_id = layout.get('id')
271276
if layout_id is None:
272277
raise CmixError(
273-
'Layouts response did not contain a "Default" layout. Response Code: {}, Body {}'.format(
274-
layout_response.status_code,
275-
layout_response.content
276-
)
278+
'Layouts response did not contain a "Default" layout.'
277279
)
278280

279281
archive_json['dataLayoutId'] = layout_id
@@ -348,3 +350,16 @@ def create_survey(self, xml_string):
348350
response_json = response.json()
349351
self.update_project(response_json.get('projectId'), status=self.SURVEY_STATUS_DESIGN)
350352
return response_json
353+
354+
def get_survey_simulations(self, survey_id):
355+
self.check_auth_headers()
356+
simulations_url = '{}/surveys/{}/simulations'.format(CMIX_SERVICES['survey'][self.url_type], survey_id)
357+
simulations_response = requests.get(simulations_url, headers=self._authentication_headers)
358+
if simulations_response.status_code != 200:
359+
raise CmixError(
360+
'CMIX returned a non-200 response code while getting simulations: {} and error {}'.format(
361+
simulations_response.status_code,
362+
simulations_response.text
363+
)
364+
)
365+
return simulations_response.json()

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ A Python client library for the [Dynata Cmix API](https://wiki2.criticalmix.net/
2929
fetch_banner_filter(survey_id, question_a, question_b, response_id)
3030
fetch_raw_results(survey_id, payload)
3131
get_surveys(status, *args, **kwargs)
32+
get_survey_data_layouts(survey_id)
3233
get_survey_definition(survey_id)
3334
get_survey_xml(survey_id)
35+
get_survey_simulations(survey_id)
3436
get_survey_test_url(survey_id)
3537
get_survey_respondents(survey_id, respondent_type, live)
3638
get_survey_status(survey_id)

tests/test_api.py

Lines changed: 52 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

@@ -296,3 +322,29 @@ def test_add_extra_url_params(self):
296322
extra_params_formatted = '&{}&{}'.format(extra_params[0], extra_params[1])
297323
expected = '{}{}'.format(url, extra_params_formatted)
298324
self.assertEqual(result, expected)
325+
326+
def test_get_survey_simulations(self):
327+
self.cmix_api._authentication_headers = {'Authentication': 'Bearer test'}
328+
329+
# success case
330+
with mock.patch('CmixAPIClient.api.requests') as mock_request:
331+
mock_get = mock.Mock()
332+
mock_get.status_code = 200
333+
mock_get.json.return_value = {}
334+
mock_request.get.return_value = mock_get
335+
336+
self.cmix_api.get_survey_simulations(self.survey_id)
337+
338+
base_url = CMIX_SERVICES['survey']['BASE_URL']
339+
surveys_url = '{}/surveys/{}/simulations'.format(base_url, self.survey_id)
340+
mock_request.get.assert_any_call(surveys_url, headers=self.cmix_api._authentication_headers)
341+
342+
# error case (survey not found)
343+
with mock.patch('CmixAPIClient.api.requests') as mock_request:
344+
mock_get = mock.Mock()
345+
mock_get.status_code = 404
346+
mock_get.json.return_value = {}
347+
mock_request.get.return_value = mock_get
348+
349+
with self.assertRaises(CmixError):
350+
self.cmix_api.get_survey_simulations(self.survey_id)

0 commit comments

Comments
 (0)