10
10
CMIX_SERVICES = {
11
11
'auth' : {
12
12
'BASE_URL' : 'https://auth.cmix.com' ,
13
+ 'TEST_URL' : 'https://kaleidoscope-auth.cmix.com' ,
13
14
},
14
15
'file' : {
15
16
'BASE_URL' : 'https://file-processing.cmix.com' ,
17
+ 'TEST_URL' : 'https://kaleidoscope-file-processing.cmix.com' ,
16
18
},
17
19
'launchpad' : {
18
20
'BASE_URL' : 'https://launchpad.cmix.com' ,
21
+ 'TEST_URL' : 'https://kaleidoscope-launchpad.cmix.com' ,
19
22
},
20
23
'reporting' : {
21
24
'BASE_URL' : 'https://reporting-api.cmix.com' ,
25
+ 'TEST_URL' : 'https://kaleidoscope-reporting-api.cmix.com' ,
22
26
},
23
27
'survey' : {
24
28
'BASE_URL' : 'https://survey-api.cmix.com' ,
29
+ 'TEST_URL' : 'https://kaleidoscope-survey-api.cmix.com' ,
25
30
},
26
31
'test' : {
27
32
'BASE_URL' : 'https://test.cmix.com' ,
33
+ 'TEST_URL' : 'https://kaleidoscope-test.cmix.com' ,
28
34
},
29
35
}
30
36
@@ -46,13 +52,16 @@ class CmixAPI(object):
46
52
# valid extra survey url params
47
53
SURVEY_PARAMS_STATUS_AFTER = 'statusAfter'
48
54
49
- def __init__ (self , username = None , password = None , client_id = None , client_secret = None , * args , ** kwargs ):
55
+ def __init__ (self , username = None , password = None , client_id = None , client_secret = None , test = False , * args , ** kwargs ):
50
56
if None in [username , password , client_id , client_secret ]:
51
57
raise CmixError ("All authentication data is required." )
52
58
self .username = username
53
59
self .password = password
54
60
self .client_id = client_id
55
61
self .client_secret = client_secret
62
+ self .url_type = 'BASE_URL'
63
+ if test is True :
64
+ self .url_type = 'TEST_URL'
56
65
57
66
def check_auth_headers (self ):
58
67
if self ._authentication_headers is None :
@@ -67,7 +76,7 @@ def authenticate(self, *args, **kwargs):
67
76
"password" : self .password
68
77
}
69
78
70
- auth_url = '{}/access-token' .format (CMIX_SERVICES ['auth' ]['BASE_URL' ])
79
+ auth_url = '{}/access-token' .format (CMIX_SERVICES ['auth' ][self . url_type ])
71
80
try :
72
81
auth_response = requests .post (auth_url , json = auth_payload , headers = {"Content-Type" : "application/json" })
73
82
if auth_response .status_code != 200 :
@@ -95,7 +104,7 @@ def fetch_banner_filter(self, survey_id, question_a, question_b, response_id):
95
104
response_id
96
105
)
97
106
)
98
- base_url = CMIX_SERVICES ['reporting' ]['BASE_URL' ]
107
+ base_url = CMIX_SERVICES ['reporting' ][self . url_type ]
99
108
url = '{}/surveys/{}/response-counts' .format (base_url , survey_id )
100
109
payload = {
101
110
'testYN' : 'LIVE' ,
@@ -125,7 +134,7 @@ def fetch_raw_results(self, survey_id, payload):
125
134
'''
126
135
self .check_auth_headers ()
127
136
log .debug ('Requesting raw results for CMIX survey {}' .format (survey_id ))
128
- base_url = CMIX_SERVICES ['reporting' ]['BASE_URL' ]
137
+ base_url = CMIX_SERVICES ['reporting' ][self . url_type ]
129
138
url = '{}/surveys/{}/response-counts' .format (base_url , survey_id )
130
139
response = requests .post (url , headers = self ._authentication_headers , json = payload )
131
140
return response .json ()
@@ -140,7 +149,7 @@ def get_surveys(self, status, *args, **kwargs):
140
149
get_surveys('status', extra_params=params)
141
150
'''
142
151
self .check_auth_headers ()
143
- base_url = CMIX_SERVICES ['survey' ]['BASE_URL' ]
152
+ base_url = CMIX_SERVICES ['survey' ][self . url_type ]
144
153
surveys_url = '{}/surveys?status={}' .format (base_url , status )
145
154
extra_params = kwargs .get ('extra_params' )
146
155
if extra_params is not None :
@@ -156,25 +165,25 @@ def add_extra_url_params(self, url, params):
156
165
157
166
def get_survey_definition (self , survey_id ):
158
167
self .check_auth_headers ()
159
- definition_url = '{}/surveys/{}/definition' .format (CMIX_SERVICES ['survey' ]['BASE_URL' ], survey_id )
168
+ definition_url = '{}/surveys/{}/definition' .format (CMIX_SERVICES ['survey' ][self . url_type ], survey_id )
160
169
definition_response = requests .get (definition_url , headers = self ._authentication_headers )
161
170
return definition_response .json ()
162
171
163
172
def get_survey_xml (self , survey_id ):
164
173
self .check_auth_headers ()
165
- xml_url = '{}/surveys/{}' .format (CMIX_SERVICES ['file' ]['BASE_URL' ], survey_id )
174
+ xml_url = '{}/surveys/{}' .format (CMIX_SERVICES ['file' ][self . url_type ], survey_id )
166
175
xml_response = requests .get (xml_url , headers = self ._authentication_headers )
167
176
return xml_response .content
168
177
169
178
def get_survey_test_url (self , survey_id ):
170
179
self .check_auth_headers ()
171
- survey_url = '{}/surveys/{}' .format (CMIX_SERVICES ['survey' ]['BASE_URL' ], survey_id )
180
+ survey_url = '{}/surveys/{}' .format (CMIX_SERVICES ['survey' ][self . url_type ], survey_id )
172
181
survey_response = requests .get (survey_url , headers = self ._authentication_headers )
173
182
test_token = survey_response .json ().get ('testToken' , None )
174
183
if test_token is None :
175
184
raise CmixError ('Survey endpoint for CMIX ID {} did not return a test token.' .format (survey_id ))
176
185
test_link = '{}/#/?cmixSvy={}&cmixTest={}' .format (
177
- CMIX_SERVICES ['test' ]['BASE_URL' ],
186
+ CMIX_SERVICES ['test' ][self . url_type ],
178
187
survey_id ,
179
188
test_token
180
189
)
@@ -183,7 +192,7 @@ def get_survey_test_url(self, survey_id):
183
192
def get_survey_respondents (self , survey_id , respondent_type , live ):
184
193
self .check_auth_headers ()
185
194
respondents_url = '{}/surveys/{}/respondents?respondentType={}&respondentStatus={}' .format (
186
- CMIX_SERVICES ['reporting' ]['BASE_URL' ],
195
+ CMIX_SERVICES ['reporting' ][self . url_type ],
187
196
survey_id ,
188
197
"LIVE" if live else "TEST" ,
189
198
respondent_type ,
@@ -193,7 +202,7 @@ def get_survey_respondents(self, survey_id, respondent_type, live):
193
202
194
203
def get_survey_status (self , survey_id ):
195
204
self .check_auth_headers ()
196
- status_url = '{}/surveys/{}' .format (CMIX_SERVICES ['survey' ]['BASE_URL' ], survey_id )
205
+ status_url = '{}/surveys/{}' .format (CMIX_SERVICES ['survey' ][self . url_type ], survey_id )
197
206
status_response = requests .get (status_url , headers = self ._authentication_headers )
198
207
status = status_response .json ().get ('status' , None )
199
208
if status is None :
@@ -205,7 +214,7 @@ def get_survey_completes(self, survey_id):
205
214
206
215
def create_export_archive (self , survey_id , export_type ):
207
216
self .check_auth_headers ()
208
- archive_url = '{}/surveys/{}/archives' .format (CMIX_SERVICES ['survey' ]['BASE_URL' ], survey_id )
217
+ archive_url = '{}/surveys/{}/archives' .format (CMIX_SERVICES ['survey' ][self . url_type ], survey_id )
209
218
headers = self ._authentication_headers .copy ()
210
219
headers ['Content-Type' ] = "application/json"
211
220
payload = {
@@ -294,7 +303,7 @@ def update_project(self, project_id, status=None):
294
303
if payload_json == {}:
295
304
raise CmixError ("No update data was provided for CMIX Project {}" .format (project_id ))
296
305
297
- url = '{}/projects/{}' .format (CMIX_SERVICES ['survey' ]['BASE_URL' ], project_id )
306
+ url = '{}/projects/{}' .format (CMIX_SERVICES ['survey' ][self . url_type ], project_id )
298
307
response = requests .patch (url , json = payload_json , headers = self ._authentication_headers )
299
308
if response .status_code > 299 :
300
309
raise CmixError (
@@ -311,7 +320,7 @@ def create_survey(self, xml_string):
311
320
'''
312
321
self .check_auth_headers ()
313
322
314
- url = '{}/surveys/data' .format (CMIX_SERVICES ['file' ]['BASE_URL' ])
323
+ url = '{}/surveys/data' .format (CMIX_SERVICES ['file' ][self . url_type ])
315
324
payload = {"data" : xml_string }
316
325
response = requests .post (url , payload , headers = self ._authentication_headers )
317
326
if response .status_code > 299 :
0 commit comments