Skip to content

Commit bae1d96

Browse files
Merge branch 'dev' into 7-get-locales
2 parents e76a0b1 + 86805e2 commit bae1d96

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
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_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_locales(survey_id)
3535
get_survey_xml(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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ def test_get_survey_locales(self):
178178
mock_get.status_code = 200
179179
mock_get.json.return_value = {}
180180
mock_request.get.return_value = mock_get
181-
182181
self.cmix_api.get_survey_locales(self.survey_id)
183182

184183
base_url = CMIX_SERVICES['survey']['BASE_URL']
@@ -195,6 +194,31 @@ def test_get_survey_locales(self):
195194
with self.assertRaises(CmixError):
196195
self.cmix_api.get_survey_locales(self.survey_id)
197196

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

0 commit comments

Comments
 (0)