Skip to content

Commit 0759e1b

Browse files
committed
add get termination codes function
1 parent b427bf3 commit 0759e1b

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
@@ -212,6 +212,19 @@ def get_survey_status(self, survey_id):
212212
def get_survey_completes(self, survey_id):
213213
return self.get_survey_respondents(survey_id, "COMPLETE", True)
214214

215+
def get_survey_termination_codes(self, survey_id):
216+
self.check_auth_headers()
217+
termination_codes_url = '{}/surveys/{}/termination-codes'.format(CMIX_SERVICES['survey'][self.url_type], survey_id)
218+
termination_codes_response = requests.get(termination_codes_url, headers=self._authentication_headers)
219+
if termination_codes_response.status_code != 200:
220+
raise CmixError(
221+
'CMIX returned a non-200 response code while getting termination_codes: {} and error {}'.format(
222+
termination_codes_response.status_code,
223+
termination_codes_response.text
224+
)
225+
)
226+
return termination_codes_response.json()
227+
215228
def create_export_archive(self, survey_id, export_type):
216229
self.check_auth_headers()
217230
archive_url = '{}/surveys/{}/archives'.format(CMIX_SERVICES['survey'][self.url_type], survey_id)

tests/test_api.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,32 @@ def test_get_survey_completes(self):
183183
]
184184
self.assertEqual(self.cmix_api.get_survey_completes(self.survey_id), mock_respondents)
185185

186+
def test_get_survey_termination_codes(self):
187+
self.cmix_api._authentication_headers = {'Authentication': 'Bearer test'}
188+
189+
# success case
190+
with mock.patch('CmixAPIClient.api.requests') as mock_request:
191+
mock_get = mock.Mock()
192+
mock_get.status_code = 200
193+
mock_get.json.return_value = {}
194+
mock_request.get.return_value = mock_get
195+
196+
self.cmix_api.get_survey_termination_codes(self.survey_id)
197+
198+
base_url = CMIX_SERVICES['survey']['BASE_URL']
199+
surveys_url = '{}/surveys/{}/termination-codes'.format(base_url, self.survey_id)
200+
mock_request.get.assert_any_call(surveys_url, headers=self.cmix_api._authentication_headers)
201+
202+
# error case (survey not found)
203+
with mock.patch('CmixAPIClient.api.requests') as mock_request:
204+
mock_get = mock.Mock()
205+
mock_get.status_code = 404
206+
mock_get.json.return_value = {}
207+
mock_request.get.return_value = mock_get
208+
209+
with self.assertRaises(CmixError):
210+
self.cmix_api.get_survey_termination_codes(self.survey_id)
211+
186212
def test_get_surveys(self):
187213
with mock.patch('CmixAPIClient.api.requests') as mock_request:
188214
mock_post = mock.Mock()

0 commit comments

Comments
 (0)