Skip to content

Commit cc35b44

Browse files
Merge pull request #98 from dynata/6-get-sections
added get survey sections function
2 parents f8eb3fa + 84383bf commit cc35b44

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

CmixAPIClient/api.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,19 @@ def get_survey_status(self, survey_id):
235235
raise CmixError('Get Survey Status returned without a status. Response: {}'.format(status_response.json()))
236236
return status.lower()
237237

238+
def get_survey_sections(self, survey_id):
239+
self.check_auth_headers()
240+
sections_url = '{}/surveys/{}/sections'.format(CMIX_SERVICES['survey'][self.url_type], survey_id)
241+
sections_response = requests.get(sections_url, headers=self._authentication_headers)
242+
if sections_response.status_code != 200:
243+
raise CmixError(
244+
'CMIX returned a non-200 response code while getting sections: {} and error {}'.format(
245+
sections_response.status_code,
246+
sections_response.text
247+
)
248+
)
249+
return sections_response.json()
250+
238251
def get_survey_sources(self, survey_id):
239252
self.check_auth_headers()
240253
sources_url = '{}/surveys/{}/sources'.format(CMIX_SERVICES['survey'][self.url_type], survey_id)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ A Python client library for the [Dynata Cmix API](https://wiki2.criticalmix.net/
3333
get_survey_definition(survey_id)
3434
get_survey_locales(survey_id)
3535
get_survey_xml(survey_id)
36+
get_survey_sections(survey_id)
3637
get_survey_simulations(survey_id)
3738
get_survey_sources(survey_id)
3839
get_survey_test_url(survey_id)

tests/test_api.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,32 @@ def test_get_survey_status_error_handled(self):
169169
with self.assertRaises(CmixError):
170170
self.cmix_api.get_survey_status(self.survey_id)
171171

172+
def test_get_survey_sections(self):
173+
self.cmix_api._authentication_headers = {'Authentication': 'Bearer test'}
174+
175+
# success case
176+
with mock.patch('CmixAPIClient.api.requests') as mock_request:
177+
mock_get = mock.Mock()
178+
mock_get.status_code = 200
179+
mock_get.json.return_value = {}
180+
mock_request.get.return_value = mock_get
181+
182+
self.cmix_api.get_survey_sections(self.survey_id)
183+
184+
base_url = CMIX_SERVICES['survey']['BASE_URL']
185+
surveys_url = '{}/surveys/{}/sections'.format(base_url, self.survey_id)
186+
mock_request.get.assert_any_call(surveys_url, headers=self.cmix_api._authentication_headers)
187+
188+
# error case (survey not found)
189+
with mock.patch('CmixAPIClient.api.requests') as mock_request:
190+
mock_get = mock.Mock()
191+
mock_get.status_code = 404
192+
mock_get.json.return_value = {}
193+
mock_request.get.return_value = mock_get
194+
195+
with self.assertRaises(CmixError):
196+
self.cmix_api.get_survey_sections(self.survey_id)
197+
172198
def test_get_survey_locales(self):
173199
self.cmix_api._authentication_headers = {'Authentication': 'Bearer test'}
174200

0 commit comments

Comments
 (0)