Skip to content

Commit 86805e2

Browse files
Merge pull request #100 from wogsland/8-get-survey-sources
add get survey sources function
2 parents e4014d2 + 8e25bd1 commit 86805e2

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
@@ -222,6 +222,19 @@ def get_survey_status(self, survey_id):
222222
raise CmixError('Get Survey Status returned without a status. Response: {}'.format(status_response.json()))
223223
return status.lower()
224224

225+
def get_survey_sources(self, survey_id):
226+
self.check_auth_headers()
227+
sources_url = '{}/surveys/{}/sources'.format(CMIX_SERVICES['survey'][self.url_type], survey_id)
228+
sources_response = requests.get(sources_url, headers=self._authentication_headers)
229+
if sources_response.status_code != 200:
230+
raise CmixError(
231+
'CMIX returned a non-200 response code while getting sources: {} and error {}'.format(
232+
sources_response.status_code,
233+
sources_response.text
234+
)
235+
)
236+
return sources_response.json()
237+
225238
def get_survey_completes(self, survey_id):
226239
return self.get_survey_respondents(survey_id, "COMPLETE", True)
227240

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_xml(survey_id)
3535
get_survey_simulations(survey_id)
36+
get_survey_sources(survey_id)
3637
get_survey_test_url(survey_id)
3738
get_survey_respondents(survey_id, respondent_type, live)
3839
get_survey_status(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_sources(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_sources(self.survey_id)
183+
184+
base_url = CMIX_SERVICES['survey']['BASE_URL']
185+
surveys_url = '{}/surveys/{}/sources'.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_sources(self.survey_id)
197+
172198
def test_get_survey_test_url(self):
173199
self.cmix_api._authentication_headers = {'Authentication': 'Bearer test'}
174200
correct_test_link = '{}/#/?cmixSvy={}&cmixTest={}'.format(

0 commit comments

Comments
 (0)