Skip to content

Commit dd0cadb

Browse files
committed
Merge branch '6-get-sections' of https://github.com/dynata/python-cmixapi-client into 6-get-sections
2 parents 273f430 + 0d13e2b commit dd0cadb

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-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_sections(self, survey_id):
235235
)
236236
return sections_response.json()
237237

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

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ A Python client library for the [Dynata Cmix API](https://wiki2.criticalmix.net/
3434
get_survey_xml(survey_id)
3535
get_survey_sections(survey_id)
3636
get_survey_simulations(survey_id)
37+
get_survey_sources(survey_id)
3738
get_survey_test_url(survey_id)
3839
get_survey_respondents(survey_id, respondent_type, live)
3940
get_survey_status(survey_id)

tests/test_api.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,31 @@ def test_get_survey_sections(self):
195195
with self.assertRaises(CmixError):
196196
self.cmix_api.get_survey_sections(self.survey_id)
197197

198+
def test_get_survey_sources(self):
199+
self.cmix_api._authentication_headers = {'Authentication': 'Bearer test'}
200+
201+
# success case
202+
with mock.patch('CmixAPIClient.api.requests') as mock_request:
203+
mock_get = mock.Mock()
204+
mock_get.status_code = 200
205+
mock_get.json.return_value = {}
206+
mock_request.get.return_value = mock_get
207+
self.cmix_api.get_survey_sources(self.survey_id)
208+
209+
base_url = CMIX_SERVICES['survey']['BASE_URL']
210+
surveys_url = '{}/surveys/{}/sources'.format(base_url, self.survey_id)
211+
mock_request.get.assert_any_call(surveys_url, headers=self.cmix_api._authentication_headers)
212+
213+
# error case (survey not found)
214+
with mock.patch('CmixAPIClient.api.requests') as mock_request:
215+
mock_get = mock.Mock()
216+
mock_get.status_code = 404
217+
mock_get.json.return_value = {}
218+
mock_request.get.return_value = mock_get
219+
220+
with self.assertRaises(CmixError):
221+
self.cmix_api.get_survey_sources(self.survey_id)
222+
198223
def test_get_survey_test_url(self):
199224
self.cmix_api._authentication_headers = {'Authentication': 'Bearer test'}
200225
correct_test_link = '{}/#/?cmixSvy={}&cmixTest={}'.format(

0 commit comments

Comments
 (0)