Skip to content

Commit 541055c

Browse files
committed
add get survey sources function
1 parent b427bf3 commit 541055c

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

CmixAPIClient/api.py

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

212+
def get_survey_sources(self, survey_id):
213+
self.check_auth_headers()
214+
sources_url = '{}/surveys/{}/sources'.format(CMIX_SERVICES['survey'][self.url_type], survey_id)
215+
sources_response = requests.get(sources_url, headers=self._authentication_headers)
216+
if sources_response.status_code != 200:
217+
raise CmixError(
218+
'CMIX returned a non-200 response code while getting sources: {} and error {}'.format(
219+
sources_response.status_code,
220+
sources_response.text
221+
)
222+
)
223+
return sources_response.json()
224+
212225
def get_survey_completes(self, survey_id):
213226
return self.get_survey_respondents(survey_id, "COMPLETE", True)
214227

tests/test_api.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,32 @@ def test_get_survey_status_error_handled(self):
143143
with self.assertRaises(CmixError):
144144
self.cmix_api.get_survey_status(self.survey_id)
145145

146+
def test_get_survey_sources(self):
147+
self.cmix_api._authentication_headers = {'Authentication': 'Bearer test'}
148+
149+
# success case
150+
with mock.patch('CmixAPIClient.api.requests') as mock_request:
151+
mock_get = mock.Mock()
152+
mock_get.status_code = 200
153+
mock_get.json.return_value = {}
154+
mock_request.get.return_value = mock_get
155+
156+
self.cmix_api.get_survey_sources(self.survey_id)
157+
158+
base_url = CMIX_SERVICES['survey']['BASE_URL']
159+
surveys_url = '{}/surveys/{}/sources'.format(base_url, self.survey_id)
160+
mock_request.get.assert_any_call(surveys_url, headers=self.cmix_api._authentication_headers)
161+
162+
# error case (survey not found)
163+
with mock.patch('CmixAPIClient.api.requests') as mock_request:
164+
mock_get = mock.Mock()
165+
mock_get.status_code = 404
166+
mock_get.json.return_value = {}
167+
mock_request.get.return_value = mock_get
168+
169+
with self.assertRaises(CmixError):
170+
self.cmix_api.get_survey_sources(self.survey_id)
171+
146172
def test_get_survey_test_url(self):
147173
self.cmix_api._authentication_headers = {'Authentication': 'Bearer test'}
148174
correct_test_link = '{}/#/?cmixSvy={}&cmixTest={}'.format(

0 commit comments

Comments
 (0)